1、获得系统服务
notificationmanager=(NotificationManager) context.getSystemService(NOTIFICATION_SERVICE);
2、创建Notification
notification=new Notification(R.drawable.keai,”可爱”,System.currentTimeMillis());
3、对Notification进行一些设置
notification.flags=Notification.FLAG_AUTO_CANCEL; //设置下拉点击之后回到应用程序,可以有多个值选择
4、Intent与PendingIntent
Intent intent=new Intent(this,Z18Activity.class);
pendingintent=PendingIntent.getActivity(this,0,intent,0);
notification.setLatestEventInfo(this,”真可爱”,”太可爱啦!”,pendingintent);
5、可更改提示下拉条中的布局
RemoteViews rv=new RemoteViews(this.getPackageName(),R.layout.main); //此步骤是修改下拉后看到提示条的布局
notification.contentView=rv;
6、用NotificationManager发送Notification提示信息
notificationmanager.notify(0,notification); //通过标识发送指定的Notification

上面有些步骤是属于并列的,可能后面的操作将覆盖前面的操作

Example1:

Ticker Text:Notification刚出来的时候,在状态栏上滚动的字幕,如果很长,会自动分割滚动
Content Title:Notification展开后的标题
Content Text:Notification展开后的内容

取得NotificationManage

private NotificationManager mNotificationManager;
mNotificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);

创建Notification并且显示

//Notification的滚动提示
String tickerText = "My notification, It's a long text! Hello World desiyo?";
//Notification的图标,一般不要用彩色的
int icon = R.drawable.icon_02241_3;//contentTitle和contentText都是标准的Notification View的内容
//Notification的内容标题,拖下来后看到的标题
String contentTitle="My notification";
//Notification的内容
String contentText="Hello World!";//Notification的Intent,即点击后转向的Activity
Intent notificationIntent = new Intent(this, this.getClass());
notificationIntent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);//创建Notifcation
Notification notification = new Notification(icon, tickerText, System.currentTimeMillis());
//设定Notification出现时的声音,一般不建议自定义
notification.defaults |= Notification.DEFAULT_SOUND;
//设定如何振动
notification.defaults |= Notification.DEFAULT_VIBRATE;
//指定Flag,Notification.FLAG_AUTO_CANCEL意指点击这个Notification后,立刻取消自身
//这符合一般的Notification的运作规范
notification.flags|=Notification.FLAG_AUTO_CANCEL;
notification.setLatestEventInfo(this, contentTitle, contentText, contentIntent);//显示这个notification
mNotificationManager.notify(HELLO_ID, notification);

这是最基本的应用,可以说除了找个合适的图标以外,其它都很简单。
使用自定义View的Notification
同Toast一样,我们也可以自已指定1个View来作为Notification展开后的显示内容,比如说在Android Market中下载的时候,Notification中会显示当前下载的进度,那么我们也来模拟1个这样的效果吧。

首先给出View的定义文件:notification_view_sample.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
        xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="fill_parent"android:layout_height="fill_parent"android:padding="3dp"><ImageView android:id="@+id/notificationImage"android:layout_width="wrap_content"                                   android:layout_height="wrap_content"android:src="@android:drawable/stat_sys_download"/><TextView android:id="@+id/notificationTitle"android:layout_width="wrap_content" android:layout_height="wrap_content"android:layout_toRightOf="@id/notificationImage"android:layout_alignParentRight="true"android:paddingLeft="6dp"android:textColor="#FF000000"/><TextView android:id="@+id/notificationPercent"android:layout_width="wrap_content" android:layout_height="wrap_content"android:layout_below="@id/notificationImage"android:paddingTop="2dp"android:textColor="#FF000000"/><ProgressBar android:id="@+id/notificationProgress"android:layout_width="wrap_content"  android:layout_height="wrap_content"                   android:layout_below="@id/notificationTitle"android:layout_alignLeft="@id/notificationTitle"android:layout_alignParentRight="true"android:layout_alignTop="@id/notificationPercent"android:paddingLeft="6dp"android:paddingRight="3dp"android:paddingTop="2dp"style="?android:attr/progressBarStyleHorizontal"/>
