Android定义一个不消失的悬停通知栏
16lz
2021-01-23
private NotificationManager manager = null;private Notification.Builder builder = null;
private void showNotification(Context context) { manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); builder = new Notification.Builder(context); builder.setShowWhen(false) .setSmallIcon(R.drawable.ic_power_low) .setLargeIcon(BitmapFactory.decodeResource(context.getResources(), R.drawable.ic_power_low)) .setContentTitle("自定义标题") .setContentText("自定义内容") .setDefaults(NotificationCompat.FLAG_ONGOING_EVENT) .setPriority(Notification.PRIORITY_MAX); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {//SDK版本>=21才能设置悬挂式通知栏 builder.setCategory(String.valueOf(Notification.FLAG_ONGOING_EVENT)) .setVisibility(Notification.VISIBILITY_PUBLIC) .setColor(context.getResources().getColor(R.color.salmon)); Intent intent = new Intent(); PendingIntent pi = PendingIntent.getBroadcast(this, 0, intent, 0); builder.setFullScreenIntent(pi, true); manager.notify(null, 0, builder.build()); }}
private void cancelNotification() { if (manager != null) { manager.cancel(0); }}
更多相关文章
- 查看当前android设备api版本
- Android 创建没有标题栏的对话框
- [Android] 如何取得版本号码
- APP启动时白屏或出现标题
- Android Q 版本新的网络状况判断方式
- ndk 历史版本下载
- Android 更新升级版本
- Cordova + vue 打包安卓(Android) apk 及 Apk版本自动升级及安装
- 定制Android版本的chromium之笔记