开发板 Android7.1 适配 Android10.0---记录
程序员文章站
2022-03-11 15:43:20
1.启动服务的变更:第一步:使用startForegroundService来启动service if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { startForegroundService(new Intent(this, MyService.class)); } else { startService(new Intent(this, MyService.class)); }第二步:在myService...
1.启动服务的变更:
第一步:使用startForegroundService来启动service
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
startForegroundService(new Intent(this, MyService.class));
} else {
startService(new Intent(this, MyService.class));
}
第二步:在myService的onCreate中添加一下代码
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
NotificationChannel mChannel = null;
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
mChannel = new NotificationChannel("service_01", getString(R.string.app_name),NotificationManager.IMPORTANCE_LOW);
notificationManager.createNotificationChannel(mChannel);
Notification notification = new Notification.Builder(getApplicationContext(), "service_01").build();
startForeground(1, notification);
}
第三步:在manifest中添加权限
<uses-permission android:name="android.permission.FOREGROUND_SERVICE"/>
2.Android10的sd卡读写权限使用了沙盘模式。但也提供了一个兼容代码
在application中添加android:requestLegacyExternalStorage=“true”
<application
android:requestLegacyExternalStorage="true"
</application>
3.android 10 限制了使用http请求;可通过一下做兼容:
a.在res目录下创建xml目录,然后随便创建一个myNetwork.xml文件,里面内容如下:
<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
<base-config cleartextTrafficPermitted="true" />
</network-security-config>
b.然后在application中添加android:networkSecurityConfig="@xml/myNetwork"
<application
android:networkSecurityConfig="@xml/myNetwork"
</application>
4.智能安装步骤:
a.
private void smartInstall(String apkPath) {
if(Build.VERSION.SDK_INT >= 24) {//判读版本是否在7.0以上
Uri apkUri = FileProvider.getUriForFile(getView().getContext(), "包名.fileprovider", new File(apkPath));//在AndroidManifest中的android:authorities值
Intent install = new Intent(Intent.ACTION_VIEW);
install.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
install.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
install.setDataAndType(apkUri, "application/vnd.android.package-archive");
getView().getContext().startActivity(install);
} else{
Intent install = new Intent(Intent.ACTION_VIEW);
install.setDataAndType(Uri.fromFile(new File(apkPath)), "application/vnd.android.package-archive");
install.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
getView().getContext().startActivity(install);
}
}
b. 在manifase中添加provider 标签,如下代码
<application
<provider
android:authorities="包名.fileprovider"
android:name="androidx.core.content.FileProvider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths" />
</provider>
</application>
c.在res目录下创建xml目录,然后随便创建一个file_paths.xml文件,里面内容如下:
<paths>
<external-path path="" name="ihelmet"/>
</paths>
本文地址:https://blog.csdn.net/m0_37039192/article/details/111880395
上一篇: 立秋最适合吃啥菜?
下一篇: 关于Fuchsia及鸿蒙的一点想法