diff options
12 files changed, 133 insertions, 112 deletions
diff --git a/samples/Alarm/AndroidManifest.xml b/samples/Alarm/AndroidManifest.xml index 6a7a831a8..224256771 100644 --- a/samples/Alarm/AndroidManifest.xml +++ b/samples/Alarm/AndroidManifest.xml @@ -29,6 +29,7 @@ <!-- Declares the application, its icon, and its visible label --> + <uses-sdk android:minSdkVersion="11" /> <application android:icon="@drawable/icon" android:label="@string/app_name"> diff --git a/samples/Alarm/src/com/example/android/newalarm/AlarmService.java b/samples/Alarm/src/com/example/android/newalarm/AlarmService.java index 1f882069d..ec3163cb8 100644 --- a/samples/Alarm/src/com/example/android/newalarm/AlarmService.java +++ b/samples/Alarm/src/com/example/android/newalarm/AlarmService.java @@ -181,13 +181,6 @@ public class AlarmService extends Service { // Sets the text to use for the status bar and status list views. CharSequence notificationText = getText(R.string.alarm_service_started); - // Sets the icon, status bar text, and display time for the mNotification. - mNotification = new Notification( - R.drawable.stat_sample, // the status icon - notificationText, // the status text - System.currentTimeMillis() // the time stamp - ); - // Sets up the Intent that starts AlarmActivity mContentIntent = PendingIntent.getActivity( this, // Start the Activity in the current context @@ -196,14 +189,15 @@ public class AlarmService extends Service { 0 // Use an existing activity instance if available ); - // Creates a new content view for the mNotification. The view appears when the user - // shows the expanded status window. - mNotification.setLatestEventInfo( - this, // Put the content view in the current context - getText(R.string.alarm_service_label), // The text to use as the label of the entry - notificationText, // The text to use as the contents of the entry - mContentIntent // The intent to send when the entry is clicked - ); + // Build the notification object. + mNotification = new Notification.Builder(this) // The builder requires the context + .setSmallIcon(R.drawable.stat_sample) // the status icon + .setTicker(notificationText) // the status text + .setWhen(System.currentTimeMillis()) // the time stamp + .setContentTitle(getText(R.string.alarm_service_label)) // the label of the entry + .setContentText(notificationText) // the contents of the entry + .setContentIntent(mContentIntent) // The intent to send when the entry is clicked + .build(); // Sets a unique ID for the notification and sends it to NotificationManager to be // displayed. The ID is the integer marker for the notification string, which is diff --git a/samples/ApiDemos/src/com/example/android/apis/app/AlarmService_Service.java b/samples/ApiDemos/src/com/example/android/apis/app/AlarmService_Service.java index 6762ba9fe..cede9dced 100644 --- a/samples/ApiDemos/src/com/example/android/apis/app/AlarmService_Service.java +++ b/samples/ApiDemos/src/com/example/android/apis/app/AlarmService_Service.java @@ -99,17 +99,19 @@ public class AlarmService_Service extends Service { // In this sample, we'll use the same text for the ticker and the expanded notification CharSequence text = getText(R.string.alarm_service_started); - // Set the icon, scrolling text and timestamp - Notification notification = new Notification(R.drawable.stat_sample, text, - System.currentTimeMillis()); - // The PendingIntent to launch our activity if the user selects this notification PendingIntent contentIntent = PendingIntent.getActivity(this, 0, new Intent(this, AlarmService.class), 0); // Set the info for the views that show in the notification panel. - notification.setLatestEventInfo(this, getText(R.string.alarm_service_label), - text, contentIntent); + Notification notification = new Notification.Builder(this) + .setSmallIcon(R.drawable.stat_sample) // the status icon + .setTicker(text) // the status text + .setWhen(System.currentTimeMillis()) // the time stamp + .setContentTitle(getText(R.string.alarm_service_label)) // the label of the entry + .setContentText(text) // the contents of the entry + .setContentIntent(contentIntent) // The intent to send when the entry is clicked + .build(); // Send the notification. // We use a layout id because it is a unique number. We use it later to cancel. diff --git a/samples/ApiDemos/src/com/example/android/apis/app/ForegroundService.java b/samples/ApiDemos/src/com/example/android/apis/app/ForegroundService.java index 123e369c9..19e41d838 100644 --- a/samples/ApiDemos/src/com/example/android/apis/app/ForegroundService.java +++ b/samples/ApiDemos/src/com/example/android/apis/app/ForegroundService.java @@ -166,18 +166,19 @@ public class ForegroundService extends Service { // In this sample, we'll use the same text for the ticker and the expanded notification CharSequence text = getText(R.string.foreground_service_started); - // Set the icon, scrolling text and timestamp - Notification notification = new Notification(R.drawable.stat_sample, text, - System.currentTimeMillis()); - - // The PendingIntent to launch our activity if the user selects this notification PendingIntent contentIntent = PendingIntent.getActivity(this, 0, new Intent(this, Controller.class), 0); // Set the info for the views that show in the notification panel. - notification.setLatestEventInfo(this, getText(R.string.local_service_label), - text, contentIntent); - + Notification notification = new Notification.Builder(this) + .setSmallIcon(R.drawable.stat_sample) // the status icon + .setTicker(text) // the status text + .setWhen(System.currentTimeMillis()) // the time stamp + .setContentTitle(getText(R.string.alarm_service_label)) // the label + .setContentText(text) // the contents of the entry + .setContentIntent(contentIntent) // The intent to send when clicked + .build(); + startForegroundCompat(R.string.foreground_service_started, notification); } else if (ACTION_BACKGROUND.equals(intent.getAction())) { 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 63a254f7f..399f52d6f 100644 --- a/samples/ApiDemos/src/com/example/android/apis/app/IncomingMessage.java +++ b/samples/ApiDemos/src/com/example/android/apis/app/IncomingMessage.java @@ -121,24 +121,26 @@ public class IncomingMessage extends Activity { // 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); + Notification.Builder notifBuilder = new Notification.Builder(this) + .setSmallIcon(R.drawable.stat_sample) // the status icon + .setTicker(tickerText) // the status text + .setWhen(System.currentTimeMillis()) // the time stamp + .setContentTitle(from) // the label of the entry + .setContentText(message) // the contents of the entry + .setContentIntent(contentIntent); // The intent to send when the entry is clicked // 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; + notifBuilder.setDefaults(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); + nm.notify(R.string.imcoming_message_ticker_text, notifBuilder.build()); } //END_INCLUDE(app_notification) @@ -174,24 +176,26 @@ public class IncomingMessage extends Activity { // 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); + Notification.Builder notifBuilder = new Notification.Builder(this) + .setSmallIcon(R.drawable.stat_sample) // the status icon + .setTicker(tickerText) // the status text + .setWhen(System.currentTimeMillis()) // the time stamp + .setContentTitle(from) // the label of the entry + .setContentText(message) // the contents of the entry + .setContentIntent(contentIntent); // The intent to send when the entry is clicked // 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; + notifBuilder.setDefaults(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); + nm.notify(R.string.imcoming_message_ticker_text, notifBuilder.build()); } //END_INCLUDE(interstitial_notification) } diff --git a/samples/ApiDemos/src/com/example/android/apis/app/LocalService.java b/samples/ApiDemos/src/com/example/android/apis/app/LocalService.java index ea1b6816b..a3742ca6a 100644 --- a/samples/ApiDemos/src/com/example/android/apis/app/LocalService.java +++ b/samples/ApiDemos/src/com/example/android/apis/app/LocalService.java @@ -100,17 +100,19 @@ public class LocalService extends Service { // In this sample, we'll use the same text for the ticker and the expanded notification CharSequence text = getText(R.string.local_service_started); - // Set the icon, scrolling text and timestamp - Notification notification = new Notification(R.drawable.stat_sample, text, - System.currentTimeMillis()); - // The PendingIntent to launch our activity if the user selects this notification PendingIntent contentIntent = PendingIntent.getActivity(this, 0, new Intent(this, LocalServiceActivities.Controller.class), 0); // Set the info for the views that show in the notification panel. - notification.setLatestEventInfo(this, getText(R.string.local_service_label), - text, contentIntent); + Notification notification = new Notification.Builder(this) + .setSmallIcon(R.drawable.stat_sample) // the status icon + .setTicker(text) // the status text + .setWhen(System.currentTimeMillis()) // the time stamp + .setContentTitle(getText(R.string.local_service_label)) // the label of the entry + .setContentText(text) // the contents of the entry + .setContentIntent(contentIntent) // The intent to send when the entry is clicked + .build(); // Send the notification. mNM.notify(NOTIFICATION, notification); diff --git a/samples/ApiDemos/src/com/example/android/apis/app/MessengerService.java b/samples/ApiDemos/src/com/example/android/apis/app/MessengerService.java index a21763e3e..f25af9154 100644 --- a/samples/ApiDemos/src/com/example/android/apis/app/MessengerService.java +++ b/samples/ApiDemos/src/com/example/android/apis/app/MessengerService.java @@ -149,17 +149,19 @@ public class MessengerService extends Service { // In this sample, we'll use the same text for the ticker and the expanded notification CharSequence text = getText(R.string.remote_service_started); - // Set the icon, scrolling text and timestamp - Notification notification = new Notification(R.drawable.stat_sample, text, - System.currentTimeMillis()); - // The PendingIntent to launch our activity if the user selects this notification PendingIntent contentIntent = PendingIntent.getActivity(this, 0, new Intent(this, Controller.class), 0); // Set the info for the views that show in the notification panel. - notification.setLatestEventInfo(this, getText(R.string.remote_service_label), - text, contentIntent); + Notification notification = new Notification.Builder(this) + .setSmallIcon(R.drawable.stat_sample) // the status icon + .setTicker(text) // the status text + .setWhen(System.currentTimeMillis()) // the time stamp + .setContentTitle(getText(R.string.local_service_label)) // the label of the entry + .setContentText(text) // the contents of the entry + .setContentIntent(contentIntent) // The intent to send when the entry is clicked + .build(); // Send the notification. // We use a string id because it is a unique number. We use it later to cancel. diff --git a/samples/ApiDemos/src/com/example/android/apis/app/NotifyingService.java b/samples/ApiDemos/src/com/example/android/apis/app/NotifyingService.java index 3b8139f93..acb00950c 100644 --- a/samples/ApiDemos/src/com/example/android/apis/app/NotifyingService.java +++ b/samples/ApiDemos/src/com/example/android/apis/app/NotifyingService.java @@ -94,20 +94,23 @@ public class NotifyingService extends Service { // In this sample, we'll use the same text for the ticker and the expanded notification CharSequence text = getText(textId); - // Set the icon, scrolling text and timestamp. - // Note that in this example, we pass null for tickerText. We update the icon enough that - // it is distracting to show the ticker text every time it changes. We strongly suggest - // that you do this as well. (Think of of the "New hardware found" or "Network connection - // changed" messages that always pop up) - Notification notification = new Notification(moodId, null, System.currentTimeMillis()); - // The PendingIntent to launch our activity if the user selects this notification PendingIntent contentIntent = PendingIntent.getActivity(this, 0, new Intent(this, NotifyingController.class), 0); + // Set the icon and timestamp. + // Note that in this example, we do not set the tickerText. We update the icon enough that + // it is distracting to show the ticker text every time it changes. We strongly suggest + // that you do this as well. (Think of of the "New hardware found" or "Network connection + // changed" messages that always pop up) // Set the info for the views that show in the notification panel. - notification.setLatestEventInfo(this, getText(R.string.status_bar_notifications_mood_title), - text, contentIntent); + Notification notification = new Notification.Builder(this) + .setSmallIcon(moodId) + .setWhen(System.currentTimeMillis()) + .setContentTitle(getText(R.string.status_bar_notifications_mood_title)) + .setContentText(text) // the contents of the entry + .setContentIntent(contentIntent) // The intent to send when the entry is clicked + .build(); // Send the notification. // We use a layout id because it is a unique number. We use it later to cancel. diff --git a/samples/ApiDemos/src/com/example/android/apis/app/RemoteService.java b/samples/ApiDemos/src/com/example/android/apis/app/RemoteService.java index 3745def07..41b9d886a 100644 --- a/samples/ApiDemos/src/com/example/android/apis/app/RemoteService.java +++ b/samples/ApiDemos/src/com/example/android/apis/app/RemoteService.java @@ -190,17 +190,19 @@ public class RemoteService extends Service { // In this sample, we'll use the same text for the ticker and the expanded notification CharSequence text = getText(R.string.remote_service_started); - // Set the icon, scrolling text and timestamp - Notification notification = new Notification(R.drawable.stat_sample, text, - System.currentTimeMillis()); - // The PendingIntent to launch our activity if the user selects this notification PendingIntent contentIntent = PendingIntent.getActivity(this, 0, new Intent(this, Controller.class), 0); // Set the info for the views that show in the notification panel. - notification.setLatestEventInfo(this, getText(R.string.remote_service_label), - text, contentIntent); + Notification notification = new Notification.Builder(this) + .setSmallIcon(R.drawable.stat_sample) // the status icon + .setTicker(text) // the status text + .setWhen(System.currentTimeMillis()) // the time stamp + .setContentTitle(getText(R.string.remote_service_label)) // the label of the entry + .setContentText(text) // the contents of the entry + .setContentIntent(contentIntent) // The intent to send when the entry is clicked + .build(); // Send the notification. // We use a string id because it is a unique number. We use it later to cancel. diff --git a/samples/ApiDemos/src/com/example/android/apis/app/ServiceStartArguments.java b/samples/ApiDemos/src/com/example/android/apis/app/ServiceStartArguments.java index f55144737..584dff35c 100644 --- a/samples/ApiDemos/src/com/example/android/apis/app/ServiceStartArguments.java +++ b/samples/ApiDemos/src/com/example/android/apis/app/ServiceStartArguments.java @@ -174,24 +174,25 @@ public class ServiceStartArguments extends Service { * Show a notification while this service is running. */ private void showNotification(String text) { - // Set the icon, scrolling text and timestamp - Notification notification = new Notification(R.drawable.stat_sample, text, - System.currentTimeMillis()); - // The PendingIntent to launch our activity if the user selects this notification PendingIntent contentIntent = PendingIntent.getActivity(this, 0, new Intent(this, Controller.class), 0); // Set the info for the views that show in the notification panel. - notification.setLatestEventInfo(this, getText(R.string.service_start_arguments_label), - text, contentIntent); + Notification.Builder noteBuilder = new Notification.Builder(this) + .setSmallIcon(R.drawable.stat_sample) // the status icon + .setTicker(text) // the status text + .setWhen(System.currentTimeMillis()) // the time stamp + .setContentTitle(getText(R.string.service_start_arguments_label)) // the label + .setContentText(text) // the contents of the entry + .setContentIntent(contentIntent); // The intent to send when the entry is clicked // We show this for as long as our service is processing a command. - notification.flags |= Notification.FLAG_ONGOING_EVENT; + noteBuilder.setOngoing(true); // Send the notification. // We use a string id because it is a unique number. We use it later to cancel. - mNM.notify(R.string.service_created, notification); + mNM.notify(R.string.service_created, noteBuilder.build()); } private void hideNotification() { 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 7179b25ed..4ae7553e7 100644 --- a/samples/ApiDemos/src/com/example/android/apis/app/StatusBarNotifications.java +++ b/samples/ApiDemos/src/com/example/android/apis/app/StatusBarNotifications.java @@ -194,20 +194,25 @@ public class StatusBarNotifications extends Activity { // In this sample, we'll use the same text for the ticker and the expanded notification CharSequence text = getText(textId); - // choose the ticker text - String tickerText = showTicker ? getString(textId) : null; - - // Set the icon, scrolling text and timestamp - Notification notification = new Notification(moodId, tickerText, - System.currentTimeMillis()); + // In this sample, we'll use this text for the title of the notification + CharSequence title = getText(R.string.status_bar_notifications_mood_title); // Set the info for the views that show in the notification panel. - notification.setLatestEventInfo(this, getText(R.string.status_bar_notifications_mood_title), - text, makeMoodIntent(moodId)); + Notification.Builder notifBuidler = new Notification.Builder(this) // the context to use + .setSmallIcon(moodId) // the status icon + .setWhen(System.currentTimeMillis()) // the timestamp for the notification + .setContentTitle(title) // the title for the notification + .setContentText(text) // the details to display in the notification + .setContentIntent(makeMoodIntent(moodId)); // The intent to send clicked + + if (showTicker) { + // include the ticker text + notifBuidler.setTicker(getString(textId)); + } // Send the notification. // We use a layout id because it is a unique number. We use it later to cancel. - mNotificationManager.notify(MOOD_NOTIFICATIONS, notification); + mNotificationManager.notify(MOOD_NOTIFICATIONS, notifBuidler.build()); } private void setMoodView(int moodId, int textId) { @@ -239,29 +244,27 @@ public class StatusBarNotifications extends Activity { } private void setDefault(int defaults) { - - // 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 = 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); - final Notification notification = new Notification( - R.drawable.stat_happy, // the icon for the status bar - text, // the text to display in the ticker - System.currentTimeMillis()); // the timestamp for the notification + // In this sample, we'll use this text for the title of the notification + CharSequence title = getText(R.string.status_bar_notifications_mood_title); - notification.setLatestEventInfo( - this, // the context to use - getText(R.string.status_bar_notifications_mood_title), - // the title for the notification - text, // the details to display in the notification - contentIntent); // the contentIntent (see above) + // Set the info for the views that show in the notification panel. + Notification notification = new Notification.Builder(this) // the context to use + .setSmallIcon(R.drawable.stat_happy) // the status icon + .setTicker(text) // the text to display in the ticker + .setWhen(System.currentTimeMillis()) // the timestamp for the notification + .setContentTitle(title) // the title for the notification + .setContentText(text) // the details to display in the notification + .setContentIntent(contentIntent) // The intent to send when the entry is clicked + .setDefaults(defaults) + .build(); - notification.defaults = defaults; - mNotificationManager.notify( MOOD_NOTIFICATIONS, // we use a string id because it is a unique // number. we use it later to cancel the notification diff --git a/samples/RandomMusicPlayer/src/com/example/android/musicplayer/MusicService.java b/samples/RandomMusicPlayer/src/com/example/android/musicplayer/MusicService.java index 25f5d81fb..5c3ccf4da 100644 --- a/samples/RandomMusicPlayer/src/com/example/android/musicplayer/MusicService.java +++ b/samples/RandomMusicPlayer/src/com/example/android/musicplayer/MusicService.java @@ -150,7 +150,7 @@ public class MusicService extends Service implements OnCompletionListener, OnPre AudioManager mAudioManager; NotificationManager mNotificationManager; - Notification mNotification = null; + Notification.Builder mNotificationBuilder = null; /** * Makes sure the media player exists and has been reset. This will create the media player @@ -516,8 +516,9 @@ public class MusicService extends Service implements OnCompletionListener, OnPre PendingIntent pi = PendingIntent.getActivity(getApplicationContext(), 0, new Intent(getApplicationContext(), MainActivity.class), PendingIntent.FLAG_UPDATE_CURRENT); - mNotification.setLatestEventInfo(getApplicationContext(), "RandomMusicPlayer", text, pi); - mNotificationManager.notify(NOTIFICATION_ID, mNotification); + mNotificationBuilder.setContentText(text) + .setContentIntent(pi); + mNotificationManager.notify(NOTIFICATION_ID, mNotificationBuilder.build()); } /** @@ -529,13 +530,18 @@ public class MusicService extends Service implements OnCompletionListener, OnPre PendingIntent pi = PendingIntent.getActivity(getApplicationContext(), 0, new Intent(getApplicationContext(), MainActivity.class), PendingIntent.FLAG_UPDATE_CURRENT); - mNotification = new Notification(); - mNotification.tickerText = text; - mNotification.icon = R.drawable.ic_stat_playing; - mNotification.flags |= Notification.FLAG_ONGOING_EVENT; - mNotification.setLatestEventInfo(getApplicationContext(), "RandomMusicPlayer", - text, pi); - startForeground(NOTIFICATION_ID, mNotification); + + // Build the notification object. + mNotificationBuilder = new Notification.Builder(getApplicationContext()) + .setSmallIcon(R.drawable.ic_stat_playing) + .setTicker(text) + .setWhen(System.currentTimeMillis()) + .setContentTitle("RandomMusicPlayer") + .setContentText(text) + .setContentIntent(pi) + .setOngoing(true); + + startForeground(NOTIFICATION_ID, mNotificationBuilder.build()); } /** |