开始和稍后仅刷新服务活动

时间:2012-09-28 10:29:46

标签: android service android-activity

我的服务目前收听电话,然后通过startActivity启动活动:

Intent intent = new Intent(context, CallMonitor.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
intent.putExtra("call", call);
startActivity(intent);

因此,当服务接到更多电话时,将开始更多活动。

但我希望该服务只启动一项活动。下次它应该将调用发送到已经启动的活动。活动会更新。

最好的方法是什么?

Alexander Miehlke,柏林

2 个答案:

答案 0 :(得分:2)

你应该使用 intent.addFlags(intent.FLAG_ACTIVITY_REORDER_TO_FRONT)来调用已经开始的活动并将它带回到前面。在该活动中,您应该重写onResume()方法。并且为了根据您传递的意图更新该活动,您应该重写onNewIntent()方法以设置新接收的意图。例如。

@Override
protected void onNewIntent(Intent intent) {
    super.onNewIntent(intent);
    setIntent(intent);
}

@Override
protected void onResume() {
    super.onResume();
    callString=getIntent().getExtras().getString("call");
    //your code
}

希望这会有所帮助。

答案 1 :(得分:0)

经过数小时的测试,解决方案只是AndroidManifest.xml中的一行:

<activity
    ...
    android:launchMode="singleTask"
    ....
</activity>