am 9265a12e: am 5d969b3f: Merge "remove setLatestEventInfo from API samples" into mnc-dev

* commit '9265a12e443e6e1bbdb26c32ec71adc6ee609f9e':
  remove setLatestEventInfo from API samples
This commit is contained in:
Chris Wren
2015-06-15 21:22:22 +00:00
committed by Android Git Automerger
12 changed files with 133 additions and 112 deletions

View File

@@ -29,6 +29,7 @@
<!-- <!--
Declares the application, its icon, and its visible label Declares the application, its icon, and its visible label
--> -->
<uses-sdk android:minSdkVersion="11" />
<application <application
android:icon="@drawable/icon" android:icon="@drawable/icon"
android:label="@string/app_name"> android:label="@string/app_name">

View File

@@ -181,13 +181,6 @@ public class AlarmService extends Service {
// Sets the text to use for the status bar and status list views. // Sets the text to use for the status bar and status list views.
CharSequence notificationText = getText(R.string.alarm_service_started); 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 // Sets up the Intent that starts AlarmActivity
mContentIntent = PendingIntent.getActivity( mContentIntent = PendingIntent.getActivity(
this, // Start the Activity in the current context 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 0 // Use an existing activity instance if available
); );
// Creates a new content view for the mNotification. The view appears when the user // Build the notification object.
// shows the expanded status window. mNotification = new Notification.Builder(this) // The builder requires the context
mNotification.setLatestEventInfo( .setSmallIcon(R.drawable.stat_sample) // the status icon
this, // Put the content view in the current context .setTicker(notificationText) // the status text
getText(R.string.alarm_service_label), // The text to use as the label of the entry .setWhen(System.currentTimeMillis()) // the time stamp
notificationText, // The text to use as the contents of the entry .setContentTitle(getText(R.string.alarm_service_label)) // the label of the entry
mContentIntent // The intent to send when the entry is clicked .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 // 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 // displayed. The ID is the integer marker for the notification string, which is

View File

@@ -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 // In this sample, we'll use the same text for the ticker and the expanded notification
CharSequence text = getText(R.string.alarm_service_started); 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 // The PendingIntent to launch our activity if the user selects this notification
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
new Intent(this, AlarmService.class), 0); new Intent(this, AlarmService.class), 0);
// Set the info for the views that show in the notification panel. // Set the info for the views that show in the notification panel.
notification.setLatestEventInfo(this, getText(R.string.alarm_service_label), Notification notification = new Notification.Builder(this)
text, contentIntent); .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. // Send the notification.
// We use a layout id because it is a unique number. We use it later to cancel. // We use a layout id because it is a unique number. We use it later to cancel.

View File

@@ -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 // In this sample, we'll use the same text for the ticker and the expanded notification
CharSequence text = getText(R.string.foreground_service_started); 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, PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
new Intent(this, Controller.class), 0); new Intent(this, Controller.class), 0);
// Set the info for the views that show in the notification panel. // Set the info for the views that show in the notification panel.
notification.setLatestEventInfo(this, getText(R.string.local_service_label), Notification notification = new Notification.Builder(this)
text, contentIntent); .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); startForegroundCompat(R.string.foreground_service_started, notification);
} else if (ACTION_BACKGROUND.equals(intent.getAction())) { } else if (ACTION_BACKGROUND.equals(intent.getAction())) {

View File

@@ -121,24 +121,26 @@ public class IncomingMessage extends Activity {
// The ticker text, this uses a formatted string so our message could be localized // The ticker text, this uses a formatted string so our message could be localized
String tickerText = getString(R.string.imcoming_message_ticker_text, message); 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. // 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. // 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 // Note that if you want any of these behaviors, you should always have
// a preference for the user to turn them off. // 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 // 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 notification. It could be any integer you want, but we use
// the convention of using a resource id for a string related to // the convention of using a resource id for a string related to
// the notification. It will always be a unique number within your // the notification. It will always be a unique number within your
// application. // application.
nm.notify(R.string.imcoming_message_ticker_text, notif); nm.notify(R.string.imcoming_message_ticker_text, notifBuilder.build());
} }
//END_INCLUDE(app_notification) //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 // The ticker text, this uses a formatted string so our message could be localized
String tickerText = getString(R.string.imcoming_message_ticker_text, message); 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. // 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. // 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 // Note that if you want any of these behaviors, you should always have
// a preference for the user to turn them off. // 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 // 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 notification. It could be any integer you want, but we use
// the convention of using a resource id for a string related to // the convention of using a resource id for a string related to
// the notification. It will always be a unique number within your // the notification. It will always be a unique number within your
// application. // application.
nm.notify(R.string.imcoming_message_ticker_text, notif); nm.notify(R.string.imcoming_message_ticker_text, notifBuilder.build());
} }
//END_INCLUDE(interstitial_notification) //END_INCLUDE(interstitial_notification)
} }

