notification

 

 

//获取NotificationManager服务
NotificationManager nm=(NotificationManager) getSystemService(NOTIFICATION_SERVICE);
//实例化一个Notification
Notification notification=new Notification(R.drawable.ic_launcher,"通知来了",System.currentTimeMillis());
//定义通知时的信息和意图
Intent intent=new Intent(this,MainActivity.class);
PendingIntent contentIntent=PendingIntent.getActivity(getApplicationContext(), 100, intent, PendingIntent.FLAG_CANCEL_CURRENT);
notification.setLatestEventInfo(getApplicationContext(), "通知", "下拉拉拉查看" ,contentIntent);
notification.ledARGB=Color.BLUE;//led灯颜色
notification.ledOffMS=100;//关闭时间 毫秒
notification.ledOnMS=100;//开启时间 毫秒
notification.vibrate=new long[]{0,100,100,100,100,100,100,100};//停震停震停震停震
notification.flags|=Notification.DEFAULT_SOUND;//使用系统默认通知声音
notification.flags|=Notification.FLAG_AUTO_CANCEL;//点击通知后自动清除
notification.flags|=Notification.FLAG_NO_CLEAR;//无法通过清除按钮清除
//向NotificationManager传送通知
nm.notify(10,notification);

 

Notification的使用
1.基本步骤
step1.获取NotificationManager对象
通过getSystemService(String)获得NotificationManager

String str = Context.NOTIFICATION_SERVICE;
NotificationManager nm = (NotificationManager) getSystemService(str);


step2.创建Notification对象

String tickerText = "这是我的第一个notification测试程序,如有不足,请多多包含。";
long when = System.currentTimeMillis();
Notification notification = new Notification(R.drawable.ic_launcher, tickerText, when);


step3.定义Notification的message和PendingIntent

Intent intent = new Intent();
intent.setClass(getApplicationContext(), MainActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(getApplicationContext(), 0, intent, 0);
notification.setLatestEventInfo(getApplicationContext(), "notification测试软件", "此软件为最新版", contentIntent);


step4.通过NotificationManager的notify(int , Notification)方法发送

nm.notify(1, notification);

 

其他特效
1.使用默认声音

notification.defaults |= Notification.DEFAULT_SOUND;

 


2.指定一个文件,作为声音

notification.sound = Uri.parse("file:///mnt/sdcard/aa.mp3");

 


3.当点击notification window时,清除notification信息

notification.flags |= Notification.FLAG_AUTO_CANCEL;

 


4.不希望被从notification window清除

notification.flags加如Notification.FLAG_AUTO_CANCEL;

 


5.手机震动效果
需要声明一个权限:

<uses-permission android:name="android.permission.VIBRATE"/>

 


1)使用系统默认的震动效果

notification.defaults |= Notification.DEFAULT_VIBRATE;

 


2)使用自定义的震动效果 

notification.vibrate=new long[]{0,100,100,100,100,100,100,100};//停震停震停震停震

 

posted on 2012-12-18 09:25  @与非  阅读(1245)  评论(0编辑  收藏  举报