2014-01-19 58 views
1

我有一個啓動新服務的應用程序。在這項服務中,我目前有一個處理程序,它每分鐘執行一些工作,然後通過BroadcastReceiver將結果發送到主要活動。我想要以下事情:每分鐘在服務內部創建一個新線程,使其完成工作並向處理程序發送一條消息,以完成處理,然後處理程序將通過BroadcastReceiver發送到主要活動。我如何結合線程和處理程序?下面是我的興趣而帶處理程序和線程的Android服務

private Runnable sendUpdatesToUI = new Runnable() { 



    public void run() { 
     try { 
      getAppResources(); //this is the work i want to place in a new thread 
     } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 

     intent.putExtra(key,value); 
     sendBroadcast(intent); 
     handler.postDelayed(this, 60*1000); 
    } 
}; 

這裏到目前爲止的代碼 - 只有一部分是我瞭解我需要做的

private Runnable sendUpdatesToUI = new Runnable() { 

    public void run() { 
     /* try { 
      getAppResources(); 
     } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     }*/ 

     Thread t = new Thread() { 
      @Override 
      public void run(){ 
       try { 
        getAppResources(); 
       } catch (IOException e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
       } 
       handler.sendEmptyMessage(0); 
      } 
     }; 


     handler.postDelayed(this, 60*1000); 
    } 
}; 

,從哪兒可以放置的handleMessage?如果我把它放在Runnable裏面,它說它永遠不會在本地使用。我只是把它放在Runnable之前?

public void handleMessage(Message msg){ 
    if(msg.what == 0){ 
     intent.putExtra(key,value); 
     sendBroadcast(intent); 
    } 
} 

這是我該怎麼做的嗎?

編輯:處理程序發送到主活動的一些數據

private final Handler handler = new Handler(){ 
    public void handleMessage(Message msg){ 
     if(msg.what == 0){ 
      Log.d("HANDLE","Am primit mesaj"); 
      //Notify preparations 
      intent.putExtra("RunningApps", runningApps.size()); 
      intent.putExtra("CPU", highestDrainPackageCPU); 

      intent.putExtra("GPS",highestDrainPackageGPS); 
      intent.putExtra("WIFI", highestDrainPackageWIFI); 

      //Now i have all my data, time to send them to the activity 
      //First , send the strings to be set in the TextViews 
      //Each running app has 7 messages to display -> ArrayList<String> 
      for(int i=0;i<runningApps.size();i++){ 
       intent.putStringArrayListExtra(String.valueOf(i), appInfo.get(i)); 
      } 

      //Next send values to plot the chart 
      //CPU energy consumption for highest draining application 
      double [] currValues_cpu = new double[tableCPU.get(highestDrainPackageCPU).size()]; 
      Log.d("CPUSIZE",String.valueOf(currValues_cpu.length)); 
      for(int j=0;j<tableCPU.get(highestDrainPackageCPU).size();j++){ 
       currValues_cpu[j]=tableCPU.get(highestDrainPackageCPU).get(j); 
       Log.d("CPUVALUE",String.valueOf(currValues_cpu[j])+"For application"+highestDrainPackageCPU); 
      } 

      intent.putExtra("highestDrainPackageCPU", currValues_cpu); 


      //GPS energy consumption for highest draining application 
      double [] currValues_gps = new double[tableGPS.get(highestDrainPackageGPS).size()]; 
      for(int j=0;j<tableGPS.get(highestDrainPackageGPS).size();j++){ 
       currValues_gps[j]=tableGPS.get(highestDrainPackageGPS).get(j); 
      } 
      intent.putExtra("highestDrainPackageGPS", currValues_gps); 

      //WIFI energy consumption for highest draining application 
      double [] currValues_wifi = new double[tableWIFI.get(highestDrainPackageWIFI).size()]; 
      for(int j=0;j<tableWIFI.get(highestDrainPackageWIFI).size();j++){ 
       currValues_wifi[j]=tableWIFI.get(highestDrainPackageWIFI).get(j); 
      } 
      intent.putExtra("highestDrainPackageWIFI", currValues_wifi); 

      sendBroadcast(intent); 

     } 
    } 
}; 

下面的代碼是在主要活動和UpdateUI功能的廣播接收器:

private BroadcastReceiver broadcastReceiver = new BroadcastReceiver() { 
    @Override 
    public void onReceive(Context context, Intent intent) { 
     updateUI(intent);  
    } 
}; 

public void updateUI(Intent intent){ 

    resourceTab.removeAllViews(); 
    //statisticsTab.removeAllViews(); 

    int apps_no = intent.getIntExtra("RunningApps", 0); 

    String highestDrainPackageCPU = intent.getStringExtra("CPU"); 

    String highestDrainPackageGPS = intent.getStringExtra("GPS"); 

    String highestDrainPackageWIFI = intent.getStringExtra("WIFI"); 


    //TO-DO: Get information for each app and store it in textview.Then add it to a linearlayout 
    for(int i=0;i<apps_no;i++){ 

     //Setup delimiter 
     View delimitator = new View(this); 
     delimitator.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,1)); 
     delimitator.setBackgroundColor(Color.parseColor("#50FFFFFF")); 

     //Extract values 
     ArrayList<String> info = new ArrayList<String>(); 
     info=intent.getStringArrayListExtra(String.valueOf(i)); 

     for(int j=0;j<info.size();j++){ 
      TextView infoApp = new TextView(this); 
      //////Setup textview////////// 
      infoApp = new TextView(this); 
      infoApp.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT)); 
      infoApp.setTextColor(Color.parseColor("#FFFFFF")); 
      infoApp.setText(info.get(j)); 

      resourceTab.addView(infoApp); 
     } 

     //Add delimiter 
     resourceTab.addView(delimitator); 
    } 

    double [] cpu_values = intent.getDoubleArrayExtra("highestDrainPackageCPU"); 

    double [] gps_values = intent.getDoubleArrayExtra("highestDrainPackageGPS"); 

    double [] wifi_values = intent.getDoubleArrayExtra("highestDrainPackageWIFI"); 

    //Now plot the graph 
    createGraphOverall(cpu_values, gps_values, wifi_values, highestDrainPackageCPU, highestDrainPackageGPS, highestDrainPackageWIFI); 

    //Update the table 
    updateTable(cpu_values, gps_values, wifi_values, highestDrainPackageCPU, highestDrainPackageGPS, highestDrainPackageWIFI); 

} 

我的活動在我之前成功更新試圖創建一個新線程來完成服務中的繁重工作。

回答

2

編輯:對不起,我想我早些注意到了,並且跟處理程序一起跟蹤了一邊。

您創建線程t,但您永遠不會運行它。

t.start(); 

您定義的handleMessage()作爲處理的方法,如:

handler = new Handler() { 

    @Override 
    public void handleMessage(Message msg) { 

     //TODO: Handle different types of messages 

    } 
}; 
+0

感謝您的快速響應。我那樣做了,但似乎我沒有收到廣播,或者至少不是我所有主要活動中的廣播。任何理由? –

+0

處理程序不會收到廣播,如果這就是您的想法。如果你有一個BroadcastReceiver,可能會發布如何定義的代碼。 – MikeHelland

+0

我不希望它收到。其實我的處理程序將廣播發送到我的主要活動。我只希望我的處理程序知道何時從新創建的線程收集數據。我在Service中有一些全局變量來保存數據。我在新線程中收集數據,然後在處理程序中訪問它。或者我不能那樣做?我將用代碼 –