标题栏不显示原因
#背景:
当前在写一个音乐播放器练手,发现手上的华为手机就是不显示Notification,但是手上的其他2台小米手机可以显示。
#分析:
开始以为是华为定制系统的适配的问题。
直到我在简书上看到这篇文章的这个点时,我忽然想起我的另外2台小米手机分别是Android 6.0 和 Android 7.0 的系统,而我的华为手机是Android 8.0 系统。这才想起是不是 8.0适配的问题。。。哎,先入为主的思想确实该改改了。
先看看你的Android studio 中 App目录下的build.gradle中的SDK的参数是多少。
先说说明一下这几个参数的作用:
compileSdkVersion:编译SDK版本
minSdkVersion:最低SDK版本
targetSdkVersion:目标SDK版本
其中compileSdkVersion要和targetSdkVersion数值一致,不然会报错。
26对应的是O版本(即Android 8.0 ),而在8.0中,假如这样创建Notification,标题栏是不会显示的(我现在手上只有一台8.0 的华为荣耀V10,其他8.0机型未查看现象,如果有出入还望各位指正)。
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
showNotification();
//showNot2();
}
private void showNotification() {
Log.d(TAG, "showNotification");
NotificationManager notificationManager =
(NotificationManager) (this.getSystemService(Context.NOTIFICATION_SERVICE));
NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
builder.setLargeIcon(BitmapFactory.decodeResource(this.getResources(), R.drawable.ic_launcher_background));
builder.setSmallIcon(R.drawable.ic_launcher_background);
builder.setContentTitle(this.getText(R.string.app_name));
String strProgress = this.getResources().getString(R.string.app_name);
builder.setContentText(strProgress);
Notification notification = builder.build();
notification.flags = Notification.FLAG_ONGOING_EVENT;
notificationManager.notify(1, notification);
}
#解决方式
compileSdkVersion,targetSdkVersion的数值设置为25或以下,使用上面这段代码的方式创建的Notification就能显示了。
当然,denpendencies里面的依赖包对应也要从26及以上修改为25及以下的版本,然后再同步。同步的时候,会报错,这个各位自己搞吧。你重新写一个Demo会发现就是Android版本适配的问题,与手机厂商无关。
#补充
上面例子(例子很简单,有需要的可以看看):https://github.com/stoneWangL/NotificationTest
如果你想做一些个性化的标题栏,比如说音乐播放器的标题栏,可以使用RemoteViews 来实现。这个网上就有很多例子了,自己去搜索一下吧。
真正原因
Notification在android 8.0以上设置时,需要设置Channel
参考来源:https://blog.csdn.net/qq_35749683/article/details/80451791
下面是我自己的记录
private void initNotificationSon() {
// NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(MusicApplication.getContext());
//
// //mBuilder.setSmallIcon(R.drawable.play_background02); // 设置顶部图标
// Bitmap icBitmap = BitmapFactory.decodeResource(getResources(),R.drawable.play_background02);
// mBuilder.setLargeIcon(icBitmap);
// mBuilder.setOngoing(true);
// mBuilder.setPriority(Notification.PRIORITY_MAX);
//
//
// notification = mBuilder.build();//构建通知
// setNotification();
// notification.contentView = remoteViews; // 设置下拉图标
// notification.bigContentView = remoteViews; // 防止显示不完全,需要添加apiSupport
// notification.flags = Notification.FLAG_ONGOING_EVENT;
// notification.icon = R.drawable.anim_log;
//
// startForeground(1, notification);//启动为前台服务
String id = "stoneMusic_channel";
String name="stoneMusic";
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(MusicApplication.getContext());
Bitmap icBitmap = BitmapFactory.decodeResource(getResources(),R.drawable.play_background02);
mBuilder.setLargeIcon(icBitmap);
mBuilder.setOngoing(true);
mBuilder.setPriority(Notification.PRIORITY_MAX);
mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel mChannel =
new NotificationChannel(id, name, NotificationManager.IMPORTANCE_LOW);
mNotificationManager.createNotificationChannel(mChannel);
Notification.Builder builder = new Notification.Builder(this , id);
notification = builder.build(); //构建通知
setNotification();
notification.contentView = remoteViews; // 设置下拉图标
notification.bigContentView = remoteViews; // 防止显示不完全,需要添加apiSupport
notification.flags = Notification.FLAG_ONGOING_EVENT;
notification.icon = R.drawable.anim_log;
mNotificationManager.notify(123, notification);//显示通知
} else {
notification = mBuilder.build();//构建通知
setNotification();
notification.contentView = remoteViews; // 设置下拉图标
notification.bigContentView = remoteViews; // 防止显示不完全,需要添加apisupport
notification.flags = Notification.FLAG_ONGOING_EVENT;
notification.icon = R.drawable.anim_log;
mNotificationManager.notify(123, notification);//显示通知
// startForeground(123, notification);//启动为前台服务
}
}