android - Notifications
来源:互联网 发布:wow数据库黑翼之巢 编辑:程序博客网 时间:2024/05/20 17:59
A notification is a message you can display to the user outside of your application's normal UI.
> You specify the UI information and actions for a notification in a NotificationCompat.Builder
object. To create the notification itself, you call NotificationCompat.Builder.build()
, which returns a Notification
object containing your specifications. To issue the notification, you pass the Notification
object to the system by calling NotificationManager.notify()
.
> A Notification
object must contain the following:
- A small icon, set by
setSmallIcon()
- A title, set by
setContentTitle()
- Detail text, set by
setContentText()
>A notification can provide multiple actions. You should always define the action that's triggered when the user clicks the notification; usually this action opens an Activity
in your application. You can also add buttons to the notification that perform additional actions such as snoozing an alarm or responding immediately to a text message; this feature is available as of Android 4.1.
Inside a Notification
, the action itself is defined by a PendingIntent
containing an Intent
that starts anActivity
in your application. To associate the PendingIntent
with a gesture, call the appropriate method ofNotificationCompat.Builder
.
> If you wish, you can set the priority of a notification. The priority acts as a hint to the device UI about how the notification should be displayed. To set a notification's priority, callNotificationCompat.Builder.setPriority()
and pass in one of the NotificationCompat
priority constants. There are five priority levels, ranging from PRIORITY_MIN
(-2) to PRIORITY_MAX
(2); if not set, the priority defaults to PRIORITY_DEFAULT
(0).
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this) .setSmallIcon(R.drawable.notification_icon) .setContentTitle("My notification") .setContentText("Hello World!");// Creates an explicit intent for an Activity in your appIntent resultIntent = new Intent(this, ResultActivity.class);// The stack builder object will contain an artificial back stack for the// started Activity.// This ensures that navigating backward from the Activity leads out of// your application to the Home screen.TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);// Adds the back stack for the Intent (but not the Intent itself)stackBuilder.addParentStack(ResultActivity.class);// Adds the Intent that starts the Activity to the top of the stackstackBuilder.addNextIntent(resultIntent);PendingIntent resultPendingIntent = stackBuilder.getPendingIntent( 0, PendingIntent.FLAG_UPDATE_CURRENT );mBuilder.setContentIntent(resultPendingIntent);NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);// mId allows you to update the notification later on.mNotificationManager.notify(mId, mBuilder.build());>a notification that is updated to reflect the number of events
mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);// Sets an ID for the notification, so it can be updatedint notifyID = 1;mNotifyBuilder = new NotificationCompat.Builder(this) .setContentTitle("New Message") .setContentText("You've received new messages.") .setSmallIcon(R.drawable.ic_notify_status)numMessages = 0;// Start of a loop that processes data and then notifies the user... mNotifyBuilder.setContentText(currentText) .setNumber(++numMessages); // Because the ID remains unchanged, the existing notification is // updated. mNotificationManager.notify( notifyID, mNotifyBuilder.build());...
<activity android:name=".MainActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter></activity><activity android:name=".ResultActivity" android:parentActivityName=".MainActivity"> <meta-data android:name="android.support.PARENT_ACTIVITY" android:value=".MainActivity"/></activity>> Create a back stack based on the
Intent
that starts the Activity
:- Create the
Intent
to start theActivity
. - Create a stack builder by calling
TaskStackBuilder.create()
. - Add the back stack to the stack builder by calling
addParentStack()
. For eachActivity
in the hierarchy you've defined in the manifest, the back stack contains anIntent
object that starts theActivity
. This method also adds flags that start the stack in a fresh task.Note: Although the argument to
addParentStack()
is a reference to the startedActivity
, the method call doesn't add theIntent
that starts theActivity
. Instead, that's taken care of in the next step. - Add the
Intent
that starts theActivity
from the notification, by callingaddNextIntent()
. Pass theIntent
you created in the first step as the argument toaddNextIntent()
. - If you need to, add arguments to
Intent
objects on the stack by callingTaskStackBuilder.editIntentAt()
. This is sometimes necessary to ensure that the targetActivity
displays meaningful data when the user navigates to it using Back. - Get a
PendingIntent
for this back stack by callinggetPendingIntent()
. You can then use thisPendingIntent
as the argument tosetContentIntent()
.
Intent resultIntent = new Intent(this, ResultActivity.class);TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);// Adds the back stackstackBuilder.addParentStack(ResultActivity.class);// Adds the Intent to the top of the stackstackBuilder.addNextIntent(resultIntent);// Gets a PendingIntent containing the entire back stackPendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);...NotificationCompat.Builder builder = new NotificationCompat.Builder(this);builder.setContentIntent(resultPendingIntent);NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);mNotificationManager.notify(id, builder.build());
// Instantiate a Builder object.NotificationCompat.Builder builder = new NotificationCompat.Builder(this);// Creates an Intent for the ActivityIntent notifyIntent = new Intent(this, ResultActivity.class);// Sets the Activity to start in a new, empty tasknotifyIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);// Creates the PendingIntentPendingIntent notifyPendingIntent = PendingIntent.getActivity( this, 0, notifyIntent, PendingIntent.FLAG_UPDATE_CURRENT);// Puts the PendingIntent into the notification builderbuilder.setContentIntent(notifyPendingIntent);// Notifications are issued by sending them to the// NotificationManager system service.NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);// Builds an anonymous Notification object from the builder, and// passes it to the NotificationManagermNotificationManager.notify(id, builder.build());> Then add the actions and set the
Notification.MediaStyle
template:Notification notification = new Notification.Builder(context) // Show controls on lock screen even when user hides sensitive content. .setVisibility(Notification.VISIBILITY_PUBLIC) .setSmallIcon(R.drawable.ic_stat_player) // Add media control buttons that invoke intents in your media service .addAction(R.drawable.ic_prev, "Previous", prevPendingIntent) // #0 .addAction(R.drawable.ic_pause, "Pause", pausePendingIntent) // #1 .addAction(R.drawable.ic_next, "Next", nextPendingIntent) // #2 // Apply the media style template .setStyle(new Notification.MediaStyle() .setShowActionsInCompactView(1 /* #1: pause button */) .setMediaSession(mMediaSession.getSessionToken()) .setContentTitle("Wonderful music") .setContentText("My Awesome Band") .setLargeIcon(albumArtBitmap) .build();
- Android Notifications
- Android: notifications
- Android Notifications
- Android Notifications
- android - Notifications
- Android Notifications
- Android Notifications通知
- Android Notifications通知
- Android Notifications通知
- Android API Guides---Notifications
- Notifications in Android N
- Android 笔记 通知/notifications
- Notifications
- notifications
- Notifications
- Notifications
- Notifications
- Notifications
- vi编辑器技巧总结(更新中)
- Fragment的基本使用方法与通信
- Socket
- android设备终端与蓝牙模块(HC-06)的通讯编程思路
- iOS9模拟器无法进行HTTP网络请求的解决方案
- android - Notifications
- Android进阶之Service远程通信AIDL
- ros由indigo版本升级至jade版本的烦恼
- iOS集成支付宝
- #学习笔记#(13)仿 kugou-logo-phptoshop
- 听说”双11”是这么解决线上bug的
- 解决Xcode7.0在iPhone 9.2 系统真机测试时出现could not find developer disk image问题
- java中内部类之成员内部类
- 学习笔记(2)-标签传播算法伪代码实现