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

歡迎訪問(wèn) 生活随笔!

生活随笔

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

编程问答

android8 通知呼吸灯_android学习笔记----解决兼容8.0以上和8.0之前版本通知栏显示、振动、LED呼吸灯闪烁问题(真机验证)...

發(fā)布時(shí)間:2024/9/27 编程问答 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 android8 通知呼吸灯_android学习笔记----解决兼容8.0以上和8.0之前版本通知栏显示、振动、LED呼吸灯闪烁问题(真机验证)... 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

Android 8.0系統(tǒng)的通知欄適配文章講解(郭霖大神的):

然后開始試驗(yàn)了:

模擬器:

真機(jī)(華為榮耀V9,8.0系統(tǒng)),下拉橫幅需要手動(dòng)打開,除非是廠家白名單,比如QQ、微信

我在oppo手機(jī)6.0系統(tǒng)測(cè)試結(jié)果是這樣的,需要手動(dòng)打開設(shè)置,點(diǎn)擊后會(huì)出現(xiàn)這樣

然后點(diǎn)擊通知管理設(shè)置權(quán)限,oppo手機(jī)默認(rèn)權(quán)限都是關(guān)閉的。

設(shè)置左上角通知的小圖標(biāo)setSmallIcon()只能使用純alpha圖層的圖片進(jìn)行設(shè)置,需要美工實(shí)現(xiàn),具體詳解請(qǐng)見這里:

activity_main.xml

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="vertical">

android:onClick="sendChatMsg"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="發(fā)送聊天消息"/>

android:onClick="sendSubscribeMsg"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="發(fā)送訂閱消息"/>

MainActivity.java

import android.annotation.TargetApi;

import android.app.Notification;

import android.app.NotificationChannel;

import android.app.NotificationManager;

import android.app.PendingIntent;

import android.content.Intent;

import android.graphics.BitmapFactory;

import android.graphics.Color;

import android.net.Uri;

import android.os.Build;

import android.os.Bundle;

import android.provider.Settings;

import android.support.v4.app.NotificationCompat;

import android.support.v7.app.AppCompatActivity;

import android.view.View;

import android.widget.Toast;

import static android.provider.Settings.EXTRA_APP_PACKAGE;

public class MainActivity extends AppCompatActivity {

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {

String channelId = "chat";

String channelName = "聊天消息";

int importance = NotificationManager.IMPORTANCE_MAX;

createNotificationChannel(channelId, channelName, importance);

channelId = "subscribe";

channelName = "訂閱消息";

importance = NotificationManager.IMPORTANCE_DEFAULT;

createNotificationChannel(channelId, channelName, importance);

}

}

@TargetApi(Build.VERSION_CODES.O)

private void createNotificationChannel(String channelId, String channelName, int importance) {

NotificationChannel channel = new NotificationChannel(channelId, channelName, importance);

channel.enableVibration(true);

channel.setVibrationPattern(new long[]{0, 100, 100, 100});

channel.enableLights(true);

channel.setLightColor(Color.RED);

NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

notificationManager.createNotificationChannel(channel);

}

