我有一个 Activity(MainActivity) 启动一个服务 (FirstService) 和 FirstService 启动另一个服务 (SsecondService)。场景是 MainActivity 应该等待来自 FirstService 的结果,但这只会在它收到来自 SecondService 的内容时发送结果。问题是FirstService的“onDestroy()”方法在MainActivity得到最终结果之前被调用并注销了SecondService。
>> I/FirstService: On create...
>> I/FirstService: Handling intent...
>> I/SecondService: On create...
>> I/SecondService: On handling intent..
>> I/FirstService: OnDestroy receiver...
>> I/SecondService: Handling result ...
>> I/SecondService: Publishing result ...
在我的活动中:
@Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
if (id == R.id.nav_home) {
// Handle the camera action
} else if (id == R.id.nav_camera) {
} else if (id == R.id.nav_pictures) {
startAction();
}
drawer.closeDrawer(GravityCompat.START);
return true;
}
public void startAction {
Log.i("MainActivity", "Starting FirstService");
Intent intent = new Intent(this, FirstService.class);
intent.setAction(FirstService.ACTION_FETCH_PICTURES);
intent.putExtra(FirstService.EXTRA_ACCOUNT, account);
this.startService(intent);
}
private BroadcastReceiver receiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
Log.i("MainActivity", "Waiting for onReceive");
Bundle bundle = intent.getExtras();
if (bundle != null) {
resultCode = bundle.getInt(FirstService.RESULT);
switch (resultCode) {
case FirstService.RESULT_CODE_OK: [...]
}
[...]
}
}
}
}
};
@Override
protected void onResume() {
super.onResume();
Log.i("MainActivity", "on resume");
registerReceiver(receiver, new IntentFilter(FirstService.NOTIFICATION));
}
@Override
protected void onPause() {
super.onPause();
Log.i("MainActivity", "Unregister receiver...");
unregisterReceiver(receiver);
}
我的第一服务:
@Override
public void onCreate() {
super.onCreate();
Log.i("FirstService", "On create...");
registerReceiver(secondReceiver, new IntentFilter(SecondService.NOTIFICATION_SECOND));
}
@Override
protected void onHandleIntent(Intent intent) {
Log.i("SecondService", "Handling intent...");
if (intent != null) {
final String action = intent.getAction();
// .... some more code
Intent intent = new Intent(this, SecondService.class);
intent.setAction(SecondService.ACTION_FETCH_VALIDITY);
intent.putExtra(SecondService.EXTRA_ACCOUNT, accNumber);
this.startService(intent);
}
}
private void publishResults(int result) {
Log.i("FirstService", "Publishing result...");
Intent intent = new Intent(NOTIFICATION);
intent.putExtra(RESULT, result);
sendBroadcast(intent);
}
public BroadcastReceiver secondReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
Log.i("SecondService", "On receiving result...");
Bundle bundle = intent.getExtras();
if (bundle != null) {
result = bundle.getInt(SecondService.RESULT);
switch (result) {
//[....]
}
publishResults(result);
}
}
};
@Override
public void onDestroy() {
super.onDestroy();
Log.i("SecondService", "OnDestroy receiver...");
unregisterReceiver(secondReceiver);
}
我现在为此苦苦挣扎了几个小时,但找不到解决方案。我做错了什么,我应该怎么做才能让它发挥作用?任何想法都受到欢迎。干杯!