What is the correct way for an Activity NOT in the foreground to launch another Activity in the same app?
An app I'm working on can receive asynchronous events in the background. The wa开发者_如何学运维y I've architected the app, when the client receives such events, it sends a Message to a Handler that an Activity owns. The activities handles messages in various ways. In one scenario, when a particular event arrives while the app is not in the foreground, I need to bring up an Activity and some UI to the user. Like how when a call arrives, the Answer or Decline UI comes to the foreground no matter what other app is currently active. What's the best way to do this? I'm re-reading the documentation on Activities, Intents, and Tasks, but it's not always clear or easy to digest. I'm also actively looking for example code or tutorials that do similar things, but no luck there so far. Any pointers or references are welcomed.
You mean like
startActivity( new Intent(this, YourClass.class), Intent.FLAG_ACTIVITY_NEW_TASK);
Edit: ah you don't have the background stuff yet
`
private IntentFilter mNoticeFilter = new IntentFilter("com.you.yourapp.NEW_NOTICE");
private BroadcastReceiver mNoticeReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
// Do something! ie launch your activity
startActivity( new Intent(this, YourClass.class));
}
};`
When you create your activity then you can
registerReceiver(mNoticeReceiver, mNoticeFilter);
and don't forget to unregisterReceiver(mNoticeReceiver);
see here: http://developer.android.com/reference/android/content/BroadcastReceiver.html
Sounds like you need a BroadcastReceiver.
精彩评论