public void sendChatMsg(View view) {

NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {

NotificationChannel channel = manager.getNotificationChannel("chat");

if (channel.getImportance() == NotificationManager.IMPORTANCE_NONE) {

Intent intent = new Intent(Settings.ACTION_CHANNEL_NOTIFICATION_SETTINGS);

intent.putExtra(EXTRA_APP_PACKAGE, getPackageName());

intent.putExtra(Settings.EXTRA_CHANNEL_ID, channel.getId());

startActivity(intent);

Toast.makeText(this, "請(qǐng)手動(dòng)將通知打開", Toast.LENGTH_SHORT).show();

}

// Intent intent = new Intent();

//intent.setAction(Settings.ACTION_APP_NOTIFICATION_SETTINGS);

/*

//這種方案適用于 API 26, 即8.0(含8.0)以上可以用

intent.putExtra(EXTRA_APP_PACKAGE, getPackageName());

intent.putExtra(EXTRA_CHANNEL_ID, getApplicationInfo().uid);

//這種方案適用于 API21——25,即 5.0——7.1 之間的版本可以使用

intent.putExtra("app_package", getPackageName());

intent.putExtra("app_uid", getApplicationInfo().uid);

startActivity(intent);*/

// 小米6 -MIUI9.6-8.0.0系統(tǒng),是個(gè)特例,通知設(shè)置界面只能控制"允許使用通知圓點(diǎn)"——然而這個(gè)玩意并沒有卵用,我想對(duì)雷布斯說(shuō):I'm not ok!!!

// if ("MI 6".equals(Build.MODEL)) {

// intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);

// Uri uri = Uri.fromParts("package", getPackageName(), null);

// intent.setData(uri);

// // intent.setAction("com.android.settings/.SubSettings");

// }

} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {

if (!NotificationsUtils.isNotificationEnabled(this)) {

Intent intent = new Intent("android.settings.APPLICATION_DETAILS_SETTINGS");

/*intent.putExtra(EXTRA_APP_PACKAGE, getPackageName());

intent.putExtra("uid", getApplicationInfo().uid);*/

intent.setData(Uri.fromParts("package",

getPackageName(), null));

startActivity(intent);

Toast.makeText(this, "請(qǐng)手動(dòng)將通知打開", Toast.LENGTH_SHORT).show();

}

}

Intent intent = new Intent(this, ChatMessage.class);

PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);

Notification notification = new NotificationCompat.Builder(this, "chat")

.setContentTitle("收到一條聊天消息")

.setContentText("今天晚上吃點(diǎn)啥?")

.setWhen(System.currentTimeMillis())

.setSmallIcon(R.mipmap.ic_launcher)

.setLargeIcon(BitmapFactory.decodeResource(getResources(),

R.mipmap.ic_launcher_round))

.setAutoCancel(true)

// 8.0以前的低版本中,若沒有setDefaults,無(wú)論多高的優(yōu)先級(jí),通知都無(wú)法彈出橫幅

//.setDefaults(NotificationCompat.DEFAULT_ALL)

// 這里并非多此一舉,channel設(shè)置了振動(dòng)只是為了8.0以上的手機(jī),低版本的振動(dòng)只能在這里設(shè)置

.setVibrate(new long[]{0, 100, 100, 100})

.setLights(Color.RED, 1000, 1000)

.setContentIntent(pendingIntent)

.build();

manager.notify(1, notification);

}

public void sendSubscribeMsg(View view) {

Intent intent = new Intent(this, SubscribeMessage.class);

PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);

NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

Notification notification = new NotificationCompat.Builder(this, "subscribe")

.setContentTitle("收到一條支付寶消息")

.setContentText("免費(fèi)醫(yī)療金領(lǐng)取提醒")

.setWhen(System.currentTimeMillis())

.setSmallIcon(R.mipmap.ic_launcher_round)

.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher))

.setAutoCancel(true)

// 這里并非多此一舉,channel設(shè)置了振動(dòng)只是為了8.0以上的手機(jī),低版本的振動(dòng)只能在這里設(shè)置

.setVibrate(new long[]{0, 100, 100, 100})

.setLights(Color.RED, 1000, 1000)

.setContentIntent(pendingIntent)

.build();

manager.notify(2, notification);

}

}

筆記批注:

這里我們?cè)贛ainActivity中創(chuàng)建了兩個(gè)通知渠道,首先要確保的是當(dāng)前手機(jī)的系統(tǒng)版本必須是Android 8.0系統(tǒng)或者更高,因?yàn)榈桶姹镜氖謾C(jī)系統(tǒng)并沒有通知渠道這個(gè)功能,不做系統(tǒng)版本檢查的話會(huì)在低版本手機(jī)上造成崩潰。

這里封裝了一個(gè)createNotificationChannel()方法,需要注意的是,創(chuàng)建一個(gè)通知渠道至少需要渠道ID、渠道名稱以及重要等級(jí)這三個(gè)參數(shù),其中渠道ID可以隨便定義,只要保證全局唯一性就可以。渠道名稱是給用戶看的,需要能夠表達(dá)清楚這個(gè)渠道的用途。重要等級(jí)的不同則會(huì)決定通知的不同行為,當(dāng)然這里只是初始狀態(tài)下的重要等級(jí),用戶可以隨時(shí)手動(dòng)更改某個(gè)渠道的重要等級(jí),App是無(wú)法干預(yù)的。

