日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

华为手机 标题栏 Notification 8.0 不显示

發(fā)布時(shí)間:2024/1/8 编程问答 40 豆豆
生活随笔 收集整理的這篇文章主要介紹了 华为手机 标题栏 Notification 8.0 不显示 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

標(biāo)題欄不顯示原因

#背景:

當(dāng)前在寫一個(gè)音樂播放器練手,發(fā)現(xiàn)手上的華為手機(jī)就是不顯示Notification,但是手上的其他2臺(tái)小米手機(jī)可以顯示。

#分析:

開始以為是華為定制系統(tǒng)的適配的問題。

直到我在簡(jiǎn)書上看到這篇文章的這個(gè)點(diǎn)時(shí),我忽然想起我的另外2臺(tái)小米手機(jī)分別是Android 6.0 和 Android 7.0 的系統(tǒng),而我的華為手機(jī)是Android 8.0 系統(tǒng)。這才想起是不是 8.0適配的問題。。。哎,先入為主的思想確實(shí)該改改了。

先看看你的Android studio 中 App目錄下的build.gradle中的SDK的參數(shù)是多少。

?

先說說明一下這幾個(gè)參數(shù)的作用:

compileSdkVersion:編譯SDK版本

minSdkVersion:最低SDK版本

targetSdkVersion:目標(biāo)SDK版本

其中compileSdkVersion要和targetSdkVersion數(shù)值一致,不然會(huì)報(bào)錯(cuò)。

26對(duì)應(yīng)的是O版本(即Android 8.0 ),而在8.0中,假如這樣創(chuàng)建Notification,標(biāo)題欄是不會(huì)顯示的(我現(xiàn)在手上只有一臺(tái)8.0 的華為榮耀V10,其他8.0機(jī)型未查看現(xiàn)象,如果有出入還望各位指正)。

@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的數(shù)值設(shè)置為25或以下,使用上面這段代碼的方式創(chuàng)建的Notification就能顯示了。

當(dāng)然,denpendencies里面的依賴包對(duì)應(yīng)也要從26及以上修改為25及以下的版本,然后再同步。同步的時(shí)候,會(huì)報(bào)錯(cuò),這個(gè)各位自己搞吧。你重新寫一個(gè)Demo會(huì)發(fā)現(xiàn)就是Android版本適配的問題,與手機(jī)廠商無關(guān)。

#補(bǔ)充

上面例子(例子很簡(jiǎn)單,有需要的可以看看):https://github.com/stoneWangL/NotificationTest

如果你想做一些個(gè)性化的標(biāo)題欄,比如說音樂播放器的標(biāo)題欄,可以使用RemoteViews?來實(shí)現(xiàn)。這個(gè)網(wǎng)上就有很多例子了,自己去搜索一下吧。

?

真正原因

Notification在android 8.0以上設(shè)置時(shí),需要設(shè)置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); // 設(shè)置頂部圖標(biāo) // Bitmap icBitmap = BitmapFactory.decodeResource(getResources(),R.drawable.play_background02); // mBuilder.setLargeIcon(icBitmap); // mBuilder.setOngoing(true); // mBuilder.setPriority(Notification.PRIORITY_MAX); // // // notification = mBuilder.build();//構(gòu)建通知 // setNotification(); // notification.contentView = remoteViews; // 設(shè)置下拉圖標(biāo) // notification.bigContentView = remoteViews; // 防止顯示不完全,需要添加apiSupport // notification.flags = Notification.FLAG_ONGOING_EVENT; // notification.icon = R.drawable.anim_log; // // startForeground(1, notification);//啟動(dòng)為前臺(tái)服務(wù)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(); //構(gòu)建通知setNotification();notification.contentView = remoteViews; // 設(shè)置下拉圖標(biāo)notification.bigContentView = remoteViews; // 防止顯示不完全,需要添加apiSupportnotification.flags = Notification.FLAG_ONGOING_EVENT;notification.icon = R.drawable.anim_log;mNotificationManager.notify(123, notification);//顯示通知} else {notification = mBuilder.build();//構(gòu)建通知setNotification();notification.contentView = remoteViews; // 設(shè)置下拉圖標(biāo)notification.bigContentView = remoteViews; // 防止顯示不完全,需要添加apisupportnotification.flags = Notification.FLAG_ONGOING_EVENT;notification.icon = R.drawable.anim_log;mNotificationManager.notify(123, notification);//顯示通知 // startForeground(123, notification);//啟動(dòng)為前臺(tái)服務(wù)}}

?

總結(jié)

以上是生活随笔為你收集整理的华为手机 标题栏 Notification 8.0 不显示的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。