View File

@@ -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 // In this sample, we'll use the same text for the ticker and the expanded notification
CharSequence text = getText(R.string.local_service_started); 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 // The PendingIntent to launch our activity if the user selects this notification
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
new Intent(this, LocalServiceActivities.Controller.class), 0); new Intent(this, LocalServiceActivities.Controller.class), 0);
// Set the info for the views that show in the notification panel. // Set the info for the views that show in the notification panel.
notification.setLatestEventInfo(this, getText(R.string.local_service_label), Notification notification = new Notification.Builder(this)
text, contentIntent); .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. // Send the notification.
mNM.notify(NOTIFICATION, notification); mNM.notify(NOTIFICATION, notification);

View File

@@ -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 // In this sample, we'll use the same text for the ticker and the expanded notification
CharSequence text = getText(R.string.remote_service_started); 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 // The PendingIntent to launch our activity if the user selects this notification
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
new Intent(this, Controller.class), 0); new Intent(this, Controller.class), 0);
// Set the info for the views that show in the notification panel. // Set the info for the views that show in the notification panel.
notification.setLatestEventInfo(this, getText(R.string.remote_service_label), Notification notification = new Notification.Builder(this)
text, contentIntent); .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. // Send the notification.
// We use a string id because it is a unique number. We use it later to cancel. // We use a string id because it is a unique number. We use it later to cancel.

View File

@@ -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 // In this sample, we'll use the same text for the ticker and the expanded notification
CharSequence text = getText(textId); 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 // The PendingIntent to launch our activity if the user selects this notification
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
new Intent(this, NotifyingController.class), 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. // Set the info for the views that show in the notification panel.
notification.setLatestEventInfo(this, getText(R.string.status_bar_notifications_mood_title), Notification notification = new Notification.Builder(this)
text, contentIntent); .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. // Send the notification.
// We use a layout id because it is a unique number. We use it later to cancel. // We use a layout id because it is a unique number. We use it later to cancel.

View File

@@ -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 // In this sample, we'll use the same text for the ticker and the expanded notification
CharSequence text = getText(R.string.remote_service_started); 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 // The PendingIntent to launch our activity if the user selects this notification
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
new Intent(this, Controller.class), 0); new Intent(this, Controller.class), 0);
// Set the info for the views that show in the notification panel. // Set the info for the views that show in the notification panel.
notification.setLatestEventInfo(this, getText(R.string.remote_service_label), Notification notification = new Notification.Builder(this)
text, contentIntent); .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. // Send the notification.
// We use a string id because it is a unique number. We use it later to cancel. // We use a string id because it is a unique number. We use it later to cancel.

View File

@@ -174,24 +174,25 @@ public class ServiceStartArguments extends Service {
* Show a notification while this service is running. * Show a notification while this service is running.
*/ */
private void showNotification(String text) { 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 // The PendingIntent to launch our activity if the user selects this notification
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
new Intent(this, Controller.class), 0); new Intent(this, Controller.class), 0);
// Set the info for the views that show in the notification panel. // Set the info for the views that show in the notification panel.
notification.setLatestEventInfo(this, getText(R.string.service_start_arguments_label), Notification.Builder noteBuilder = new Notification.Builder(this)
text, contentIntent); .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. // 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. // Send the notification.
// We use a string id because it is a unique number. We use it later to cancel. // 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() { private void hideNotification() {

View File