其中App通知主要可以分為兩類,一類是我和別人的聊天消息,這類消息非常重要,因此重要等級(jí)設(shè)為了IMPORTANCE_HIGH。另一類是公眾號(hào)的訂閱消息,這類消息不是那么重要,因此重要等級(jí)設(shè)為了IMPORTANCE_DEFAULT。除此之外,重要等級(jí)還可以設(shè)置為IMPORTANCE_LOW、IMPORTANCE_MIN,分別對(duì)應(yīng)了更低的通知重要程度。

創(chuàng)建通知渠道的這部分代碼,你可以寫在MainActivity中,也可以寫在Application中,實(shí)際上可以寫在程序的任何位置,只需要保證在通知彈出之前調(diào)用就可以了。并且創(chuàng)建通知渠道的代碼只在第一次執(zhí)行的時(shí)候才會(huì)創(chuàng)建,以后每次執(zhí)行創(chuàng)建代碼系統(tǒng)會(huì)檢測(cè)到該通知渠道已經(jīng)存在了,因此不會(huì)重復(fù)創(chuàng)建,也并不會(huì)影響任何效率。

通知渠道一旦創(chuàng)建之后就不能再通過(guò)代碼修改了。只有在將通道提交給如果在提交之前作了修改,請(qǐng)先卸載再重新安裝app即可,之前就因?yàn)闆]有重新安裝導(dǎo)致始終無(wú)法振動(dòng)。

關(guān)于led呼吸燈,在oppo手機(jī)上需要手動(dòng)打開,在華為android8.0系統(tǒng)手機(jī)呼吸燈可正常顯示,當(dāng)然這個(gè)需要手機(jī)支持,有的手機(jī)就只有白色的呼吸燈,沒有紅綠藍(lán)。

在真機(jī)上需要手動(dòng)打開顯示橫幅的權(quán)限,否則設(shè)置最大的priority也無(wú)效,除非廠家白名單。

這里不用根據(jù)api等級(jí)手動(dòng)設(shè)置NotificationCompat.Builder參數(shù)的個(gè)數(shù),8.0以上版本是2個(gè)參數(shù),8.0以下是1個(gè)參數(shù),在.build()的時(shí)候源碼里面會(huì)自動(dòng)根據(jù)系統(tǒng)的api等級(jí)做出相應(yīng)的判斷。如下圖:

NotificationsUtils.java(判斷通知是否打開,針對(duì)8.0以下系統(tǒng))

import android.annotation.SuppressLint;

import android.app.AppOpsManager;

import android.content.Context;

import android.content.pm.ApplicationInfo;

import java.lang.reflect.Field;

import java.lang.reflect.InvocationTargetException;

import java.lang.reflect.Method;

public class NotificationsUtils {

private static final String CHECK_OP_NO_THROW = "checkOpNoThrow";

private static final String OP_POST_NOTIFICATION = "OP_POST_NOTIFICATION";

@SuppressLint("NewApi")

public static boolean isNotificationEnabled(Context context) {

AppOpsManager mAppOps = (AppOpsManager) context.getSystemService(Context.APP_OPS_SERVICE);

ApplicationInfo appInfo = context.getApplicationInfo();

String pkg = context.getApplicationContext().getPackageName();

int uid = appInfo.uid;

Class appOpsClass = null;

/* Context.APP_OPS_MANAGER */

try {

appOpsClass = Class.forName(AppOpsManager.class.getName());

Method checkOpNoThrowMethod = appOpsClass.getMethod(CHECK_OP_NO_THROW, Integer.TYPE, Integer.TYPE,

String.class);

Field opPostNotificationValue = appOpsClass.getDeclaredField(OP_POST_NOTIFICATION);

int value = (Integer) opPostNotificationValue.get(Integer.class);

return ((Integer) checkOpNoThrowMethod.invoke(mAppOps, value, uid, pkg) == AppOpsManager.MODE_ALLOWED);

} catch (ClassNotFoundException e) {

e.printStackTrace();

} catch (NoSuchMethodException e) {

e.printStackTrace();

} catch (NoSuchFieldException e) {

e.printStackTrace();

} catch (InvocationTargetException e) {

e.printStackTrace();

} catch (IllegalAccessException e) {

e.printStackTrace();

}

return false;

}

}

