因为android6以来,google公司不断缩紧权限,不断增强android系统的安全性,导致现如今在新版手机上出现了各种问题。前段时间刚刚升级了融云的推送聊天在android8上无法正常弹出通知栏提示后,又被测试发现了一个更大的坑。原来我们的APP里面通知没有查找到应用中对应的NotificationChannel的原因,而无法弹出来,以至于手机无法显示应用正在下载。于是乎只能百度一下,马上修改一下。
1.添加channelid,来弹出通知栏,主要代码
....
String channelID = "subscribe";
String channelName = "channel_name";
NotificationChannel channel = new NotificationChannel(channelID, channelName, NotificationManager.IMPORTANCE_HIGH);
mNotificationManager.createNotificationChannel(channel);
mNotification = new Notification.Builder(mContext);
mNotification.setContent(contentView).setOngoing(true).setContentText(tickerText).setOngoing(false) .setSmallIcon(icon).setContentTitle(tickerText).setWhen(when).setAutoCancel(true).setPriority(Notification.PRIORITY_DEFAULT);
no = mNotification.build();
mNotification.setChannelId(channelID);
.....
当顺利弹出通知栏后,以为大功告成了,谁知android8又不让安装未知来源的应用,导致下载后应用无法自动安装,于是又一顿改
2.添加未知来源应用安装权限
添加权限
<uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES" />
判断是否是android8,并主动调到应用安装列表
private void checkIsAndroidO(int versionflag) {
if (Build.VERSION.SDK_INT >= 26) {
boolean b = getActivity().getPackageManager().canRequestPackageInstalls();
if (b) {
if (versionflag == 1) {
showForceUpdate();
} else if(versionflag == 0) {
showUpdateDialog();
}
} else {
DialogEnsureCancelView dialogEnsureCancelView = new DialogEnsureCancelView(
getActivity()).setDialogMsg("温馨提示", "安装应用需要打开未知来源权限,请去设置中开启权限?", "打开权限")
.setOnClickListenerEnsure(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
startInstallPermissionSettingActivity();
}
}
});
DialogUtils.showSelfDialog(getActivity(), dialogEnsureCancelView);
}
} else {
if (versionflag == 1) {
//强制升级
showForceUpdate();
} else if(versionflag == 0) {
//非强制升级
showUpdateDialog();
}
}
}
//跳往未知应用安装列表
@RequiresApi(api = Build.VERSION_CODES.O)
private void startInstallPermissionSettingActivity() {
//注意这个是8.0新API
Intent intent = new Intent(Settings.ACTION_MANAGE_UNKNOWN_APP_SOURCES);
startActivityForResult(intent, 100);
}
根据intent值回调
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case 100:
checkIsAndroidO(versionflag);
break;
default:
break;
}
}
网友评论