@@ -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 // In this sample, we'll use the same text for the ticker and the expanded notification
CharSequence text = getText(textId); CharSequence text = getText(textId);
// choose the ticker text // In this sample, we'll use this text for the title of the notification
String tickerText = showTicker ? getString(textId) : null; CharSequence title = getText(R.string.status_bar_notifications_mood_title);
// Set the icon, scrolling text and timestamp
Notification notification = new Notification(moodId, tickerText,
System.currentTimeMillis());
// Set the info for the views that show in the notification panel. // Set the info for the views that show in the notification panel.
notification.setLatestEventInfo(this, getText(R.string.status_bar_notifications_mood_title), Notification.Builder notifBuidler = new Notification.Builder(this) // the context to use
text, makeMoodIntent(moodId)); .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. // Send the notification.
// We use a layout id because it is a unique number. We use it later to cancel. // 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) { private void setMoodView(int moodId, int textId) {
@@ -239,29 +244,27 @@ public class StatusBarNotifications extends Activity {
} }
private void setDefault(int defaults) { 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. // This is who should be launched if the user selects our notification.
PendingIntent contentIntent = makeDefaultIntent(); PendingIntent contentIntent = makeDefaultIntent();
// In this sample, we'll use the same text for the ticker and the expanded notification // 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); CharSequence text = getText(R.string.status_bar_notifications_happy_message);
final Notification notification = new Notification( // In this sample, we'll use this text for the title of the notification
R.drawable.stat_happy, // the icon for the status bar CharSequence title = getText(R.string.status_bar_notifications_mood_title);
text, // the text to display in the ticker
System.currentTimeMillis()); // the timestamp for the notification
notification.setLatestEventInfo( // Set the info for the views that show in the notification panel.
this, // the context to use Notification notification = new Notification.Builder(this) // the context to use
getText(R.string.status_bar_notifications_mood_title), .setSmallIcon(R.drawable.stat_happy) // the status icon
// the title for the notification .setTicker(text) // the text to display in the ticker
text, // the details to display in the notification .setWhen(System.currentTimeMillis()) // the timestamp for the notification
contentIntent); // the contentIntent (see above) .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( mNotificationManager.notify(
MOOD_NOTIFICATIONS, // we use a string id because it is a unique MOOD_NOTIFICATIONS, // we use a string id because it is a unique
// number. we use it later to cancel the notification // number. we use it later to cancel the notification

View File

@@ -150,7 +150,7 @@ public class MusicService extends Service implements OnCompletionListener, OnPre
AudioManager mAudioManager; AudioManager mAudioManager;
NotificationManager mNotificationManager; 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 * 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, PendingIntent pi = PendingIntent.getActivity(getApplicationContext(), 0,
new Intent(getApplicationContext(), MainActivity.class), new Intent(getApplicationContext(), MainActivity.class),
PendingIntent.FLAG_UPDATE_CURRENT); PendingIntent.FLAG_UPDATE_CURRENT);
mNotification.setLatestEventInfo(getApplicationContext(), "RandomMusicPlayer", text, pi); mNotificationBuilder.setContentText(text)
mNotificationManager.notify(NOTIFICATION_ID, mNotification); .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, PendingIntent pi = PendingIntent.getActivity(getApplicationContext(), 0,
new Intent(getApplicationContext(), MainActivity.class), new Intent(getApplicationContext(), MainActivity.class),
PendingIntent.FLAG_UPDATE_CURRENT); PendingIntent.FLAG_UPDATE_CURRENT);
mNotification = new Notification();
mNotification.tickerText = text; // Build the notification object.
mNotification.icon = R.drawable.ic_stat_playing; mNotificationBuilder = new Notification.Builder(getApplicationContext())
mNotification.flags |= Notification.FLAG_ONGOING_EVENT; .setSmallIcon(R.drawable.ic_stat_playing)
mNotification.setLatestEventInfo(getApplicationContext(), "RandomMusicPlayer", .setTicker(text)
text, pi); .setWhen(System.currentTimeMillis())
startForeground(NOTIFICATION_ID, mNotification); .setContentTitle("RandomMusicPlayer")
.setContentText(text)
.setContentIntent(pi)
.setOngoing(true);
startForeground(NOTIFICATION_ID, mNotificationBuilder.build());
} }
/** /**