/*** Install APK * *@paramContext *@paramApkpath*/ Public Static voidinstallapk (Context context, String Apkpath) {Try { /*** provider * deal with Android 7.0 and above system installation abnormal problem*/File File=NewFile (Apkpath); Intent Install=NewIntent (); Install.setaction (Intent.action_view); Install.addcategory (Intent.category_default); Install.setflags (Intent.flag_activity_new_task); if(Build.VERSION.SDK_INT >=Build.version_codes. N) {Uri Apkuri= Fileprovider.geturiforfile (Context, "Com.jxzg.jxzgapp.fileprovider", file);//The android:authorities value in the AndroidmanifestLOG.D ("======", "apkuri=" +Apkuri); Install.addflags (intent.flag_grant_read_uri_permission);//Add this sentence to indicate the file that the URI represents for temporary authorization to the target appInstall.setdataandtype (Apkuri, "application/vnd.android.package-archive"); } Else{install.setdataandtype (uri.fromfile (file),"Application/vnd.android.package-archive"); } context.startactivity (install); } Catch(Exception e) {log.d ("======", E.getmessage ()); Toast.maketext (Context,"File parsing failed", Toast.length_short). Show (); DeleteFile (Apkpath); } }
Androidmanifest.xml Inside Register Provider
<providerAndroid:name= "Android.support.v4.content.FileProvider"android:authorities= "Com.chao.app.fileprovider"android:granturipermissions= "true"android:exported= "false"> <Meta-dataAndroid:name= "Android.support.FILE_PROVIDER_PATHS"Android:resource= "@xml/file_paths" /> </provider>
Need to add permissions
<android:name= "Android.permission.REQUEST_INSTALL_PACKAGES"/>
Create a new file_paths.xml under/res/xml/
<? XML version= "1.0" encoding= "Utf-8" ?> < Resources > < Paths > < path= "" name= "Download"/> </ paths></resources>
Android Intent install APK