Working with handlers and threads in service, Thread.sleep makes the program to hang?
i am working on a task scheduler applications as my college project, i have a service that checks the expire time of an task. i had implemented handlers to check the expire time. When the expire time of an application is matched with current time then it sends a status bar notification. At this point i am pausing the Thread using Thread.sleep method for a minute, which is causing my application to hang. At logcat it shows heavy CPU usages by applications.
i am fetching the data from database, but it works fine when Thread.sleep
is not called.
Please help.
Here is the code:
package com.apps.niit.taskm;
import java.util.ArrayList;
import java.util.Calendar;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.util.Log;
public class ExpireTimeService extends Service {
DataHelper dh;
ArrayList<String> tData=new ArrayList<String>();
String date;
Calendar c;
String str;
String str1;
String str2;
String str3;
String str4;
String str5;
int notificationID=1;
String [][] data;
NotificationManager notificationManager;
@Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
}
@Override
public void onCreate(){
super.onCreate();
dh = new DataHelper(this);
fetchData();
handler.removeCallbacks(updateTimeTask);
handler.postDelayed(updateTimeTask, 1000);
}
public void fetchData(){
String eDate = android.text.format.DateFormat.format("d/M/yyyy", new java.util.Date()).toString();
tData.addAll(this.dh.selectDate(eDate));
data =new String[tData.size()][4];
if(!tData.isEmpty()){
for(int i=0; i<tData.size();i++){
breakString(tData.get(i));
data[i][0]=str1;
data[i][1]=str2;
data[i][2]=str3;
data[i][3]=str4;
}
}
}
public void stopService(){
stopSelf();
}
private Runnable updateTimeTask = new Runnable() {
public void run(){
try {
String time = android.text.format.DateFormat.format("k:m", new java.util.Date()).toString();
for(int i=0; i<tData.size();i++){
if(data[i][3].equals(time)){
//send notification code goes here
String serName = Context.NOTIFICATION_SERVICE;
notificationManager = (NotificationManager)getSystemService(serName);
String ticker= data[i][0]+" "+data[i][1]+" "+data[i][2]+" "+data[i][3];
long when= System.currentTimeMillis();
int icon = R.drawable.i开发者_运维问答con;
Notification notification = new Notification(icon, ticker,when);
Intent intent = new Intent (getApplicationContext(), DisplayTask.class);
Bundle myBundle = new Bundle();
myBundle.putString("str1", data[i][0]);
myBundle.putString("str2", data[i][1]);Log.i("data1",data[i][1]);
myBundle.putString("str3", data[i][2]);Log.i("data1",data[i][2]);
myBundle.putString("str4", data[i][3]);Log.i("data1",data[i][3]);
intent.putExtras(myBundle);
PendingIntent launchIntent = PendingIntent.getActivity(getApplicationContext(), 0, intent, 0);
notification.setLatestEventInfo(getApplicationContext(), "", "", launchIntent);
notificationID=1;
notificationManager.notify(notificationID, notification);
Thread.sleep(10000);
}
}
handler.postDelayed(this, 1000);
} catch (Exception e) {
// TODO Auto-generated catch block
Log.e("Error from service", e.toString());
}
}
};
private void breakString(String str) {
// TODO Auto-generated method stub
str1 = str.substring(0, str.indexOf(";"));
str = str.substring(str1.length()+1, str.length());
str2 = str.substring(0, str.indexOf(";"));
str = str.substring(str2.length()+1, str.length());
str3 = str.substring(0, str.indexOf(";"));
str4 = str.substring(str3.length()+1, str.length());
}
@Override
public void onDestroy() {
super.onDestroy();
if (handler != null)
handler.removeCallbacks(updateTimeTask);
handler = null;
}
private Handler handler = new Handler();
}
When you're using Handler
's postDelayed
function here is what happens:
- Handler puts your
Runnable
into the current thread's Looper queue. - When time comes, your code in
Runnable
is run on the UI thread.
Note that its not that Handler
always puts Runnable
to UI thread queue. It puts Runnable
to current thread's queue, and your current thread was the UI thread.
So if you put Thread.sleep
or anything time consuming (like network communication) in that updateTimeTask
it will hang the whole UI thread.
In your case you should use ScheduledExecutorService
, see scheduleWithFixedDelay
function. Or as alternative, you can start AsyncTask
from your updateTimeTask
function and do all heavy-lifting and Thread.sleep
in doInBackgrund
function.
精彩评论