android8 通知呼吸灯_android学习笔记----解决兼容8.0以上和8.0之前版本通知栏显示、振动、LED呼吸灯闪烁问题(真机验证)...
Android 8.0系統(tǒng)的通知欄適配文章講解(郭霖大神的):
然后開始試驗(yàn)了:
模擬器:
真機(jī)(華為榮耀V9,8.0系統(tǒng)),下拉橫幅需要手動(dòng)打開,除非是廠家白名單,比如QQ、微信
我在oppo手機(jī)6.0系統(tǒng)測試結(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ì)雷布斯說: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,無論多高的優(yōu)先級(jí),通知都無法彈出橫幅
//.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是無法干預(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ì)檢測到該通知渠道已經(jīng)存在了,因此不會(huì)重復(fù)創(chuàng)建,也并不會(huì)影響任何效率。
通知渠道一旦創(chuàng)建之后就不能再通過代碼修改了。只有在將通道提交給如果在提交之前作了修改,請(qǐng)先卸載再重新安裝app即可,之前就因?yàn)闆]有重新安裝導(dǎo)致始終無法振動(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也無效,除非廠家白名單。
這里不用根據(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呼吸灯闪烁问题(真机验证)...的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 生活常识:如何正确地清洁窗户?
- 下一篇: 家里木地板装修,入户门那块需要铺瓷砖吗,