华为手机 标题栏 Notification 8.0 不显示
標題欄不顯示原因
#背景:
當前在寫一個音樂播放器練手,發現手上的華為手機就是不顯示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機型未查看現象,如果有出入還望各位指正)。
@Overrideprotected 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; // 防止顯示不完全,需要添加apiSupportnotification.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; // 防止顯示不完全,需要添加apisupportnotification.flags = Notification.FLAG_ONGOING_EVENT;notification.icon = R.drawable.anim_log;mNotificationManager.notify(123, notification);//顯示通知 // startForeground(123, notification);//啟動為前臺服務}}?
總結
以上是生活随笔為你收集整理的华为手机 标题栏 Notification 8.0 不显示的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 微信公众号代运营的的技巧有哪些(2)
- 下一篇: 红亚太学链微信公众号正式发布