2011-06-02 98 views
1

我正在研究任務調度器應用程序作爲我的大學項目,我有一個服務來檢查任務的過期時間。我實施了處理程序來檢查到期時間。當應用程序的到期時間與當前時間匹配時,它會發送狀態欄通知。在這一點上,我暫停使用Thread.sleep方法的線程一分鐘,這導致我的應用程序掛起。在logcat中,它顯示了應用程序對CPU的大量使用。Thread.sleep使得程序掛起?

我正在從數據庫中獲取數據,但是當Thread.sleep未被調用時,它工作正常。 請幫忙。

下面是代碼:

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.icon; 
         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(); 
} 

回答

2

當你在這裏使用HandlerpostDelayed功能是發生了什麼:

  1. 處理程序使您Runnable到當前線程的活套隊列。
  2. 當時間到了,您的Runnable中的代碼在UI線程上運行

請注意,它不是Handler總是將Runnable放到UI線程隊列。它將Runnable置爲當前線程的隊列,並且您當前的線程是UI線程。

所以,如果你把Thread.sleep或其他任何耗費時間的東西(比如網絡通信)updateTimeTask它會掛起整個UI線程。

在你的情況下,你應該使用ScheduledExecutorService,見scheduleWithFixedDelay函數。或者作爲替代方案,您可以從updateTimeTask功能開始AsyncTask並完成所有重起動功能,並在doInBackgrund功能中執行Thread.sleep

+0

該問題已通過使用handler.postDelayed(

+0

此問題的另一個解決方案是以hh:mm:ss格式匹配時間 – 2011-06-14 07:06:32