Android Project Combat (40): Andoird 7.0+ Install APK adapter

Source: Internet
Author: User

Original: Android Project Combat (40): Andoird 7.0+ Install APK adapter

  

First look at the code to install the APK file

      /** * Install apk with implicit intent to invoke system installer*/     Public Static voidInstall (context context) {Intent Intent=NewIntent (intent.action_view);intent.setflags (Intent.flag_activity_new_task); Intent.setdataandtype (Uri.fromfile (NewFile (Environment.getexternalstoragepublicdirectory (environment.directory_downloads),"xxxx. apk")),                "application/vnd.android.package-archive");    Context.startactivity (Intent); }

  

The test found that the code on the 7.0 model can successfully open the specified path under the specified APK file, but on the 7.0+ model calls the code will be error:

ANDROID.OS.FILEURIEXPOSEDEXCEPTION:FILE:///STORAGE/EMULATED/0/DOWNLOAD/XXXX.APK exposed beyond app through Intent.getdata ()

The reason:Android 7.0 is starting to prohibit exposing the file://URI to your app. If a Intent that contains the file file://URI type leaves your app, the app fails with an fileuriexposedexception exception.

  Workaround:

  One, add one of the four components in the Androidmanifest.xml file <provider>

    

<!--suitable for 7.0APK Installation-<provider Android:name="Android.support.v4.content.FileProvider"android:authorities="Com.xxx.xxxx.fileprovider" android:granturipermissions="true"android:exported="false"> <!--metadata-<meta-Data Android:name="Android.support.FILE_PROVIDER_PATHS"Android:resource="@xml/file_paths"/> </provider>

Note that the com.xxx.xxxx in the value of the Android:authorities property here is your package name and cannot be filled in arbitrarily

 Second, the Res directory to build an XML file, and create a new XML file File_paths.xml

    Note that the file name matches the value of the resource property in the first step

The contents are:

<?xml version="1.0" encoding="utf-8"?><paths>    <external-path path=".  "name="download"/></paths>

  

 Three, according to the model's Android system level to perform different installation apk code

      Note that according to the system version to execute different code, 7.0 the following call 7.0+ code will error, 7.0+ call 7.0 or less will be an error.

if(file!=NULL) {//file-apk file Intent Intent=NewIntent (Intent.action_view); //since no activity is initiated in the activity environment, the following label is setintent.setflags (Intent.flag_activity_new_task); if(build.version.sdk_int>= -) {//is the interpretation version above 7.0 ?//Parameter 1 context, parameter 2 provider the host address and the configuration file remain consistent with parameter 3 shared filesUri Apkuri =fileprovider.geturiforfile (Context,"Com.xxx.xxxxx.fileprovider", file); //Add this sentence to indicate the file that the URI represents for temporary authorization to the target appintent.addflags (intent.flag_grant_read_uri_permission); Intent.setdataandtype (Apkuri,"application/vnd.android.package-archive"); }Else{intent.setdataandtype (uri.fromfile (file),"application/vnd.android.package-archive");            } context.startactivity (Intent); }

Android Project Combat (40): Andoird 7.0+ Install APK adapter

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.