Nonsense not to say,on the Code
Private voidInstall (String filePath) {log.i (TAG,"Start the installation:" +FilePath); File Apkfile=NewFile (FilePath); Intent Intent=NewIntent (Intent.action_view); Intent.setflags (Intent.flag_activity_new_task); if(Build.VERSION.SDK_INT >=Build.version_codes. N) {LOG.W (TAG,"Version greater than N, start installation with Fileprovider"); Intent.setflags (intent.flag_grant_read_uri_permission); Uri Contenturi=fileprovider.geturiforfile (Mcontext,"Your package name. Fileprovider", Apkfile); Intent.setdataandtype (Contenturi,"Application/vnd.android.package-archive"); } Else{LOG.W (TAG,"Install normally"); Intent.setdataandtype (Uri.fromfile (apkfile),"Application/vnd.android.package-archive"); } startactivity (Intent); }
Code description
about installing the APK file in code, after Android N , for Android for security reasons, cannot directly access the software, you need to use the Fileprovider mechanism to access, open the APK file.
The above if statement is to differentiate the software running platform, to set different properties on the intent.
For each version of Android, install the APK using the code First step:
Add <provider> tags to the manifest file (manifests.xml) application tag:
<Application> <!--Additional Configuration Items - <providerAndroid:name= "Android.support.v4.content.FileProvider"android:authorities= "Your package name. Fileprovider"android:exported= "false"android:granturipermissions= "true"> <Meta-dataAndroid:name= "Android.support.FILE_PROVIDER_PATHS"Android:resource= "@xml/file_paths" /> </provider> <!--Additional Configuration Items - </Application>
Note the two-point content:
1. android:authorities= "Your package name. Fileprovider" This property is set to your own package name.
2. android:resource= "@xml/file_paths" under the <meta-data> tag is the XML file to be configured, and his content is as follows:
Step Two:
Add a file under res/xml : file_paths.xml The file reads as follows:
<? XML version= "1.0" encoding= "Utf-8" ?> < Paths > < External-path name = "Your_Name" path= "Your_path"/></paths>
The above two properties are configured according to their own use.
Where <external-path> is the external storage directory of the phone.
Step Three:
Use the top-most code in your Java code to solve the problem.
Here's a point to note:
The android:authorities= in the manifest file "your package name. Fileprovider" and
Uri Contenturi = fileprovider.geturiforfile ( mcontext "your package name. Fileprovider" , Apkfile);
The fields in the Green background must be the same, or you will get an error.
Android installs the APK file in the code