1.介绍
通知是android系统中比较有特色的功能之一,当某个应用程序希望向用户发出一些提示信息的时候,而应用程序又不在前台运行,可以借助通知来实现。发出一条通知后手机通知栏会显示一条通知消息。
通知可以在活动、广播、服务里创建。虽然通知在活动里创建比较少,但是为了演示方便还是选择了在活动里创建。
2.举例
2.1 发送通知
点击发送通知。
NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
Notification notification = new NotificationCompat.Builder(MainActivity.this)
.setContentTitle("标题")//标题
.setContentText("内容")//内容
.setWhen(System.currentTimeMillis())//通知被创建的时间
.setSmallIcon(R.mipmap.ic_launcher)//小图标
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher))//大图标
.build();
manager.notify(1, notification);//id 1
仅仅是发送一条通知。
2.2发送通知点击通知跳转
当收到通知后,点击通知跳转到对应的目录
Intent intent = new Intent(MainActivity.this, Main2Activity.class);
//第一个参数 上下文
//第二个参数 默认0
//第三个参数 Intent
//第四个参数 行为 FLAG_ONE_SHOT FLAG_NO_CARATE FLAG_CANCEL_CURRENT FLAG_UPDATE_CURRENT
PendingIntent pendingIntent = PendingIntent.getActivity(MainActivity.this, 0, intent, 0);
NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
Notification notification = new NotificationCompat.Builder(MainActivity.this)
.setContentTitle("标题")//标题
.setContentText("内容")//内容
.setWhen(System.currentTimeMillis())//通知被创建的时间
.setSmallIcon(R.mipmap.ic_launcher)//小图标
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher))//大图标
.setContentIntent(pendingIntent)//点击挑转
.build();
manager.notify(2, notification);//id 2
跳转完了,通知还保留着。
2.3清除通知
清除通知有两种方法,一种是 点击通知后自动取消:
Intent intent = new Intent(MainActivity.this, Main2Activity.class);
//第一个参数 上下文
//第二个参数 默认0
//第三个参数 Intent
//第四个参数 行为 FLAG_ONE_SHOT FLAG_NO_CARATE FLAG_CANCEL_CURRENT FLAG_UPDATE_CURRENT
PendingIntent pendingIntent = PendingIntent.getActivity(MainActivity.this, 0, intent, 0);
NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
Notification notification = new NotificationCompat.Builder(MainActivity.this)
.setContentTitle("标题")//标题
.setContentText("内容")//内容
.setWhen(System.currentTimeMillis())//通知被创建的时间
.setSmallIcon(R.mipmap.ic_launcher)//小图标
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher))//大图标
.setContentIntent(pendingIntent)//点击挑转
.setAutoCancel(true)//点击通知后自动取消 或者从跳转的activity取消
.build();
manager.notify(3, notification);//id 3
另外一种是跳转到对应的活动后取消:
Intent intent = new Intent(MainActivity.this, Main2Activity.class);
//第一个参数 上下文
//第二个参数 默认0
//第三个参数 Intent
//第四个参数 行为 FLAG_ONE_SHOT FLAG_NO_CARATE FLAG_CANCEL_CURRENT FLAG_UPDATE_CURRENT
PendingIntent pendingIntent = PendingIntent.getActivity(MainActivity.this, 0, intent, 0);
NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
Notification notification = new NotificationCompat.Builder(MainActivity.this)
.setContentTitle("标题")//标题
.setContentText("内容")//内容
.setWhen(System.currentTimeMillis())//通知被创建的时间
.setSmallIcon(R.mipmap.ic_launcher)//小图标
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher))//大图标
.setContentIntent(pendingIntent)//点击挑转
.build();
manager.notify(4, notification);//id 4
对应的Main2Activity:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
// manager.notify(4, notification);//id 4
manager.cancel(4);//
}
中的 manager.cancel(4);4就是是在跳转的时候配置的id。
2.4播放音乐
播放本地的音乐
NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
Notification notification = new NotificationCompat.Builder(MainActivity.this)
.setContentTitle("标题")//标题
.setContentText("薛之谦-怪咖-最好")//内容
.setWhen(System.currentTimeMillis())//通知被创建的时间
.setSmallIcon(R.mipmap.ic_launcher)//小图标
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher))//大图标
.setSound(Uri.fromFile(new File("/storage/emulated/0/Baidu_music/download/薛之谦-怪咖-最好-128.mp3")))
.build();
manager.notify(5, notification);//id 5
2.5伴随震动
当收到通知的时候震动
NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
Notification notification = new NotificationCompat.Builder(MainActivity.this)
.setContentTitle("标题")//标题
.setContentText("震动")//内容
.setWhen(System.currentTimeMillis())//通知被创建的时间
.setSmallIcon(R.mipmap.ic_launcher)//小图标
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher))//大图标
.setVibrate(new long[]{0, 1000, 1000, 1000})//静止 0 ;震动 1000;静止1000;震动1000
.build();
manager.notify(6, notification);//id 6
2.6伴随提示灯
当收到通知的时候锁屏状态下显示呼吸灯
NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
Notification notification = new NotificationCompat.Builder(MainActivity.this)
.setContentTitle("标题")//标题
.setContentText("灯")//内容
.setWhen(System.currentTimeMillis())//通知被创建的时间
.setSmallIcon(R.mipmap.ic_launcher)//小图标
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher))//大图标
.setLights(Color.GREEN, 1000, 1000)//灯的颜色 亮起时长,暗去时长
.build();
manager.notify(7, notification);//id 7
2.7跟随系统默认
默认和系统一致,震动、呼吸灯
NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
Notification notification = new NotificationCompat.Builder(MainActivity.this)
.setContentTitle("标题")//标题
.setContentText("默认")//内容
.setWhen(System.currentTimeMillis())//通知被创建的时间
.setSmallIcon(R.mipmap.ic_launcher)//小图标
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher))//大图标
.setDefaults(NotificationCompat.DEFAULT_ALL)//根据手机的环境来决定
.build();
manager.notify(8, notification);//id 8
2.8显示长文本
当通知文本长的时候显示不全,显示全部文本。
NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
Notification notification = new NotificationCompat.Builder(MainActivity.this)
.setContentTitle("标题")//标题
// .setContentText("长文本长文本长文本长文本长文本长文本长文本长文本长文本长文本长文本长文本长文本长文本长文本长文本长文本长文本长文本长文本长文本长文本长文本123")//内容
.setWhen(System.currentTimeMillis())//通知被创建的时间
.setSmallIcon(R.mipmap.ic_launcher)//小图标
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher))//大图标
.setDefaults(NotificationCompat.DEFAULT_ALL)//根据手机的环境来决定
.setStyle(new NotificationCompat.BigTextStyle().bigText("长文本长文本长文本长文本长" +
"文本长文本长文本长文本长文本长文 本长文本长文本长文本长文本长文本长文本长文" +
"本长文本长文本长文本长文本长文本长文本123"
))
.build();
manager.notify(9, notification);//id 9
2.9显示大图
加载通知中的大图
NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
Notification notification = new NotificationCompat.Builder(MainActivity.this)
.setContentTitle("标题")//标题
// .setContentText("长文本长文本长文本长文本长文本长文本长文本长文本长文本长文本长文本长文本长文本长文本长文本长文本长文本长文本长文本长文本长文本长文本长文本123")//内容
.setWhen(System.currentTimeMillis())//通知被创建的时间
.setSmallIcon(R.mipmap.ic_launcher)//小图标
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher))//大图标
.setDefaults(NotificationCompat.DEFAULT_ALL)//根据手机的环境来决定
.setStyle(new NotificationCompat.BigTextStyle().bigText("长文本长文本长文本长文本长" +
"文本长文本长文本长文本长文本长文 本长文本长文本长文本长文本长文本长文本长文" +
"本长文本长文本长文本长文本长文本长文本123"
))
.setStyle(new NotificationCompat.BigPictureStyle().bigPicture(BitmapFactory.
decodeResource(getResources(),R.drawable.pig)))
.build();
manager.notify(10, notification);//id 10
2.10最高优先级显示
显示在最上面
NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
Notification notification = new NotificationCompat.Builder(MainActivity.this)
.setContentTitle("标题")//标题
.setContentText("最高优先级")//内容
.setWhen(System.currentTimeMillis())//通知被创建的时间
.setSmallIcon(R.mipmap.ic_launcher)//小图标
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher))//大图标
// .setDefaults(NotificationCompat.DEFAULT_ALL)//根据手机的环境来决定
.setPriority(NotificationCompat.PRIORITY_MAX)
.build();
manager.notify(11, notification);//id 11
转发表明出处:https://blog.csdn.net/qq_35698774/article/details/107051528
点击下载源码
android互助群:
感谢:郭霖的《第一行代码 第二版》