我有一个主要活动和一个从 LogCat 收集日志
的服务 该服务运行良好,直到它必须将结果广播回主要活动广播
结果但从未收到过
状态广播,就在之后它被广播和接收良好。
运行的 LogCat 日志显示日志已被收集和广播。broadcast(logBuffer)
broadcast(Status.FINISHED)
注意:我尝试使用 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:android.widget.Button@41035cf8), 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:com.devilstate.auxapp.MainActivity@41025408, 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:android.widget.Button@41035cf8), 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:com.devilstate.auxapp.MainActivity@41027a70, 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 端没有收到结果缓冲区