ChatMessage.java

public class ChatMessage extends AppCompatActivity {

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_chat_message);

}

}

activity_chat_message.xml

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="vertical">

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="聊天消息"/>

SubscribeMessage.java

public class SubscribeMessage extends AppCompatActivity {

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_subscribe_message);

}

}

activity_subscribe_message.xml

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="vertical">

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="訂閱消息"/>

========================Talk is cheap, show me the code========================

與50位技術(shù)專家面對(duì)面20年技術(shù)見證,附贈(zèng)技術(shù)全景圖

總結(jié)

以上是生活随笔為你收集整理的android8 通知呼吸灯_android学习笔记----解决兼容8.0以上和8.0之前版本通知栏显示、振动、LED呼吸灯闪烁问题(真机验证)...的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

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

主站蜘蛛池模板: 欧美成人免费播放 | 色欧美日韩| 亚洲成熟少妇视频在线观看 | 国产精品男女 | 中文字幕电影一区二区 | 亚洲午夜精品一区 | 一级黄色视屏 | 影音先锋黑人 | 天堂网一区| 殴美毛片| 风流老熟女一区二区三区 | 黄色片www | 日韩专区在线观看 | 日韩欧美麻豆 | 在线观看成人av | 国产91精品ai换脸 | www.白白色| 香港三级韩国三级日本三级 | 久草福利在线视频 | 国产无套内射普通话对白 | 国产免费成人 | 91麻豆精品国产91 | h部分肌肉警猛淫文 | 韩国一区二区在线观看 | xxx国产精品 | 91九色蝌蚪视频 | 国产精品国产三级国产aⅴ中文 | 99久久国 | 欧美男女性生活视频 | 水牛影视av一区二区免费 | 美女主播在线观看 | 啦啦啦视频在线观看 | 亚洲欧美另类激情 | 成人国产精品免费观看视频 | 蜜桃av在线| 黄色视屏软件 | 日本在线视频www色 国产在线视频网址 | 果冻av在线 | 亚洲一区电影在线观看 | 久久夫妻视频 | 精品国产乱码久久久久久蜜臀网站 | 久久亚洲精精品中文字幕早川悠里 | 亚洲乱强伦 | 午夜av网站| 午夜秋霞影院 | 亚洲综合免费视频 | 操极品女神 | 亚洲av无码专区在线 | 亚洲一区二区三区四区在线观看 | 无人在线观看的免费高清视频 | 欧美黑人一级 | 精品无码久久久久国产 | 女优色图| 午夜激情在线观看视频 | 久久午夜网站 | 欧美日韩精品一区二区在线观看 | 色91精品久久久久久久久 | av免费在线观看不卡 | 日韩美女一级片 | 一二三四区在线 | 国产精品欧美一区二区 | 欧美成a| 午夜爽视频 | 亚州国产精品 | 偷偷操网站 | 一区二区三区四区国产精品 | 91插插插永久免费 | 91国产丝袜在线播放 | 91porny在线 | 欧美成人福利 | 成年人av在线播放 | 亚洲成av人片在www色猫咪 | 免费a v网站 | 国产五月天婷婷 | 熟女国产精品一区二区三 | 91九色蝌蚪| 日韩一区二区精品 | 波多野结衣在线 | xxxx在线播放 | 波多野结衣中文字幕一区 | 一区二区三区在线 | 一区二区三区四区亚洲 | 亚州三级 | 538国产精品一区二区 | 波多野结衣喷潮 | 亚洲日本韩国在线 | 国产一卡二卡在线播放 | 女同性αv亚洲女同志 | 国产精品美女久久久久图片 | 精品乱码一区二区三区 | 日本黄色的视频 | 亚洲国产精品无码久久久久高潮 | 国产一区二区视频播放 | 手机看片在线观看 | 欧美做受xxxxxⅹ性视频 | 已满十八岁免费观看全集动漫 | 秋霞伦理一区二区 | 亚洲爽爽网 | 欧美美女爱爱视频 |