这篇文章主要介绍了 Android 中 Notification 用法, 较为详细的分析了 Notification 的功能、使用步骤与相关注意事项, 需要的朋友可以参考下
Android 是一种基于 Linux 的自由及开放源代码的操作系统,主要使用于移动设备,如智能手机和平板电脑,由 Google 公司和开放手机联盟领导及开发。尚未有统一中文名称,中国大陆地区较多人使用 "安卓" 或 "安致"。
本文实例讲述了 Android 中 Notification 用法。分享给大家供大家参考,具体如下:
Notification 可以理解为通知的意思一般用来显示广播信息 用 Notification 就必须要用到 NotificationManager
想用 Notification 一般有三个步骤,如下所示
① 一般获得系统级的服务 NotificationManager。
调用 Context.getSystemService(NOTIFICATION_SERVICE) 方法即可返回 NotificationManager 实例
② 实例化 Notification,并设置其属性
用 Notification 构造函数 public Notification(int icon, CharSequence tickerText, long when) 构造 Notification 实例
③ 通过 NotificationManager 发通知就 OK 了
NotificationManager 有两个方法:notify() 发出通知 cancel(...) 取消通知
下面通过一个代码实例来介绍一下 Notification
先初始化 notificationManager 和 notification 两个成员变量
- notificationManager = (NotificationManager)this.getSystemService(NOTIFICATION_SERVICE);
- notification = new Notification(R.drawable.touxiang,"信息",System.currentTimeMillis());
PS:Notification 构造函数里传的参数就是这样显示的第一个参数是 图像,第二个是标题 :信息 ,第三个是系统时间 (见图一) 之所以在这说一下这个构造函数是想与下面的 setLatestEventInfo(...) 作个区别。
( 图一 )
( 图二 )
布局就不贴了 是两个 Button 看看发送按钮
- sendButton.setOnClickListener(new OnClickListener()
- {
- @Override
- public void onClick(View v)
- {
- Intent intent = new Intent(NotificationActivity.this,NotificationActivity.class);
- PendingIntent pendingIntent = PendingIntent.getActivity(NotificationActivity.this, 0, intent, 0);
- notification.setLatestEventInfo(NotificationActivity.this, "你的一条信息", "来自张三的信息", pendingIntent);
- notificationManager.notify(ID,notification);
- notificationManager.notify(ID+1, notification);
- }
- })
setLatestEventInfo(...) 里面所传的参数的效果图:(见图二)通知里面有两条完全一样的消息,是的 你没看错,这是因为我用 notificationManager notify(通知) 了两次 而且 ID 不同, ID 是 int 型是通知信息的标示符。虽然我上面两条信息是一模一样的, 但由于 ID 的不同 , 所以 Android 还是会显示两条信息。
在此说一下参数 pendingIntent 的在 setLatestEventInfo 里所扮演的角色,是啥子意思呢?pendingIntent 可以在另外的地方执行, 不是立即意图。当用户点击扩展通知的时候 pendingIntent 意图才开始执行,例如图二 我点击其中一个消息后,立马就进入另外一个 Activity...
正如上面看到的那样,除了为 notification 设置图标,标题外还可以设置提示音, 震动, 闪光灯 详情请见我转的一片文章 Notification 使用详解.....
- package com.study.android;
- import android.app.Activity;
- import android.app.Notification;
- import android.app.NotificationManager;
- import android.app.PendingIntent;
- import android.content.Context;
- import android.content.Intent;
- import android.os.Bundle;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.Button;
- public class MainActivity extends Activity {
- private Button startBtn;
- private Button cancelBtn;
- private static final int HELLO_ID = 1;
- NotificationManager mNotificationManager;
- Notification mNotification;
- /** Called when the activity is first created. */
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- startBtn = (Button)findViewById(R.id.startBtn);
- cancelBtn = (Button)findViewById(R.id.cancelBtn);
- // ① 获取NotificationManager的引用
- String ns = Context.NOTIFICATION_SERVICE;
- mNotificationManager = (NotificationManager)this.getSystemService(ns);
- // ② 初始化Notification
- int icon = R.drawable.ic_launcher;
- CharSequence tickerText = "Hello";
- long when = System.currentTimeMillis();
- mNotification = new Notification(icon,tickerText,when);
- mNotification.defaults = Notification.DEFAULT_ALL;
- mNotification.flags |= Notification.FLAG_NO_CLEAR;
- mNotification.flags |= Notification.FLAG_SHOW_LIGHTS;
- // ③ 定义notification的消息 和 PendingIntent
- Context context = this;
- CharSequence contentTitle ="My notification";
- CharSequence contentText = "Hello World";
- Intent notificationIntent = new Intent(this,MainActivity.class);
- PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent,0 );
- mNotification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
- // ④ 把封装好的notification传入NotificationManager
- // 开启通知
- startBtn.setOnClickListener(new OnClickListener() {
- @Override
- public void onClick(View v) {
- mNotificationManager.notify(HELLO_ID,mNotification);
- }
- });
- // 取消通知
- cancelBtn.setOnClickListener(new OnClickListener() {
- @Override
- public void onClick(View v) {
- mNotificationManager.cancel(HELLO_ID);
- }
- });
- }
- }
代码中有创建 Notification 步骤,详情可以看一下。
希望本文所述对大家 Android 程序设计有所帮助。
来源: http://www.phperz.com/article/17/0331/238703.html