赞
踩
这是一篇关于 Android 通知栏的记录。包括:通知栏消息、点亮屏幕、震动、声音、显示样式等。下面是简单的效果图:
下面主要讲的是:
- <uses-permission android:name="android.permission.WAKE_LOCK" />
- <uses-permission android:name="android.permission.DISABLE_KEYGUARD" />
- /**
- * author: wu
- * date: on 2018/11/14.
- * describe: 静态常量类
- */
- public class Constant {
- public static Integer TYPE1 = 1;
- public static Integer TYPE2 = 2;
- public static Integer TYPE3 = 3;
- }
内容比较简单,直接给出代码,复制即可用,xml 文件非常简单,只有三个 TextView,在这里就不再给出:
- public class MainActivity extends AppCompatActivity {
-
- private TextView tv_1, tv_2, tv_3;
- private Notification notification;
- private NotificationCompat.Builder builder;
- private NotificationManager manager;
-
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- tv_1 = findViewById(R.id.tv_1);
- tv_2 = findViewById(R.id.tv_2);
- tv_3 = findViewById(R.id.tv_3);
-
- //短文本,点亮屏幕显示
- tv_1.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View view) {
- //3秒后发通知,这样可以模仿锁屏,点亮屏幕
- Handler handler = new Handler();
- handler.postDelayed(new Runnable() {
- @Override
- public void run() {
- setNotification(1);
- //唤醒屏幕并解锁
- wakeUpAndUnlock(MainActivity.this);
- }
- }, 3000);
- }
- });
- //长文本
- tv_2.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View view) {
- setNotification(2);
- }
- });
- //带图片
- tv_3.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View view) {
- setNotification(3);
- }
- });
- }
-
- //设置通知栏消息样式
- private void setNotification(int type) {
- //点击通知栏消息跳转页
- Intent intent = new Intent(this, NotificationDetailsActivity.class);
- PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
- //创建通知消息管理类
- manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
- builder = new NotificationCompat.Builder(this)//创建通知消息实例
- .setContentTitle("我是标题")
- .setContentText("我是内容")
- .setWhen(System.currentTimeMillis())//通知栏显示时间
- .setSmallIcon(R.mipmap.ic_launcher)//通知栏小图标
- .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher))//通知栏下拉是图标
- .setContentIntent(pendingIntent)//关联点击通知栏跳转页面
- .setPriority(NotificationCompat.PRIORITY_MAX)//设置通知消息优先级
- .setAutoCancel(true)//设置点击通知栏消息后,通知消息自动消失
- // .setSound(Uri.fromFile(new File("/system/MP3/music.mp3"))) //通知栏消息提示音
- .setVibrate(new long[]{0, 1000, 1000, 1000}) //通知栏消息震动
- .setLights(Color.GREEN, 1000, 2000) //通知栏消息闪灯(亮一秒间隔两秒再亮)
- .setDefaults(NotificationCompat.DEFAULT_ALL); //通知栏提示音、震动、闪灯等都设置为默认
-
- if (type == 1) {
- //短文本
- notification = builder.build();
- //Constant.TYPE1为通知栏消息标识符,每个id都是不同的
- manager.notify(Constant.TYPE1, notification);
- } else if (type == 2) {
- //长文本
- notification = builder.setStyle(new NotificationCompat.BigTextStyle().
- bigText("我是长文字内容: 今年双十一结束后,一如既往又出现了一波冲动剁手党被理智唤醒的退货潮。不过,一位来自福建厦门的网友在这其中贡献了堪称历史里程碑式的高光时刻。别人退衣服退鞋子,而他要退的是一只蓝孔雀、一只宠物小香猪、还有一斤娃娃鱼……"))
- .build();
- manager.notify(Constant.TYPE2, notification);
- } else {
- //带图片
- notification = builder.setStyle(new NotificationCompat.BigPictureStyle().
- bigPicture(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher)))
- .build();
- manager.notify(Constant.TYPE3, notification);
- }
- }
-
- //唤醒屏幕并解锁
- public void wakeUpAndUnlock(Context context) {
- KeyguardManager km = (KeyguardManager) context.getSystemService(Context.KEYGUARD_SERVICE);
- KeyguardManager.KeyguardLock kl = km.newKeyguardLock("unLock");
- //解锁
- kl.disableKeyguard();
- //获取电源管理器对象
- PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
- //获取PowerManager.WakeLock对象,后面的参数|表示同时传入两个值,最后的是LogCat里用的Tag
- PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.ACQUIRE_CAUSES_WAKEUP | PowerManager.SCREEN_DIM_WAKE_LOCK, "bright");
- //点亮屏幕
- wl.acquire();
- //释放
- wl.release();
- }
- }
- /**
- * author: wu
- * date: on 2018/11/14.
- * describe:点击通知栏跳转过来的页面
- */
- public class NotificationDetailsActivity extends AppCompatActivity {
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- TextView textView = new TextView(this);
- textView.setText("我是通知详情");
- setContentView(textView);
- // 当点击通知消息跳转到详情页时根据通知消息的id关闭通知栏的显示
- NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
- manager.cancel(Constant.TYPE1);
- manager.cancel(Constant.TYPE2);
- manager.cancel(Constant.TYPE2);
-
- }
- }
到此就可以完成效果图中的效果了。
Android8.0以上要设置 NotificationChannel 才可以:
- private void setNotification(int type) {
- //点击通知栏消息跳转页
- manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
- Intent intent = new Intent(this,MainActivity.class);
- PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
- Notification notification;
- Notification.Builder builder;
- if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
- builder=new Notification.Builder(this,"5996773");
- }else {
- builder=new Notification.Builder(this);
- }
- //设置标题
- builder.setContentTitle("设置标题");
- //设置内容
- builder.setContentText("内容是............");
- //设置状态栏显示的图标,建议图标颜色透明
- builder.setSmallIcon(R.mipmap.ic_launcher);
- // 设置通知灯光(LIGHTS)、铃声(SOUND)、震动(VIBRATE)、(ALL 表示都设置)
- // builder.setDefaults(Notification.DEFAULT_ALL);
- //灯光三个参数,颜色(argb)、亮时间(毫秒)、暗时间(毫秒),灯光与设备有关
- builder.setLights(Color.RED, 200, 200);
- // 铃声,传入铃声的 Uri(可以本地或网上)我这没有铃声就不传了
- builder.setSound(Uri.parse("")) ;
- // 震动,传入一个 long 型数组,表示 停、震、停、震 ... (毫秒)
- builder.setVibrate(new long[]{0, 200, 200, 200, 200, 200});
- // 通知栏点击后自动消失
- builder.setAutoCancel(true);
- // 简单通知栏设置 Intent
- builder.setContentIntent(pendingIntent);
- builder.setPriority(Notification.PRIORITY_HIGH);
-
- //设置下拉之后显示的图片
- builder.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher_background));
- if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
- NotificationChannel channel = new NotificationChannel("5996773", "安卓10a", NotificationManager.IMPORTANCE_DEFAULT);
- channel.enableLights(true);//是否在桌面icon右上角展示小红点
- channel.setLightColor(Color.GREEN);//小红点颜色
- channel.setShowBadge(false); //是否在久按桌面图标时显示此渠道的通知
- manager.createNotificationChannel(channel);
- }
- notification=builder.build();
- manager.notify(1,notification);
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。