private void update(Context mContext) {
//安装应用 Intent intent = new Intent(Intent.ACTION_VIEW); intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); intent.setDataAndType(Uri.fromFile(new File(Environment .getExternalStorageDirectory(), DOWNLOAD_NAME)), "application/vnd.android.package-archive"); mContext.startActivity(intent); android.os.Process.killProcess(android.os.Process.myPid()); }
//如果没有android.os.Process.killProcess(android.os.Process.myPid()); 最后,弹出窗口不会提示完成和打开。 //Intent.FLAG_ACTIVITY_NEW_TASK当调用startActivity启动一个Activity时使用的。 //如果没有i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 最后一点打开,新版本的应用不会打开。
1、getUriForFile获取Uri路径NullPointerException 报错如下:
04-16 11:39:34.664 1035 1140 E AndroidRuntime: FATAL EXCEPTION: VPSClient 04-16 11:39:34.664 1035 1140 E AndroidRuntime: Process: com.android.vpsclient:plugin, PID: 1035 04-16 11:39:34.664 1035 1140 E AndroidRuntime: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.Context com.tencent.shadow.sample.host.lib.HostUiLayerProvider.getContext()' on a null object reference 04-16 11:39:34.664 1035 1140 E AndroidRuntime: at com.android.vps.upgrade.UpgradeUtils.<init>(UpgradeUtils.java:42) 04-16 11:39:34.664 1035 1140 E AndroidRuntime: at com.android.vps.upgrade.UpgradeUtils.getInstance(UpgradeUtils.java:62) 04-16 11:39:34.664 1035 1140 E AndroidRuntime: at com.android.vps.basic.core.VPSClient$WorkHandler$UpdateChecker.startUpdateChecker(VPSClient.java:187) 04-16 11:39:34.664 1035 1140 E AndroidRuntime: at com.android.vps.basic.core.VPSClient$WorkHandler.handleMessage(VPSClient.java:242) 04-16 11:39:34.664 1035 1140 E AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:102) 04-16 11:39:34.664 1035 1140 E AndroidRuntime: at android.os.Looper.loop(Looper.java:154) 04-16 11:39:34.664 1035 1140 E AndroidRuntime: at android.os.HandlerThread.run(HandlerThread.java:61)
Android7.0之后,Android不再允许在app中把file://Uri暴露给其他app 1、添加清单
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="com.android.vpsclient.fileprovider"
android:grantUriPermissions="true"
android:exported="false">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths"/>
</provider>
2、创建file_paths.xml文件
路径:res\xml\file_paths.xml
说明:
<?xml version="1.0" encoding="utf-8"?>
<paths>
<external-path path="Android/data/com.android.vpsclient/" name="files_root"/>
<external-path path="." name="external_storage_root"/>
<root-path name="root_path" path="."/>
</paths>
3、使用
Uri contentUri = FileProvider.getUriForFile(getContext(),"packagename.fileprovider",tempFile);
4、完整调用
private Boolean doUpgradeConfirm(File file){
Logger.d(TAG, "going to install: " + file.getAbsolutePath()+". Confirm!");
Intent intent = new Intent();
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setAction(Intent.ACTION_VIEW);
Uri uri;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
uri = FileProvider.getUriForFile(mContext,
mContext.getPackageName() + ".fileprovider", file);
} else {
uri = Uri.fromFile(file);
}
Logger.d(TAG, "do uri: " + uri.toString());
intent.setDataAndType(uri,"application/vnd.android.package-archive");
this.mContext.startActivity(intent);
android.os.Process.killProcess(android.os.Process.myPid());
return true;
}
———————————————— 版权声明:本文为CSDN博主「我在看图」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。 原文链接:https://blog.csdn.net/twk121109281/article/details/99580866