</RelativeLayout>
//Notification的滚动提示
String tickerText1 = "Custom view for download notification";
//Notification的图标,一般不要用彩色的
int icon1 = android.R.drawable.stat_sys_download;//Notification的Intent,即点击后转向的Activity
Intent notificationIntent1 = new Intent(this, this.getClass());
notificationIntent1.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent contentIntent1 = PendingIntent.getActivity(this, 0, notificationIntent1, 0);//创建Notifcation
Notification notification1 = new Notification(icon1, tickerText1, System.currentTimeMillis());
//设定Notification出现时的声音,一般不建议自定义
notification1.defaults |= Notification.DEFAULT_SOUND;
//设定是否振动
notification1.defaults |= Notification.DEFAULT_VIBRATE;
//notification.number=numbers++;
//指定Flag,Notification.FLAG_AUTO_CANCEL意指点击这个Notification后,立刻取消自身
//这符合一般的Notification的运作规范
notification1.flags|=Notification.FLAG_ONGOING_EVENT;//创建RemoteViews用在Notification中
RemoteViews contentView = new RemoteViews(getPackageName(), R.layout.notification_view_sample);
contentView.setTextViewText(R.id.notificationTitle, "Download:Facebook for android");
contentView.setTextViewText(R.id.notificationPercent, "35%");
contentView.setProgressBar(R.id.notificationProgress, 100, 35, false);notification1.contentView = contentView;
notification1.contentIntent=contentIntent1;//显示这个notification
mNotificationManager.notify(CUSTOM_VIEW_ID, notification1);

注意以上代码中使用的是RemoteViews,而不是普通的View,另外使用的是PendingIntent而不是普通的Intent,这都说明了 Notification是1个“远程”的东西,其中能够使用的控件是受限制的,比如说TableLayout就不能使用。看下效果图,是不是和 Market中的界面很接近呢?

由于在使用自定义的view时不必使用setLastEventInfo()方法,所以你必须为Notification的contentIntent域定义一个Intent,如下所示:

RemoteViews contentView = new RemoteViews(getPackageName(),R.layout.notification_view);
contentView.setTextViewText(R.id.notificationTitle, "下载文件:测试文件");
contentView.setTextViewText(R.id.notificationPercent, "55%");
contentView.setProgressBar(R.id.notificationProgress, 100, 55, false);notice.contentView = contentView;
notice.contentIntent = contentIntent;

注意:当为notification创建自定义的布局时,你必须确保它能够再不同的设备方向以及分辨率下都正常工作。这对于所有的布局工作都是十分重要的,所以,不要定义过于复杂的布局,而且要在多种情况下进行测试。

更好的控制Notification 动画图标怎么做?
和selector类似,定义1个XML文件放在drawable下,下面是之前用到的stat_sys_download的定义:

<animation-list
        xmlns:android="http://schemas.android.com/apk/res/android"android:oneshot="false"><item android:drawable="@drawable/stat_sys_download_anim0" android:duration="200" /><item android:drawable="@drawable/stat_sys_download_anim1" android:duration="200" /><item android:drawable="@drawable/stat_sys_download_anim2" android:duration="200" /><item android:drawable="@drawable/stat_sys_download_anim3" android:duration="200" /><item android:drawable="@drawable/stat_sys_download_anim4" android:duration="200" /><item android:drawable="@drawable/stat_sys_download_anim5" android:duration="200" />
</animation-list>

如何更新Notification?
注意到前面的代码中用到的CUSTOM_VIEW_ID,这是Notification的ID,如果2次弹出的Notification的ID相同,那么Notification就只会更新而不会再次滚动提醒。之前给出的ProgressBar是不会动的,利用这个方法就可以让它动起来(或者也可以直接调用RemoteView的set方法来直接更新?未试验)
如何自定义提示的声音和振动 ?
//自定义提示音notification.sound = Uri.parse(“file:///sdcard/notification/ringer.mp3”);
//自定义振动方式long[] vibrate = {0,100,200,300};notification.vibrate = vibrate;
请注意:如果使用了DEFAULT_SOUND或DEFAULT_VIBRATE,则自定义的提示音和振动无效。
在类似于短消息的应用中如何提示数量?
使用Notification的number属性,默认为0,如果是1或更大的数字,则会在图标上覆盖显示这个数字。
notification.number=notificationNumber;
Flag的使用
notification有1个flag属性,除了DEFAULT_SOUND之外,还有几个很有用的属性。
FLAG_AUTO_CANCEL:自动清除Notification,前面的例子中有用到
FLAG_INSISTENT:提示音一直不停,直至用户响应(很吵吧!)
FLAG_ONGOING_EVENT:表示这是1个正在进行的任务,不可以清除,第2个例子中有用到
FLAG_NO_CLEAR:不可以清除
实例来源:
http://www.apkbus.com/forum.php?mod=viewthread&tid=20486
参考:http://www.apkbus.com/forum.php?mod=viewthread&tid=19156

