看第一行代码采用了如下的方法调用通知:
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;
原文:http://www.cnblogs.com/xuehe/p/5061412.html