2013-02-19 127 views
0

我有一個主要活動,誰收集日誌形成的logcat
服務運行良好,正確的,直到目前它具有廣播結果返回到主活動
的服務結果broadcast(logBuffer)的廣播是廣播,卻從未收到
狀態廣播,它是broadcast(Status.FINISHED)廣播並收到罰款之後。
運行的LogCat日誌顯示日誌已收集並廣播。Android的服務結果廣播沒有被廣播

注:我試圖使用sendOrderedBroadcast(),但問題依然存在。

服務相關代碼:

private void broadcast(Status status) { 
    Log.d(TAG, "+ broadcast(status: " + status + ")"); 
    Intent broadcastIntent = new Intent(MainActivity.StatusReceiver.ACTION_STATUS); 
    broadcastIntent.putExtra("STATUS", status); 
    sendBroadcast(broadcastIntent); 
    Log.d(TAG, "- broadcast()"); 
} 

private void broadcast(ArrayList<String> logs) { 
    Log.d(TAG, "+ broadcast(logs: " + logs + ")"); 
    Intent broadcastIntent = new Intent(MainActivity.StatusReceiver.ACTION_RESULT); 
    broadcastIntent.putExtra("LOGS", logs); 
    sendBroadcast(broadcastIntent); 
    Log.d(TAG, "- broadcast()"); 
} 


@Override 
protected void onHandleIntent(Intent workIntent) { 

    .... some background collection work 

    broadcast(logBuffer); 
    broadcast(Status.FINISHED); 
    Log.d(TAG, "- onHandleIntent()"); 
} 

主要業務相關的代碼:

public class StatusReceiver extends BroadcastReceiver { 

    public static final String ACTION_STATUS = "com.devilstate.auxapp.action.STATUS_CHANGED"; 
    public static final String ACTION_RESULT = "com.devilstate.auxapp.action.RESULTS_READY"; 

    @Override 
    public void onReceive(Context context, Intent intent) { 
     Log.d(TAG, "+ onReceive(context:" + context + ", intent:" + intent + ")"); 
     String action = intent.getAction(); 
     Log.w(TAG, action); 
     if(action.equals(ACTION_STATUS)) { 

      Status status = (Status) intent.getSerializableExtra("STATUS"); 
      switch(status) { 
      case COLLECTING: 
       if(mState == State.INIT || mState == State.WAIT_FOR_RUNNING) { 
        setViewState(State.RUNNING); 
       } 
       break; 
      case FINISHED: 
       if(mState == State.RUNNING || mState == State.WAIT_FOR_DONE) { 
        setViewState(State.DONE); 
       } 
       break; 
      } 

     } else if(action.equals(ACTION_RESULT)) { 

      logBuffer.clear(); 
      logBuffer = (ArrayList<String>) intent.getSerializableExtra("LOGS"); 
     } 
     Log.d(TAG, "- onReceive()"); 
    } 

} 

logcat的相關消息:

