Android开发入门之Notification用法分析
Android开发入门之Notification用法分析
发布时间:2016-12-28 来源:查字典编辑
摘要:本文实例讲述了Android中Notification用法。分享给大家供大家参考,具体如下:Notification可以理解为通知的意思一般...

本文实例讲述了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(...)作个区别。

Android开发入门之Notification用法分析1

( 图一 )

Android开发入门之Notification用法分析2

( 图二 )

布局就不贴了 是两个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程序设计有所帮助。

推荐文章
猜你喜欢
附近的人在看
推荐阅读
拓展阅读
相关阅读
网友关注
最新安卓软件开发学习
热门安卓软件开发学习
编程开发子分类