首页 > 移动平台 > 详细

安卓开发之通知的使用(notification)

时间:2015-12-20 18:59:48      阅读:260      评论:0      收藏:0      [点我收藏+]

看第一行代码采用了如下的方法调用通知:

NotificationManager manager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
Notification notification = new Notification(R.drawable.ic_launcher, "This is a ticker text", System.currentTimeMillis());
notification.setLatestEventInfo(this, "This is content title", "This is content text", null);
manager.notify(1, notification);

现在notification方法已经过时失效,查看api看到如下方法:

NotificationManager manager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
Notification notification = new Notification.Builder(MainActivity.this)
.setContentTitle("this is a title")
.setContentText("this is content")
.setSmallIcon(R.drawable.ic_launcher)
.build();
manager.notify(1, notification);

点击通知触发事件通过PendingIntent实现

Intent intent = new Intent(MainActivity.this, NotificationActivity.class);
PendingIntent pi = PendingIntent.getActivity(MainActivity.this, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);

.setContentIntent(pi)

通过通知可以控制手机播放音乐或者振动

音乐:

// Uri soundUri = Uri.fromFile(new File("/system/media/audio/ringtones/Banjo.ogg"));  //"/system/media/audio/ringtones/Banjo.ogg"许多系统自带铃声的路径
// notification.sound = soundUri;

振动:

添加权限 <uses-permission android:name="android.permission.VIBRATE" />

long[] vibrates = {0, 1000, 1000, 1000};
notification.vibrate = vibrates;

振动代码,但是测试没有效果。。。

LED控制(现在很多手机都没有灯了吧)

notification.ledARGB = Color.GREEN;
notification.ledOnMS = 1000;
notification.ledOffMS = 1000;
notification.flags = Notification.FLAG_SHOW_LIGHTS;

通过以下方法调用系统默认的通知设置

notification.defaults = Notification.DEFAULT_ALL;

 

安卓开发之通知的使用(notification)

原文:http://www.cnblogs.com/xuehe/p/5061412.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!