• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2007 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.example.android.apis.app;
18 
19 import java.util.Random;
20 
21 import com.example.android.apis.R;
22 
23 import android.app.Activity;
24 import android.app.Notification;
25 import android.app.NotificationManager;
26 import android.app.PendingIntent;
27 import android.content.ComponentName;
28 import android.content.Context;
29 import android.content.Intent;
30 import android.os.Bundle;
31 import android.view.View;
32 import android.widget.Button;
33 
34 /**
35  * UI for posting an example notification.
36  */
37 public class IncomingMessage extends Activity {
38     @Override
onCreate(Bundle savedInstanceState)39     protected void onCreate(Bundle savedInstanceState) {
40         super.onCreate(savedInstanceState);
41 
42         setContentView(R.layout.incoming_message);
43 
44         Button button = (Button) findViewById(R.id.notify_app);
45         button.setOnClickListener(new Button.OnClickListener() {
46                 public void onClick(View v) {
47                     showAppNotification();
48                 }
49             });
50 
51         button = (Button) findViewById(R.id.notify_interstitial);
52         button.setOnClickListener(new Button.OnClickListener() {
53                 public void onClick(View v) {
54                     showInterstitialNotification();
55                 }
56             });
57     }
58 
59 //BEGIN_INCLUDE(app_notification)
60 //BEGIN_INCLUDE(intent_array)
61     /**
62      * This method creates an array of Intent objects representing the
63      * activity stack for the incoming message details state that the
64      * application should be in when launching it from a notification.
65      */
makeMessageIntentStack(Context context, CharSequence from, CharSequence msg)66     static Intent[] makeMessageIntentStack(Context context, CharSequence from,
67             CharSequence msg) {
68         // A typical convention for notifications is to launch the user deeply
69         // into an application representing the data in the notification; to
70         // accomplish this, we can build an array of intents to insert the back
71         // stack stack history above the item being displayed.
72         Intent[] intents = new Intent[4];
73 
74         // First: root activity of ApiDemos.
75         // This is a convenient way to make the proper Intent to launch and
76         // reset an application's task.
77         intents[0] = Intent.makeRestartActivityTask(new ComponentName(context,
78                 com.example.android.apis.ApiDemos.class));
79 
80         // "App"
81         intents[1] = new Intent(context, com.example.android.apis.ApiDemos.class);
82         intents[1].putExtra("com.example.android.apis.Path", "App");
83         // "App/Notification"
84         intents[2] = new Intent(context, com.example.android.apis.ApiDemos.class);
85         intents[2].putExtra("com.example.android.apis.Path", "App/Notification");
86 
87         // Now the activity to display to the user.  Also fill in the data it
88         // should display.
89         intents[3] = new Intent(context, IncomingMessageView.class);
90         intents[3].putExtra(IncomingMessageView.KEY_FROM, from);
91         intents[3].putExtra(IncomingMessageView.KEY_MESSAGE, msg);
92 
93         return intents;
94     }
95 //END_INCLUDE(intent_array)
96 
97     /**
98      * The notification is the icon and associated expanded entry in the
99      * status bar.
100      */
showAppNotification()101     void showAppNotification() {
102         // look up the notification manager service
103         NotificationManager nm = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
104 
105         // The details of our fake message
106         CharSequence from = "Joe";
107         CharSequence message;
108         switch ((new Random().nextInt()) % 3) {
109             case 0: message = "r u hungry?  i am starved"; break;
110             case 1: message = "im nearby u"; break;
111             default: message = "kthx. meet u for dinner. cul8r"; break;
112         }
113 
114         // The PendingIntent to launch our activity if the user selects this
115         // notification.  Note the use of FLAG_CANCEL_CURRENT so that, if there
116         // is already an active matching pending intent, cancel it and replace
117         // it with the new array of Intents.
118         PendingIntent contentIntent = PendingIntent.getActivities(this, 0,
119                 makeMessageIntentStack(this, from, message), PendingIntent.FLAG_CANCEL_CURRENT);
120 
121         // The ticker text, this uses a formatted string so our message could be localized
122         String tickerText = getString(R.string.imcoming_message_ticker_text, message);
123 
124         // construct the Notification object.
125         Notification notif = new Notification(R.drawable.stat_sample, tickerText,
126                 System.currentTimeMillis());
127 
128         // Set the info for the views that show in the notification panel.
129         notif.setLatestEventInfo(this, from, message, contentIntent);
130 
131         // We'll have this notification do the default sound, vibration, and led.
132         // Note that if you want any of these behaviors, you should always have
133         // a preference for the user to turn them off.
134         notif.defaults = Notification.DEFAULT_ALL;
135 
136         // Note that we use R.layout.incoming_message_panel as the ID for
137         // the notification.  It could be any integer you want, but we use
138         // the convention of using a resource id for a string related to
139         // the notification.  It will always be a unique number within your
140         // application.
141         nm.notify(R.string.imcoming_message_ticker_text, notif);
142     }
143 //END_INCLUDE(app_notification)
144 
145 //BEGIN_INCLUDE(interstitial_notification)
146     /**
147      * The notification is the icon and associated expanded entry in the
148      * status bar.
149      */
showInterstitialNotification()150     void showInterstitialNotification() {
151         // look up the notification manager service
152         NotificationManager nm = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
153 
154         // The details of our fake message
155         CharSequence from = "Dianne";
156         CharSequence message;
157         switch ((new Random().nextInt()) % 3) {
158             case 0: message = "i am ready for some dinner"; break;
159             case 1: message = "how about thai down the block?"; break;
160             default: message = "meet u soon. dont b late!"; break;
161         }
162 
163         // The PendingIntent to launch our activity if the user selects this
164         // notification.  Note the use of FLAG_CANCEL_CURRENT so that, if there
165         // is already an active matching pending intent, cancel it and replace
166         // it with the new Intent.
167         Intent intent = new Intent(this, IncomingMessageInterstitial.class);
168         intent.putExtra(IncomingMessageView.KEY_FROM, from);
169         intent.putExtra(IncomingMessageView.KEY_MESSAGE, message);
170         intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
171         PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
172                 intent, PendingIntent.FLAG_CANCEL_CURRENT);
173 
174         // The ticker text, this uses a formatted string so our message could be localized
175         String tickerText = getString(R.string.imcoming_message_ticker_text, message);
176 
177         // construct the Notification object.
178         Notification notif = new Notification(R.drawable.stat_sample, tickerText,
179                 System.currentTimeMillis());
180 
181         // Set the info for the views that show in the notification panel.
182         notif.setLatestEventInfo(this, from, message, contentIntent);
183 
184         // We'll have this notification do the default sound, vibration, and led.
185         // Note that if you want any of these behaviors, you should always have
186         // a preference for the user to turn them off.
187         notif.defaults = Notification.DEFAULT_ALL;
188 
189         // Note that we use R.layout.incoming_message_panel as the ID for
190         // the notification.  It could be any integer you want, but we use
191         // the convention of using a resource id for a string related to
192         // the notification.  It will always be a unique number within your
193         // application.
194         nm.notify(R.string.imcoming_message_ticker_text, notif);
195     }
196 //END_INCLUDE(interstitial_notification)
197 }
198