当需要实现用户点击组件,系统就发送一条消息并在状态栏中提醒时。可使用PendingIntent实现这一效果:
private void notice(){
Intent intent = new Intent(this,PendingActivity.class);//即跳转到本类
PendingIntent pendingIntent = PendingIntent.getActivity(this,0,intent,PendingIntent.FLAG_UPDATE_CURRENT);
NotificationManager nm = (NotificationManager) getSystemService(Activity.NOTIFICATION_SERVICE);
Notification notification = new NotificationCompat.Builder(this)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentInfo("我是消息")
.setContentTitle("我是标题")
.setContentText("我是内容")
.setAutoCancel(true)
.setDefaults(Notification.DEFAULT_ALL)
.setContentIntent(pendingIntent)
.build();
nm.notify(0,notification);
}