android8.1使用自定义控件绘制Drawable奔溃
java.lang.ClassCastException: android.graphics.drawable.AdaptiveIconDrawable cannot be cast to android.graphics.drawable.BitmapDrawable
AdaptiveIconDrawable:anroid O开始有一个Drawable子类,“自适应桌面图标”(Adaptive Icons),我在代码使用了自适应图标,在绘制的时候报错,解决办法就是判断AdaptiveIconDrawable,使用AdaptiveIconDrawable去绘制。
android8.0系统开启服务奔溃,以下是解决方案:
Intent intent = new Intent(this, UpdateApkService.class);
intent.putExtra("VersionName", version);
intent.putExtra("VersionCode", versionCode);
intent.putExtra("VersionDescript", versionDescript);
intent.putExtra("ApkUrl", apkUrl);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
startForegroundService(intent);
} else {
startService(intent);
}
android8.0运行服务奔溃
在Service中的onCreate方法中正确创建NotificationChannel,通知的渠道ID(ChanneId)。
@Override
public void onCreate() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel channel = new NotificationChannel(channelID, channelName, NotificationManager.IMPORTANCE_HIGH);
NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
manager.createNotificationChannel(channel);
Notification notification = new Notification.Builder(this, channelID).build();
notification.iconLevel = R.mipmap.icon_app;
startForeground(serviceID, notification);
}
}
在Service中关闭自己本身
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
stopForeground(Service.STOP_FOREGROUND_REMOVE);
}
//关闭
stopSelf();
android8.0更新安装app时报错
需要在AndroidManifest.xml中添加android.permission.REQUEST_INSTALL_PACKAGES权限
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
boolean hasInstallPerssion = mContext.getPackageManager().canRequestPackageInstalls();
if (hasInstallPerssion ) {
AppUtils.installApk(mContext,downloadApkPath);
getDialog().cancel();
}else {
Intent intent = new Intent(Settings.ACTION_MANAGE_UNKNOWN_APP_SOURCES);
startActivityForResult(intent, REQUEST_CODE_UNKNOWN_APP);
ToastUtils.show("请打开\"某某应用\"未知应用安装权限,才可以正常安装更新apk!");
}
}else{
AppUtils.installApk(mContext,downloadApkPath);
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == REQUEST_CODE_UNKNOWN_APP){
AppUtils.installApk(mContext,downloadApkPath);
getDialog().cancel();
}
}
/**
* 安装应用
*/
public static void installApk(Context activity, String apkPath) {
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
File file = new File(apkPath);
if (file.exists()) {
if (Build.VERSION.SDK_INT >= 24) {
Uri fileUri = FileProvider.getUriForFile(activity, "com.wanhe.eng100.listening", file);
intent.setDataAndType(fileUri,
"application/vnd.android.package-archive");
} else {
intent.setDataAndType(Uri.parse("file://" + apkPath),
"application/vnd.android.package-archive");
}
activity.startActivity(intent);
}
}