diff --git a/samples/ApiDemos/AndroidManifest.xml b/samples/ApiDemos/AndroidManifest.xml
index 4e63dde0e..9cd9d9583 100644
--- a/samples/ApiDemos/AndroidManifest.xml
+++ b/samples/ApiDemos/AndroidManifest.xml
@@ -615,24 +615,27 @@
-
+ android:label="App/Notification/IncomingMessage">
-
-
-
-
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
diff --git a/samples/ApiDemos/res/layout/incoming_message_view.xml b/samples/ApiDemos/res/layout/incoming_message_view.xml
index b2daf4ec2..a40807ca7 100644
--- a/samples/ApiDemos/res/layout/incoming_message_view.xml
+++ b/samples/ApiDemos/res/layout/incoming_message_view.xml
@@ -23,6 +23,15 @@
android:paddingTop="4dip"
>
+
+
-
+ >
+
+
+
diff --git a/samples/ApiDemos/res/values-v11/styles.xml b/samples/ApiDemos/res/values-v11/styles.xml
index 04c6f3f72..3b4fead01 100644
--- a/samples/ApiDemos/res/values-v11/styles.xml
+++ b/samples/ApiDemos/res/values-v11/styles.xml
@@ -19,6 +19,10 @@
+
+
+
diff --git a/samples/ApiDemos/res/values/strings.xml b/samples/ApiDemos/res/values/strings.xml
index 2913844be..99e24635d 100644
--- a/samples/ApiDemos/res/values/strings.xml
+++ b/samples/ApiDemos/res/values/strings.xml
@@ -1195,11 +1195,9 @@
- Show Notification
-
this is the text of a previous message.\nkthx. meet u for dinner. cul8r
- this is the text of a previous message.\nkthx. meet u for dinner. cul8r
+ This is the text of the posted notification.Did you notice that the status bar icon disappeared?New text message: %0$s
diff --git a/samples/ApiDemos/res/values/styles.xml b/samples/ApiDemos/res/values/styles.xml
index 10dcf35eb..49a1c25a5 100644
--- a/samples/ApiDemos/res/values/styles.xml
+++ b/samples/ApiDemos/res/values/styles.xml
@@ -30,6 +30,13 @@
+
+
+
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 41d2ea38a..63a254f7f 100644
--- a/samples/ApiDemos/src/com/example/android/apis/app/IncomingMessage.java
+++ b/samples/ApiDemos/src/com/example/android/apis/app/IncomingMessage.java
@@ -16,22 +16,24 @@
package com.example.android.apis.app;
+import java.util.Random;
+
import com.example.android.apis.R;
import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
+import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
-import android.graphics.Bitmap;
-import android.graphics.BitmapFactory;
import android.os.Bundle;
-import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
-import android.widget.TextView;
+/**
+ * UI for posting an example notification.
+ */
public class IncomingMessage extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
@@ -39,36 +41,82 @@ public class IncomingMessage extends Activity {
setContentView(R.layout.incoming_message);
- Button button = (Button) findViewById(R.id.notify);
+ Button button = (Button) findViewById(R.id.notify_app);
button.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
- showNotification();
+ showAppNotification();
+ }
+ });
+
+ button = (Button) findViewById(R.id.notify_interstitial);
+ button.setOnClickListener(new Button.OnClickListener() {
+ public void onClick(View v) {
+ showInterstitialNotification();
}
});
}
- private View inflateView(int resource) {
- LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
- return vi.inflate(resource, null);
+//BEGIN_INCLUDE(app_notification)
+//BEGIN_INCLUDE(intent_array)
+ /**
+ * This method creates an array of Intent objects representing the
+ * activity stack for the incoming message details state that the
+ * application should be in when launching it from a notification.
+ */
+ static Intent[] makeMessageIntentStack(Context context, CharSequence from,
+ CharSequence msg) {
+ // 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.
+ // This is a convenient way to make the proper Intent to launch and
+ // reset an application's task.
+ intents[0] = Intent.makeRestartActivityTask(new ComponentName(context,
+ com.example.android.apis.ApiDemos.class));
+
+ // "App"
+ intents[1] = new Intent(context, com.example.android.apis.ApiDemos.class);
+ intents[1].putExtra("com.example.android.apis.Path", "App");
+ // "App/Notification"
+ intents[2] = new Intent(context, com.example.android.apis.ApiDemos.class);
+ intents[2].putExtra("com.example.android.apis.Path", "App/Notification");
+
+ // Now the activity to display to the user. Also fill in the data it
+ // should display.
+ intents[3] = new Intent(context, IncomingMessageView.class);
+ intents[3].putExtra(IncomingMessageView.KEY_FROM, from);
+ intents[3].putExtra(IncomingMessageView.KEY_MESSAGE, msg);
+
+ return intents;
}
+//END_INCLUDE(intent_array)
/**
* The notification is the icon and associated expanded entry in the
* status bar.
*/
- protected void showNotification() {
+ void showAppNotification() {
// look up the notification manager service
NotificationManager nm = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
// The details of our fake message
CharSequence from = "Joe";
- CharSequence message = "kthx. meet u for dinner. cul8r";
+ CharSequence message;
+ switch ((new Random().nextInt()) % 3) {
+ case 0: message = "r u hungry? i am starved"; break;
+ case 1: message = "im nearby u"; break;
+ default: message = "kthx. meet u for dinner. cul8r"; break;
+ }
- // 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 PendingIntent to launch our activity if the user selects this
+ // notification. Note the use of FLAG_CANCEL_CURRENT so that, if there
+ // is already an active matching pending intent, cancel it and replace
+ // it with the new array of Intents.
+ PendingIntent contentIntent = PendingIntent.getActivities(this, 0,
+ makeMessageIntentStack(this, from, message), PendingIntent.FLAG_CANCEL_CURRENT);
// The ticker text, this uses a formatted string so our message could be localized
String tickerText = getString(R.string.imcoming_message_ticker_text, message);
@@ -80,20 +128,10 @@ public class IncomingMessage extends Activity {
// Set the info for the views that show in the notification panel.
notif.setLatestEventInfo(this, from, message, contentIntent);
- /*
- // On tablets, the ticker shows the sender, the first line of the message,
- // the photo of the person and the app icon. For our sample, we just show
- // the same icon twice. If there is no sender, just pass an array of 1 Bitmap.
- notif.tickerTitle = from;
- notif.tickerSubtitle = message;
- notif.tickerIcons = new Bitmap[2];
- notif.tickerIcons[0] = getIconBitmap();;
- notif.tickerIcons[1] = getIconBitmap();;
- */
-
- // after a 0ms delay, vibrate for 250ms, pause for 100 ms and
- // then vibrate for 500ms.
- notif.vibrate = new long[] { 0, 250, 100, 500};
+ // We'll have this notification do the default sound, vibration, and led.
+ // Note that if you want any of these behaviors, you should always have
+ // a preference for the user to turn them off.
+ notif.defaults = Notification.DEFAULT_ALL;
// Note that we use R.layout.incoming_message_panel as the ID for
// the notification. It could be any integer you want, but we use
@@ -102,10 +140,58 @@ public class IncomingMessage extends Activity {
// application.
nm.notify(R.string.imcoming_message_ticker_text, notif);
}
+//END_INCLUDE(app_notification)
- private Bitmap getIconBitmap() {
- BitmapFactory f = new BitmapFactory();
- return f.decodeResource(getResources(), R.drawable.app_sample_code);
+//BEGIN_INCLUDE(interstitial_notification)
+ /**
+ * The notification is the icon and associated expanded entry in the
+ * status bar.
+ */
+ void showInterstitialNotification() {
+ // look up the notification manager service
+ NotificationManager nm = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
+
+ // The details of our fake message
+ CharSequence from = "Dianne";
+ CharSequence message;
+ switch ((new Random().nextInt()) % 3) {
+ case 0: message = "i am ready for some dinner"; break;
+ case 1: message = "how about thai down the block?"; break;
+ default: message = "meet u soon. dont b late!"; break;
+ }
+
+ // The PendingIntent to launch our activity if the user selects this
+ // notification. Note the use of FLAG_CANCEL_CURRENT so that, if there
+ // is already an active matching pending intent, cancel it and replace
+ // it with the new Intent.
+ Intent intent = new Intent(this, IncomingMessageInterstitial.class);
+ intent.putExtra(IncomingMessageView.KEY_FROM, from);
+ intent.putExtra(IncomingMessageView.KEY_MESSAGE, message);
+ intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
+ PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
+ intent, PendingIntent.FLAG_CANCEL_CURRENT);
+
+ // The ticker text, this uses a formatted string so our message could be localized
+ String tickerText = getString(R.string.imcoming_message_ticker_text, message);
+
+ // construct the Notification object.
+ Notification notif = new Notification(R.drawable.stat_sample, tickerText,
+ System.currentTimeMillis());
+
+ // Set the info for the views that show in the notification panel.
+ notif.setLatestEventInfo(this, from, message, contentIntent);
+
+ // We'll have this notification do the default sound, vibration, and led.
+ // Note that if you want any of these behaviors, you should always have
+ // a preference for the user to turn them off.
+ notif.defaults = Notification.DEFAULT_ALL;
+
+ // Note that we use R.layout.incoming_message_panel as the ID for
+ // the notification. It could be any integer you want, but we use
+ // the convention of using a resource id for a string related to
+ // the notification. It will always be a unique number within your
+ // application.
+ nm.notify(R.string.imcoming_message_ticker_text, notif);
}
+//END_INCLUDE(interstitial_notification)
}
-
diff --git a/samples/ApiDemos/src/com/example/android/apis/app/IncomingMessageInterstitial.java b/samples/ApiDemos/src/com/example/android/apis/app/IncomingMessageInterstitial.java
new file mode 100644
index 000000000..f9bbd7b02
--- /dev/null
+++ b/samples/ApiDemos/src/com/example/android/apis/app/IncomingMessageInterstitial.java
@@ -0,0 +1,64 @@
+/*
+ * Copyright (C) 2012 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.example.android.apis.app;
+
+import com.example.android.apis.R;
+
+import android.app.Activity;
+import android.app.NotificationManager;
+import android.content.Intent;
+import android.os.Bundle;
+import android.view.View;
+import android.widget.Button;
+
+/**
+ * This is an activity that provides an interstitial UI for the notification
+ * that is posted by {@link IncomingMessage}. It allows the user to switch
+ * to the app in its appropriate state if they want.
+ */
+public class IncomingMessageInterstitial extends Activity {
+ @Override
+ protected void onCreate(Bundle savedInstanceState) {
+ super.onCreate(savedInstanceState);
+
+ setContentView(R.layout.incoming_message_interstitial);
+
+ Button button = (Button) findViewById(R.id.notify_app);
+ button.setOnClickListener(new Button.OnClickListener() {
+ public void onClick(View v) {
+ switchToApp();
+ }
+ });
+ }
+
+//BEGIN_INCLUDE(app_launch)
+ /**
+ * Perform a switch to the app. A new activity stack is started, replacing
+ * whatever is currently running, and this activity is finished.
+ */
+ void switchToApp() {
+ // We will launch the app showing what the user picked. In this simple
+ // example, it is just what the notification gave us.
+ CharSequence from = getIntent().getCharSequenceExtra(IncomingMessageView.KEY_FROM);
+ CharSequence msg = getIntent().getCharSequenceExtra(IncomingMessageView.KEY_MESSAGE);
+ // Build the new activity stack, launch it, and finish this UI.
+ Intent[] stack = IncomingMessage.makeMessageIntentStack(this, from, msg);
+ startActivities(stack);
+ finish();
+ }
+//END_INCLUDE(app_launch)
+}
diff --git a/samples/ApiDemos/src/com/example/android/apis/app/IncomingMessageView.java b/samples/ApiDemos/src/com/example/android/apis/app/IncomingMessageView.java
index 13ea3dae1..144184da8 100644
--- a/samples/ApiDemos/src/com/example/android/apis/app/IncomingMessageView.java
+++ b/samples/ApiDemos/src/com/example/android/apis/app/IncomingMessageView.java
@@ -21,6 +21,7 @@ import com.example.android.apis.R;
import android.app.Activity;
import android.app.NotificationManager;
import android.os.Bundle;
+import android.widget.TextView;
/**
* This activity is run as the click activity for {@link IncomingMessage}.
@@ -28,11 +29,26 @@ import android.os.Bundle;
* has been "read."
*/
public class IncomingMessageView extends Activity {
+ /**
+ * Extra that can be supplied to Intent: who the message is from.
+ */
+ static final public String KEY_FROM = "from";
+ /**
+ * Extra that can be supplied to Intent: the message that was sent.
+ */
+ static final public String KEY_MESSAGE = "message";
+
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.incoming_message_view);
+ // Fill in the message content.
+ ((TextView)findViewById(R.id.from)).setText(
+ getIntent().getCharSequenceExtra(KEY_FROM));
+ ((TextView)findViewById(R.id.message)).setText(
+ getIntent().getCharSequenceExtra(KEY_MESSAGE));
+
// look up the notification manager service
NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);