02-19 20:30:13.338: D/LogCollectorService(3880): + broadcast(logs: [02-19 20:17:37.229 I/ActivityManager(10160): Displayed com.devilstate.auxapp/.MainActivity: +8s703ms, 02-19 20:17:37.300 W/NetworkManagementSocketTagger(10160): setKernelCountSet(10013, 0) failed with errno -2, 02-19 20:17:44.928 D/MainActivity(2372): + onStartStopButtonClick(v:[email protected]), 02-19 20:17:44.963 D/MainActivity(2372): - onStartStopButtonClick(), 02-19 20:17:44.980 D/LogCollectorService(2372): + onHandleIntent(workIntent: Intent { cmp=com.devilstate.auxapp/.LogCollectorService }), 02-19 20:17:44.980 D/LogCollectorService(2372): + broadcast(status: COLLECTING), 02-19 20:17:45.208 D/MainActivity(2372): + onReceive(context:[email protected], intent:Intent { act=com.devilstate.auxapp.action.STATUS_CHANGED cat=[android.intent.category.DEFAULT] flg=0x10 (has extras) }), 02-19 20:17:45.218 D/LogCollectorService(2372): - broadcast(), 02-19 20:17:46.128 D/dalvikvm(2372): threadid=1: still suspended after undo (sc=1 dc=1), 02-19 20:17:46.368 D/dalvikvm(2372): threadid=1: still suspended after undo (sc=1 dc=1), 02-19 20:17:46.418 D/dalvikvm(2372): GC_CONCURRENT freed 220K, 5% free 6701K/7047K, paused 8ms+8ms, 02-19 20:17:49.863 W/MainActivity(2372): com.devilstate.auxapp.action.STATUS_CHANGED, 02-19 20:17:50.030 D/MainActivity(2372): + setViewState(state:RUNNING), 02-19 20:17:50.038 I/MainActivity(2372): Current State:WAIT_FOR_RUNNING, 02-19 20:17:50.080 I/MainActivity(2372): New State:RUNNING, 02-19 20:17:50.080 D/MainActivity(2372): - setViewState(), 02-19 20:17:50.112 D/MainActivity(2372): - onReceive(), 02-19 20:17:52.989 I/ActivityManager(10160): START {act=android.intent.action.MAIN cat=[android.intent.category.HOME] flg=0x10200000 cmp=com.android.launcher/com.android.launcher2.Launcher} from pid 10160, 02-19 20:17:53.009 W/WindowManager(10160): Failure taking screenshot for (250x135) to layer 21010, 02-19 20:17:53.039 W/NetworkManagementSocketTagger(10160): setKernelCountSet(10013, 1) failed with errno -2, 02-19 20:17:54.309 D/MainActivity(2372): + onStop(), 02-19 20:17:54.440 D/MainActivity(2372): - onStop(), 02-19 20:17:54.509 W/NetworkManagementSocketTagger(10160): setKernelCountSet(10040, 0) failed with errno -2, 02-19 20:17:56.039 I/ActivityManager(10160): START {act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10200000 cmp=com.devilstate.auxapp/.MainActivity bnds=[400,344][496,440]} from pid 10283, 02-19 20:17:56.039 W/WindowManager(10160): Failure taking screenshot for (250x135) to layer 21010, 02-19 20:17:56.089 W/NetworkManagementSocketTagger(10160): setKernelCountSet(10040, 1) failed with errno -2, 02-19 20:17:56.139 D/MainActivity(2372): + onStart(), 02-19 20:17:56.139 D/MainActivity(2372): + updateViewState(), 02-19 20:17:56.179 D/LogCollectorService(2372): + isRunning(), 02-19 20:17:56.179 D/LogCollectorService(2372): - isRunning(): true, 02-19 20:17:56.215 D/MainActivity(2372): + setViewState(state:RUNNING), 02-19 20:17:56.215 I/MainActivity(2372): Current State:RUNNING, 02-19 20:17:56.339 I/MainActivity(2372): New State:RUNNING, 02-19 20:17:56.339 D/MainActivity(2372): - setViewState(), 02-19 20:17:56.349 D/MainActivity(2372): - updateViewState(), 02-19 20:17:56.389 D/MainActivity(2372): - onStart(), 02-19 20:17:56.589 I/Process (10160): Sending signal. PID: 2372 SIG: 3, 02-19 20:17:56.589 I/dalvikvm(2372): threadid=3: reacting to signal 3, 02-19 20:17:56.740 I/dalvikvm(2372): Wrote stack traces to '/data/anr/traces.txt', 02-19 20:17:57.519 W/NetworkManagementSocketTagger(10160): setKernelCountSet(10013, 0) failed with errno -2, 02-19 20:17:58.869 D/MainActivity(2372): + onStartStopButtonClick(v:[email protected]), 02-19 20:17:58.869 D/LogCollectorService(2372): + forceStop(): false, 02-19 20:17:58.899 D/LogCollectorService(2372): - forceStop(): true, 02-19 20:17:58.899 D/MainActivity(2372): - onStartStopButtonClick(), 02-19 20 
02-19 20:30:13.428: D/LogCollectorService(3880): - broadcast() 
02-19 20:30:13.428: D/LogCollectorService(3880): + broadcast(status: FINISHED) 
02-19 20:30:13.438: D/MainActivity(3880): + onReceive(context:[email protected], intent:Intent { act=com.devilstate.auxapp.action.STATUS_CHANGED cat=[android.intent.category.DEFAULT] flg=0x10 (has extras) }) 
02-19 20:30:13.438: W/MainActivity(3880): com.devilstate.auxapp.action.STATUS_CHANGED 
02-19 20:30:13.450: D/MainActivity(3880): + setViewState(state:DONE) 
02-19 20:30:13.450: D/MainActivity(3880): - setViewState() 
02-19 20:30:13.450: D/MainActivity(3880): - onReceive() 
02-19 20:30:13.478: D/LogCollectorService(3880): - broadcast() 
02-19 20:30:13.478: D/LogCollectorService(3880): - onHandleIntent() 

如果它是不明確的,我不知道爲什麼結果沒有收到緩衝區,因爲它應該在MainActivity端

回答

0

發現問題(真的很傻)

我沒有將ACTION_RESULT添加到廣播意圖過濾器。這就是爲什麼接收器不響應broadcast(ACTION_RESULT)。一旦我添加它,一切都很完美

// Register the broadcast receiver 
    IntentFilter filter = new IntentFilter(); 
    filter.addAction(StatusReceiver.ACTION_STATUS); 
    filter.addAction(StatusReceiver.ACTION_RESULT); 
    filter.addCategory(Intent.CATEGORY_DEFAULT); 
    receiver = new StatusReceiver(); 
    registerReceiver(receiver, filter);