概述
Android 在API26之后NotificationCompat.Builder (Context context)被废弃,创建通知需要使用新的构造器NotificationCompat.Builder (Context context, String channelId)
so:
NotificationManager manager = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE); String CHANNEL_ID = "com.example.test";//自定义的字符串 String CHANNEL_NAME = "TEST"; NotificationChannel notificationChannel = null; NotificationCompat.Builder builder = null;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { notificationChannel = new NotificationChannel(CHANNEL_ID, CHANNEL_NAME, NotificationManager.IMPORTANCE_HIGH); NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); notificationManager.createNotificationChannel(notificationChannel); } Intent intent = new Intent(this, HomeActivity.class); PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
builder = new NotificationCompat.Builder(this, CHANNEL_ID). setContentTitle(getResources().getString(R.string.app_name)). setContentText(getResources().getString(R.string.notice_001)). setWhen(System.currentTimeMillis()). setSmallIcon(R.mipmap.app_icon). setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.app_icon)). setContentIntent(pendingIntent).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(getResources().getColor(R.color.white)); Intent intent2 = new Intent(); PendingIntent pi = PendingIntent.getBroadcast(this, 0, intent2, 0); builder.setFullScreenIntent(pi, true); } Notification notification = builder.build(); manager.notify(1, notification);
最后
以上就是慈祥篮球为你收集整理的Android 创建悬停通知栏的全部内容,希望文章能够帮你解决Android 创建悬停通知栏所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复