Android installs the APK file in the code

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.