android-Building a Notification,Preserving Navigation when Starting an Activity
2016-01-31 15:56
721 查看
NotificationCompat.Builderis
in the Support Library.
You should use
NotificationCompatand
its subclasses, particularly
NotificationCompat.Builder,
to provide the best notification support for a wide range of platforms.
>When creating a notification, specify the UI content and actions with a
NotificationCompat.Builderobject.
At bare minimum, a
Builderobject must include the following:
A small icon, set by
setSmallIcon()
A title, set by
setContentTitle()
Detail text, set by
setContentText()
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this) .setSmallIcon(R.drawable.notification_icon) .setContentTitle("My notification") .setContentText("Hello World!");
Intent resultIntent = new Intent(this, ResultActivity.class); ... // Because clicking the notification opens a new ("special") activity, there's // no need to create an artificial back stack. PendingIntent resultPendingIntent = PendingIntent.getActivity( this, 0, resultIntent, PendingIntent.FLAG_UPDATE_CURRENT );
To issue the notification:
Get an instance of
NotificationManager.
Use the
notify()method to issue the notification. When
you call
notify(), specify a notification ID. You can use this ID to update the notification
later on. This is described in more detail in Managing Notifications.
Call
build(), which
returns a
Notificationobject containing your specifications.
For example:
NotificationCompat.Builder mBuilder; ... // Sets an ID for the notification int mNotificationId = 001; // Gets an instance of the NotificationManager service NotificationManager mNotifyMgr = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); // Builds the notification and issues it. mNotifyMgr.notify(mNotificationId, mBuilder.build());
> Preserving Navigation when Starting an Activity
Part of designing a notification is preserving the user's expected navigation experience. For a detailed discussion of this topic, see the Notifications API
guide. There are two general situations:
Regular activityYou're starting an
Activitythat's part of the application's
normal workflow.Special activityThe user only sees this
Activityif it's started from
a notification. In a sense, the
Activityextends the notification by providing information
that would be hard to display in the notification itself.
Intent resultIntent = new Intent(this, ResultActivity.class); TaskStackBuilder stackBuilder = TaskStackBuilder.create(this); // Adds the back stack stackBuilder.addParentStack(ResultActivity.class); // Adds the Intent to the top of the stack stackBuilder.addNextIntent(resultIntent); // Gets a PendingIntent containing the entire back stack PendingIntent 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());
A special
Activitydoesn't
need a back stack, so you don't have to define its
Activityhierarchy
in the manifest, and you don't have to call
addParentStack()to
build a back stack. Instead, use the manifest to set up the
Activitytask
options, and create the
PendingIntentby
calling
getActivity()
android:name="activityclass"The activity's fully-qualified class name.
android:taskAffinity=""Combined with the
FLAG_ACTIVITY_NEW_TASKflag
that you set in code, this ensures that this
Activitydoesn't go into the application's default
task. Any existing tasks that have the application's default affinity are not affected.
android:excludeFromRecents="true"Excludes the new task from Recents, so that the user can't accidentally navigate back to it.
This snippet shows the element:
<activity android:name=".ResultActivity" ... android:launchMode="singleTask" android:taskAffinity="" android:excludeFromRecents="true"> </activity>
>Build and issue the notification:Create an [code]Intentthat starts the
Activity.
Set the
Activityto start in a new, empty task by calling
setFlags()with the flags
FLAG_ACTIVITY_NEW_TASKand
FLAG_ACTIVITY_CLEAR_TASK.
Set any other options you need for the
Intent.
Create a
PendingIntentfrom the
Intentby calling
getActivity(). You can then use this
PendingIntentas the argument to
setContentIntent().
[/code]
// Instantiate a Builder object. NotificationCompat.Builder builder = new NotificationCompat.Builder(this); // Creates an Intent for the Activity Intent notifyIntent = new Intent(new ComponentName(this, ResultActivity.class)); // Sets the Activity to start in a new, empty task notifyIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK); // Creates the PendingIntent PendingIntent notifyIntent = PendingIntent.getActivity( this, 0, notifyIntent, PendingIntent.FLAG_UPDATE_CURRENT ); // Puts the PendingIntent into the notification builder builder.setContentIntent(notifyIntent); // 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 NotificationManager mNotificationManager.notify(id, builder.build());
相关文章推荐
- Codeforces 67C Sequence of Balls 编辑距离 dp
- BZOJ-1206 虚拟内存 Hash+离散化+Priority_Queue
- BZOJ-1206 虚拟内存 Hash+离散化+Priority_Queue
- iOS UITextField
- iOS8下的UIAlertContoller初探
- UE-9260使用说明1
- 联合国大楼UVA1605(Building for UN)
- Vawio Sequence (NYOJ 763 LIS变形)
- Build.gradle配置
- 通过StoryBoard创建UIViewController
- 技术杂记-改造具有监控功能的数据库连接池阿里Druid,支持simple-jndi,kettle
- 304. Range Sum Query 2D - Immutable
- QA:UICollectionView不能选中问题
- 处理键盘升起后对界面覆盖问题的处理
- 通过value取得Map中的Key的方法
- Arduino接口-week3-Lesson3
- [转]iOS之浅谈纯代码控制UIViewController视图控制器跳转界面的几种方法
- POJ 2031:Building a Space Station【Prim】
- Arduino接口-week3-Lesson2
- xcode-select: error: tool 'xcodebuild' requires Xcode错误的解决办法