Notifications

Notifications(notification的复数,通知,通告)
A notification is a message you can display(显示) to the user outside of your application's normal UI. When you tell the system to

issue(发行,发布) a notification, it first appears as an icon in the notification area. To see the details of the notification, the user

opens the notification drawer(抽屉). Both the notification area and the notification drawer are system-controlled areas that the user

can view at any time.

Notification Design
Notifications, as an important part of the Android UI, have their own design guidelines(指导方针). To learn how to design

notifications and their interactions(interaction复数,交互,相互作用), read the Android Design Guide Notifications topic.

Note: Except where noted, this guide refers to the NotificationCompat.Builder class in the version 4 Support Library. The class

Notification.Builder was added in Android 3.0.

Notification Display Elements
Notifications in the notification drawer can appear in one of two visual(视觉的) styles, depending on the version and the state of the

drawer:

Normal view
The standard view of the notifications in the notification drawer.
Big view
A large view that's visible when the notification is expanded. Big view is part of the expanded notification feature available as of

Android 4.1.
These styles are described in the following sections.

Normal(正常,标准,常态) view

A notification in normal view appears in an area that's up to 64 dp tall. Even if you create a notification with a big view style, it will

appear in normal view until it's expanded. This is an example of a normal view:

The callouts(插图的编号;图例) in the illustration(说明,插图,图解) refer to the following:


(1)Content title
(2)Large icon
(3)Content text
(4)Content info(信息,情报)
(5)Small icon
(6)Time that the notification was issued(发行,发布). You can set an explicit(明确的,清楚的) value with setWhen(); if you don't it

defaults to the time that the system received the notification.

Big view

A notification's big view appears only when the notification is expanded(扩充的,展开,扩大), which happens when the notification is

at the top of the notification drawer, or when the user expands the notification with a gesture(姿势,手势). Expanded notifications are

available starting with Android 4.1.
The following screenshot(屏幕截图) shows an inbox-style notification:



Notice that the big view shares most of its visual elements with the normal view. The only difference is callout number 7, the details

area. Each big view style sets this area in a different way. The available styles are:

Big picture style:
The details area contains a bitmap up to 256 dp tall in its detail section(截面,地区).
Big text style:
Displays a large text block(块) in the details section.
Inbox(收件箱) style:
Displays lines of text in the details section.
All of the big view styles also have the following content options that aren't available in normal view:

Big content title:
Allows you to override the normal view's content title with a title that appears only in the expanded view.
Summary(概要,摘要) text:
Allows you to add a line of text below the details area.
Applying(应用,申请) a big view style to a notification is described in the section Applying a big view style to a notification.
///创建通知///
Creating a Notification:
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(specification的复

数形式;规格,说明书). To issue the notification, you pass the Notification object to the system by calling NotificationManager.notify

通告,通知().

Required notification contents:

A Notification object must contain the following:
(1)A small icon, set by setSmallIcon().
(2)A title, set by setContentTitle().
(3)Detail text, set by setContentText().

Optional(选择项目;可选择的) notification contents and settings:
All other notification settings and contents are optional. To learn more about them, see the reference documentation for

NotificationCompat.Builder.

Notification actions:

Although they're optional, you should add at least one action to your notification. An action allows users to go directly from the

notification to an Activity in your application, where they can look at one or more events or do further work.

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(snooze小睡,打盹) an alarm(警报) or responding(反应) immediately to a text

message; this feature is available as of Android 4.1. If you use additional(附加的,额外的) action buttons, you must also make their

functionality(功能) available in an Activity in your app; see the section Handling compatibility(兼容性) for more details.

Inside a Notification, the action itself is defined by a PendingIntent containing an Intent that starts an Activity in your application. To

associate(使联合) the PendingIntent with a gesture, call the appropriate(适当的) method of NotificationCompat.Builder. For

example, if you want to start Activity when the user clicks the notification text in the notification drawer, you add the PendingIntent by

calling setContentIntent().

Starting an Activity when the user clicks the notification is the most common action scenario(方案,场景). You can also start an

Activity when the user dismisses(解散) a notification. In Android 4.1 and later, you can start an Activity from an action button. To

learn more, read the reference guide for NotificationCompat.Builder.
****************************************************创建一个简单的通知**********************************************************************
Creating a simple notification:

The following snippet(片段,摘要) illustrates(阐明,举例说明) a simple notification that specifies an activity to open when the user

clicks the notification. Notice that the code creates a TaskStackBuilder object and uses it to create the PendingIntent for the action.

This pattern(模式,样品) is explained in more detail in the section Preserving(保留,保存) Navigation when Starting an Activity:
/**创建一个简单的通知的代码,可以把这些代码放在一个方法内执行:*/
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
this).setSmallIcon(R.drawable.notification_icon)
.setContentTitle("推送消息标题").setContentText("推送消息内容");

/*** Creates an explicit(明确的,清楚的) intent for an Activity in your app */
Intent 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 stack */
stackBuilder.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.
* 这里指定一个id,在后面的ResultActivity里面可以根据这个id的值将这个通知清除.*/
mNotificationManager.notify(1, mBuilder.build());

点击通知跳到ResultActivity.class里面,并在其onCreate里面清除掉这个通知,代码如下:
public class ResultActivity extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.view);
/**清除通知*/
NotificationManager mNotificationManager = (NotificationManager) getSystemService

(Context.NOTIFICATION_SERVICE);
//mNotificationManager.cancelAll();清除所有已经发布的通知
/**根据指定的id清除通知.这里id的值是在发送通知时根据notify方法指定的.*/
mNotificationManager.cancel(1);
long l=System.currentTimeMillis();
SimpleDateFormat sdf=new SimpleDateFormat("yyyy年MM月dd日HH:mm:ss",new Locale

(Locale.CHINA.toString()));
String str=sdf.format(l);
Toast.makeText(this, str, Toast.LENGTH_SHORT).show();
}
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值