Example2:

Button btn4 = (Button) this.findViewById(R.id.btn4);btn4.setText("发出一个通知(Notification)");btn4.setOnClickListener(new Button.OnClickListener() {public void onClick(View v) {// 实例化通知管理器NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);// 指定单击通知后所打开的详细的通知页面(单击通知后打开 NotificationView)PendingIntent contentIntent = PendingIntent.getActivity(Main.this, 0, new Intent(Main.this,    NotificationView.class), 0);// 实例化一个通知,并指定其图标和标题(在提示栏上显示)Notification n = new Notification(R.drawable.icon01, "我是滚动的通知信息我是滚动的通知信息我是滚动的通知信息", System.currentTimeMillis());// 设置通知的发送人和通知的详细内容(打开提示栏后在通知列表中显示)n.setLatestEventInfo(Main.this, "通知发送人", "我是详细的通知信息我是详细的通知信息我是详细的通知信息", contentIntent);// 100 毫秒延迟后,震动 250 毫秒,暂停 100 毫秒后,再震动 500 毫秒n.vibrate = new long[] { 100, 250, 100, 500 };// 发出通知(其中第一个参数为通知标识符)nm.notify(0, n);}});  
// 单击通知列表的某个通知后,所打开的详细的通知页
public class NotificationView extends Activity {protected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.view);TextView txtMsg = (TextView)this.findViewById(R.id.txtMsg);txtMsg.setText("点通知之后所链接到的 Activity");NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);// 取消显示在通知列表中的指定通知(参数为通知标识符)nm.cancel(0);// 需要关闭此 Activity 的话就 finish 它既可// this.finish();}
}

Example3:

public class NotificationActivity extends Activity implements OnClickListener {@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.notification);findViewById(R.id.notificationBtn).setOnClickListener(this);}@Overridepublic void onClick(View arg0) {// TODO Auto-generated method stubaddNotification();}private void addNotification() {NotificationManager manager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);Notification notification = new Notification();notification.icon = R.drawable.icon;notification.tickerText = "我在这里";notification.defaults = Notification.DEFAULT_SOUND;notification.audioStreamType = android.media.AudioManager.ADJUST_LOWER;Intent intent = new Intent(this, Notification2Activity.class);PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT);notification.setLatestEventInfo(this, "短信通知", "亲爱的,晚上8点老地方见哦~", pendingIntent);manager.notify(R.drawable.icon, notification);}
}
public class Notification2Activity extends Activity implements OnClickListener {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.notification2);findViewById(R.id.cancleBtn).setOnClickListener(this);}@Overridepublic void onClick(View v) {// TODO Auto-generated method stubcancleNotification();}private void cancleNotification() {// TODO Auto-generated method stubNotificationManager manager =  (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);manager.cancel(R.drawable.icon);Toast.makeText(this, "Notification cancled", Toast.LENGTH_SHORT).show();}}

都是很简单的例子,帮助理解!

转自:http://uule.iteye.com/blog/1706092

Android中的通知Notification相关推荐

  1. Android — 在服务中发送通知Notification

