How to approximate PendingIntent getActivities in Api level 7 (Android 2.1)?
I wish to have an app that targets Android 2.1, API level 7, launch multiple activities at once when a user clicks on a C2DM notification that has come in. This is the method I currently use to launch my activity:
public static PendingIntent getActivity (Context context, int requestCode, Intent intent, int flags)
This method only allows me to put one activity on the stack. What I really want to do is use this method:
public static PendingIntent getActivities (Context context, int requestCode, Intent[] intents, int flags)
This method reports that it is only available for API level 11, which is Android 3.0. I do not wish to break backward compatibility with 2.1. Can anyone suggest how I might be able to ac开发者_StackOverflow中文版hieve this effect without taking a dependency on Android 3.0? I tried looking for the source to this new method, but it does not appear to be available yet.
What you do is have a separate activity that is the target of the alarm, and build the intent stack from there, as below. This could probably be generalised into something very like 'getactivities' quite easily - it's a pity it isn't in the compat libraries.
public class AlarmActivity extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent = new Intent(this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
intent = new Intent(this, ChildActivity.class);
startActivity(intent);
finish();
}
}
As MisterSquonk says in the comments, only one Activity can be active at any one time (even in 3.0), so launching "multiple activities at once" is not going to be possible. Even if it were, what will the user experience be like with multiple activities starting in quick succession, and no guarantee of which will be launched last, and so be the one left for the user to interact with.
I suspect that you actually want to wake up different parts of your app simultaneously without each one having its own UI. If so, then I would suggest having one or more Services which implement multiple BroadcastReceivers against a common Intent filter. When you fire a Broadcast of that event, then multiple things will get woken up at once.
精彩评论