android-Building a Notification,Preserving Navigation when Starting an Activity
来源:互联网 发布:雅思7分有多难 知乎 编辑:程序博客网 时间:2024/06/08 10:48
NotificationCompat.Builder
is in the Support Library. You should use NotificationCompat
and 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.Builder
object. At bare minimum, a Builder
object 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 callnotify()
, 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 aNotification
object containing your specifications.For example:
NotificationCompat.Builder mBuilder;...// Sets an ID for the notificationint mNotificationId = 001;// Gets an instance of the NotificationManager serviceNotificationManager mNotifyMgr = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);// Builds the notification and issues it.mNotifyMgr.notify(mNotificationId, mBuilder.build());
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 activity
- You're starting an
Activity
that's part of the application's normal workflow. - Special activity
- The user only sees this
Activity
if it's started from a notification. In a sense, theActivity
extends 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 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());A special
Activity
doesn't need a back stack, so you don't have to define its Activity
hierarchy in the manifest, and you don't have to call addParentStack()
to build a back stack. Instead, use the manifest to set up the Activity
task options, and create the PendingIntent
by calling getActivity()
android:name="activityclass"
- The activity's fully-qualified class name.
android:taskAffinity=""
- Combined with the
FLAG_ACTIVITY_NEW_TASK
flag that you set in code, this ensures that thisActivity
doesn'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
Intent
that starts theActivity
.- Set the
Activity
to start in a new, empty task by callingsetFlags()
with the flagsFLAG_ACTIVITY_NEW_TASK
andFLAG_ACTIVITY_CLEAR_TASK
.- Set any other options you need for the
Intent
.- Create a
PendingIntent
from theIntent
by callinggetActivity()
. You can then use thisPendingIntent
as the argument tosetContentIntent()
.
// Instantiate a Builder object.NotificationCompat.Builder builder = new NotificationCompat.Builder(this);// Creates an Intent for the ActivityIntent notifyIntent = new Intent(new ComponentName(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 notifyIntent = PendingIntent.getActivity( this, 0, notifyIntent, PendingIntent.FLAG_UPDATE_CURRENT);// Puts the PendingIntent into the notification builderbuilder.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 NotificationManagermNotificationManager.notify(id, builder.build());
0 0
- android-Building a Notification,Preserving Navigation when Starting an Activity
- Notifications --- Preserving Navigation when Starting an Activity
- Android之Starting an Activity for a Result例子简介
- Android之Starting an Activity for a Result例子简介
- Android Activity生命周期 之starting an activity
- Starting an Activity
- 05Starting an Activity
- Starting an Activity
- Starting an Activity
- 【English】Android -> Training -> Building Your First App -> Starting Another Activity
- [Android Training视频系列]2.1 Starting an Activity
- Android官方文档翻译 十七 4.1Starting an Activity
- Process of Starting an Activity
- Android training 管理Activity生命周期之启动Activity(Starting an Activity)
- 启动一个Activity - Starting an Activity
- 【Android界面实现】Starting an Activity(Activity生命周期金字塔模型)
- Android官方Training阅读笔记 ---- Managing the Activity Lifecycle(Starting an Activity) (一)
- A first hand look at building an Android application
- hdu1213 How Many Tables
- HDU1754线段树
- 排版题 3
- mysql根据select查询结果 循环更改
- ionic 开发小问题集锦
- android-Building a Notification,Preserving Navigation when Starting an Activity
- AlertDialog编辑框无法调出输入法、锁定Activity方向、自定义进度条的WebView基础使用
- 一个对象怎么调用另一个对象, 打jar包,到底调用哪一个方法, 匿名对象, 空对象
- POJ2503字典树
- 使用Excel画甘特图
- CComPtr用法(转)
- AFNetworking-2.0 使用方法
- 周末私家菜谱
- hdoj 1829 A Bug's Life