    1.在服务中,发送通知的方法 private void sendNotification(String title,String content,StateBean stateBean){Contex ...

  2. Android之状态栏通知Notification、NotificationManager详解

    在Android系统中,发一个状态栏通知还是很方便的.下面我们就来看一下,怎么发送状态栏通知,状态栏通知又有哪些参数可以设置? 首先,发送一个状态栏通知必须用到两个类:  NotificationMa ...

  3. 【Android】状态栏通知Notification、NotificationManager详解

    在Android系统中,发一个状态栏通知还是很方便的.下面我们就来看一下,怎么发送状态栏通知,状态栏通知又有哪些参数可以设置? 首先,发送一个状态栏通知必须用到两个类: NotificationMan ...

  4. Android中使用通知

    首先打开手机的开发者选项中的USB调试 使用通知 通知是Android系统中比较有特色的一个功能,当某个应用程序希望用户发出一些提示信息,而应用程序不在前台运行时,就可以借助通知来实现.发出一条通知后 ...

  5. android notification自动消失,Android开发 -- 状态栏通知Notification、NotificationManager详解...

    本想自己写一个的,但是看到这篇之后,我想还是转过来吧,实在是非常的详细: 在Android系统中,发一个状态栏通知还是很方便的.下面我们就来看一下,怎么发送状态栏通知,状态栏通知又有哪些参数可以设置? ...

  6. android+图标闪烁动画,如何在android中闪烁通知图标? [DONE]

    我找到了解决方案:我创建了动画文件并设置了该文件,感谢我找到了解决方案 SET FILE名称在这里: – int icon = R.drawable.animationfile; long when ...

  7. Android中pendingIntent的深入理解

    pendingIntent字面意义:等待的,未决定的Intent. 要得到一个pendingIntent对象,使用方法类的静态方法 getActivity(Context, int, Intent, ...

  8. [转]Android中pendingIntent的深入理解

    转自;here pendingIntent字面意义:等待的,未决定的Intent. 要得到一个pendingIntent对象,使用方法类的静态方法 getActivity(Context, int, ...

  9. firebase 云推送_使用Firebase云消息传递在Android中推送通知

    firebase 云推送 这篇文章介绍了如何在Android中发送推送通知 . 过去,我们曾经使用Google Cloud消息传递服务在Android中发送推送通知. 最近,它引入了一种使用Fireb ...

最新文章

  1. opencv颜色空间缩减
  2. python数据拟合fit
  3. [转] 前端数据驱动的价值
  4. python编程入门教学下载-Python编程从入门到实践的PDF教程免费下载
  5. openssl 开启AES-NI指令集性能增加
  6. ASP.NET MVC中权限控制的简单实现
  7. ik分词器 mysql php_php环境下使用elasticSearch+ik分词器进行全文搜索
  8. java android上传文件_Java-Android-如何将txt文件上传到网站?
  9. 抢那么多封面,有那么多钱发红包吗?
  10. 6折入股蚂蚁金服?巨人网络如此回应
  11. 15数码 java_A*算法求解15数码问题
  12. DxO PhotoLab 3 for Mac(照片后期处理软件)
  13. 微信小程序支付和退款(微信公众号和微信小程序支付和退款属于一个爹妈)
  14. C# Socket实现两台电脑通信(三)
  15. 如何屏幕高清录像?--QVE屏幕录制
  16. Zabbix 5.0 监控教程(一)
  17. 《二叉平衡树(一)》
  18. HDMI是什么设备与计算机连接的接口类型,​hdmi接口有什么用?怎么连接电脑?vga和hdmi的区别...
  19. 0622_ArcMap添加地图地图(矢量底图与影像地图)_太乐地图插件ArcTailer.tlb
  20. SQL Server 详细安装教程

热门文章

  1. 单元测试的重要性【转自”至简李云“博客】
  2. 程序设计、数据结构、编译相关图灵奖得主简介之二
  3. 二手车 电商+互联网金融的三种新玩法
  4. 计算机的随想作文500字,新年随想作文500字(通用5篇)
  5. Manifest merger failed with multiple errors, see logs解决方案
  6. Direct3D(D3D)简介
  7. 一个5位数,判断它是不是回文数。即12321是回文数,个位与万位相同,十位与千位相同。
  8. 如何查询网站虚拟服务器的ip,正确姿势查看海外虚拟主机真实IP地址 cPanel面板IP信息...
  9. 操作系统精选习题——第二章
  10. Cross Stage Partial Network(CSPNet)