使用通知
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 使用pendingIntent 让通知可点击
Intent intent = new Intent(this, Notification.class);
PendingIntent pi = PendingIntent.getActivities(this, 0, new Intent[] { intent }, 0);
// 1创建NotificationManager 管理通知
NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
// 2创建notification 对象
Notification notification = new Notification.Builder(this)
.setContentTitle("标题")
.setContentText("内容")
.setWhen(System.currentTimeMillis()) //通知被创建的时间"
.setSmallIcon(R.drawable.ic_launcher_background)
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher_background))
.setContentIntent(pi) // 把pendingIntent 的实例传入
.setAutoCancel(true) // 通知点击查看后自动关闭消息提示
.setSound(Uri.fromFile(new File("/system/media/audio/ringtones/播放音频文件")))
.setVibrate(new long[] { 0, 1000, 1000, 1000 }) // 通知时候震动和静止时长 需要在manifest里声明 permission.VIBRATE权限
.setLights(Color.GREEN, 1000, 1000) // 呼吸灯显示
.setDefaults(NotificationCompat.DEFAULT_ALL) // 使用手机默认的声音 震动 呼吸灯提示
.setStyle(new Notification.BigTextStyle().bigText("设置长文本数据显示"))
.setStyle(new Notification.BigPictureStyle().bigPicture(BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher_background))) // 设置大图片显示
.setPriority(Notification.PRIORITY_MAX) // 设置通知的重要程度
.build();
// 3显示通知 每个通知都有唯一的id,对象
manager.notify(1, notification);
//manager.cancel(1); 另一种方式关闭消息通知 1表示通知的id
}
}
Notification 通知
最新推荐文章于 2021-05-13 12:52:52 发布