Android Intent Install APK

Source: Internet
Author: User

/*** 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

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.