diff --git a/samples/ApiDemos/AndroidManifest.xml b/samples/ApiDemos/AndroidManifest.xml index 48ced9f3a..0d8041059 100644 --- a/samples/ApiDemos/AndroidManifest.xml +++ b/samples/ApiDemos/AndroidManifest.xml @@ -341,6 +341,14 @@ + + + + + + + @@ -474,12 +482,16 @@ - + + + diff --git a/samples/ApiDemos/res/layout/intent_activity_flags.xml b/samples/ApiDemos/res/layout/intent_activity_flags.xml new file mode 100644 index 000000000..610e2dd40 --- /dev/null +++ b/samples/ApiDemos/res/layout/intent_activity_flags.xml @@ -0,0 +1,43 @@ + + + + + + + + + + + + + + diff --git a/samples/ApiDemos/res/values/strings.xml b/samples/ApiDemos/res/values/strings.xml index eb508349a..b76189a9c 100644 --- a/samples/ApiDemos/res/values/strings.xml +++ b/samples/ApiDemos/res/values/strings.xml @@ -320,10 +320,19 @@ - App/Intents + App/Activity/Intents Example of launching various Intents. Get Music + + + + + App/Activity/Intent Activity Flags + Example of the use of various intent activity flags. + FLAG_ACTIVITY_CLEAR_TASK + FLAG_ACTIVITY_CLEAR_TASK (PI) + diff --git a/samples/ApiDemos/src/com/example/android/apis/app/IncomingMessage.java b/samples/ApiDemos/src/com/example/android/apis/app/IncomingMessage.java index 03fef4aae..41d2ea38a 100644 --- a/samples/ApiDemos/src/com/example/android/apis/app/IncomingMessage.java +++ b/samples/ApiDemos/src/com/example/android/apis/app/IncomingMessage.java @@ -65,8 +65,10 @@ public class IncomingMessage extends Activity { CharSequence message = "kthx. meet u for dinner. cul8r"; // The PendingIntent to launch our activity if the user selects this notification +//BEGIN_INCLUDE(pending_intent) PendingIntent contentIntent = PendingIntent.getActivity(this, 0, new Intent(this, IncomingMessageView.class), 0); +//END_INCLUDE(pending_intent) // The ticker text, this uses a formatted string so our message could be localized String tickerText = getString(R.string.imcoming_message_ticker_text, message); diff --git a/samples/ApiDemos/src/com/example/android/apis/app/IntentActivityFlags.java b/samples/ApiDemos/src/com/example/android/apis/app/IntentActivityFlags.java new file mode 100644 index 000000000..e93ea4317 --- /dev/null +++ b/samples/ApiDemos/src/com/example/android/apis/app/IntentActivityFlags.java @@ -0,0 +1,90 @@ +package com.example.android.apis.app; + +import com.example.android.apis.R; + +import android.app.Activity; +import android.app.PendingIntent; +import android.app.PendingIntent.CanceledException; +import android.content.Context; +import android.content.Intent; +import android.os.Bundle; +import android.util.Log; +import android.view.View; +import android.view.View.OnClickListener; +import android.widget.Button; + +/** + * Example of various Intent flags to modify the activity stack. + */ +public class IntentActivityFlags extends Activity { + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + + setContentView(R.layout.intent_activity_flags); + + // Watch for button clicks. + Button button = (Button)findViewById(R.id.flag_activity_clear_task); + button.setOnClickListener(mFlagActivityClearTaskListener); + button = (Button)findViewById(R.id.flag_activity_clear_task_pi); + button.setOnClickListener(mFlagActivityClearTaskPIListener); + } + + /** + * This creates an array of Intent objects representing the back stack + * for a user going into the Views/Lists API demos. + */ +//BEGIN_INCLUDE(intent_array) + private Intent[] buildIntentsToViewsLists() { + // We will use FLAG_ACTIVITY_CLEAR_TASK to complete replace our + // current task with a new Intent. + Intent[] intents = new Intent[3]; + + // The main activity started from launcher is action MAIN and + // category LAUNCHER; we want to match that. + Intent intent = new Intent(Intent.ACTION_MAIN); + intent.addCategory(Intent.CATEGORY_LAUNCHER); + // We will use FLAG_ACTIVITY_CLEAR_TASK to completely replace our + // current task with a new Intent. + intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK + | Intent.FLAG_ACTIVITY_CLEAR_TASK); + + // Set the actual activity to launch. + intent.setClass(IntentActivityFlags.this, com.example.android.apis.ApiDemos.class); + intents[0] = intent; + + intent = new Intent(Intent.ACTION_MAIN); + intent.setClass(IntentActivityFlags.this, com.example.android.apis.ApiDemos.class); + intent.putExtra("com.example.android.apis.Path", "Views"); + intents[1] = intent; + + intent = new Intent(Intent.ACTION_MAIN); + intent.setClass(IntentActivityFlags.this, com.example.android.apis.ApiDemos.class); + intent.putExtra("com.example.android.apis.Path", "Views/Lists"); + + intents[2] = intent; + return intents; + } +//END_INCLUDE(intent_array) + + private OnClickListener mFlagActivityClearTaskListener = new OnClickListener() { + public void onClick(View v) { + startActivities(buildIntentsToViewsLists()); + } + }; + + private OnClickListener mFlagActivityClearTaskPIListener = new OnClickListener() { + public void onClick(View v) { + Context context = IntentActivityFlags.this; +//BEGIN_INCLUDE(pending_intent) + PendingIntent pi = PendingIntent.getActivities(context, 0, + buildIntentsToViewsLists(), PendingIntent.FLAG_UPDATE_CURRENT); +//END_INCLUDE(pending_intent) + try { + pi.send(); + } catch (CanceledException e) { + Log.w("IntentActivityFlags", "Failed sending PendingIntent", e); + } + } + }; +} diff --git a/samples/ApiDemos/src/com/example/android/apis/app/StatusBarNotifications.java b/samples/ApiDemos/src/com/example/android/apis/app/StatusBarNotifications.java index 9b911cca5..b506012cd 100644 --- a/samples/ApiDemos/src/com/example/android/apis/app/StatusBarNotifications.java +++ b/samples/ApiDemos/src/com/example/android/apis/app/StatusBarNotifications.java @@ -148,15 +148,52 @@ public class StatusBarNotifications extends Activity { // The PendingIntent to launch our activity if the user selects this // notification. Note the use of FLAG_UPDATE_CURRENT so that if there // is already an active matching pending intent, we will update its - // extras to be the ones passed in here. + // extras (and other Intents in the array) to be the ones passed in here. PendingIntent contentIntent = PendingIntent.getActivity(this, 0, - new Intent(this, NotificationDisplay.class) - .setFlags(Intent.FLAG_ACTIVITY_NEW_TASK) - .putExtra("moodimg", moodId), + new Intent(this, NotificationDisplay.class).putExtra("moodimg", moodId), PendingIntent.FLAG_UPDATE_CURRENT); return contentIntent; } +//BEGIN_INCLUDE(intent_array) + private PendingIntent makeDefaultIntent() { + // A typical convention for notifications is to launch the user deeply + // into an application representing the data in the notification; to + // accomplish this, we can build an array of intents to insert the back + // stack stack history above the item being displayed. + Intent[] intents = new Intent[4]; + + // First: root activity of ApiDemos. + // The main activity started from launcher is action MAIN and + // category LAUNCHER; we want to match that. + intents[0] = new Intent(Intent.ACTION_MAIN); + intents[0].addCategory(Intent.CATEGORY_LAUNCHER); + // We will use FLAG_ACTIVITY_CLEAR_TASK to completely replace our + // current task with a new Intent. + intents[0].addFlags(Intent.FLAG_ACTIVITY_NEW_TASK + | Intent.FLAG_ACTIVITY_CLEAR_TASK); + intents[0].setClass(this, com.example.android.apis.ApiDemos.class); + + // "App" + intents[1] = new Intent(this, com.example.android.apis.ApiDemos.class); + intents[1].putExtra("com.example.android.apis.Path", "App"); + // "App/Notification" + intents[2] = new Intent(this, com.example.android.apis.ApiDemos.class); + intents[2].putExtra("com.example.android.apis.Path", "App/Notification"); + + // Now the activity to display to the user. + intents[3] = new Intent(this, StatusBarNotifications.class); + + // The PendingIntent to launch our activity if the user selects this + // notification. Note the use of FLAG_UPDATE_CURRENT so that if there + // is already an active matching pending intent, we will update its + // extras (and other Intents in the array) to be the ones passed in here. + PendingIntent contentIntent = PendingIntent.getActivities(this, 0, + intents, PendingIntent.FLAG_UPDATE_CURRENT); + return contentIntent; + } +//END_INCLUDE(intent_array) + private void setMood(int moodId, int textId, boolean showTicker) { // In this sample, we'll use the same text for the ticker and the expanded notification CharSequence text = getText(textId); @@ -210,8 +247,7 @@ public class StatusBarNotifications extends Activity { // This method sets the defaults on the notification before posting it. // This is who should be launched if the user selects our notification. - PendingIntent contentIntent = PendingIntent.getActivity(this, 0, - new Intent(this, StatusBarNotifications.class), 0); + PendingIntent contentIntent = makeDefaultIntent(); // In this sample, we'll use the same text for the ticker and the expanded notification CharSequence text = getText(R.string.status_bar_notifications_happy_message);