Download, install, and uninstall Android (original)

Source: Internet
Author: User

Scenario: When checking for version updates, you often need to download them from the server side and install them on your phone

Using tools: Xutils, this open source framework really takes a lot of time to read and understand, very useful, on the way! fighting!

Download: Download keyword search in github

 

1 //the reserved address provided by Google does not change the test IP address as the IP address of the computer changes2 Private Static FinalString mdownloadurl= "http://10.0.2.2:8080/xxx.apk";3 4 protected voiddownloadapk () {5 6         if(Environment.getexternalstoragestate (). Equals (7 environment.media_mounted)) {8           //download apk to SD path9String Sdpath =environment.getexternalstoragedirectory ()Ten. GetAbsolutePath () + File.separator + "xxx.apk"; OneHttputils httputils =Newhttputils (); A  - httputils.download (Mdownloadurl, Sdpath, -                     NewRequestcallback<file>() { the  - @Override -                          Public voidOnStart () { -LOG.I (Tag, "Start Download"); +                             Super. OnStart (); -                         } +  A @Override at                          Public voidOnloading (LongTotalLongCurrent , -                                 Booleanisuploading) { -LOG.I (tag, "Downloading in progress"); -LOG.I (Tag, "total =" +Total ); -LOG.I (Tag, "current =" +Current ); -                             Super. onloading (Total, Current, isuploading); in                         } -  to @Override +                          Public voidOnsuccess (responseinfo<file>responseinfo) { -LOG.I (tag, "Download Done"); the                             //get the downloaded file path *File File =Responseinfo.result; $                   //Install apkPanax Notoginseng installapk (file); -  the                         }   +  A @Override the                          Public voidonfailure (httpexception error, String msg) { +LOG.I (tag, "Download Failed"); -                         } $  $                     }); -         } -}
View Code

To implement the download through implicit intent

Find out the code below by querying the source.



* <data android:mimetype= "application/vnd.android.package-archive"/> * </intent-filter> * *

And then there's a simple implicit intent to start the activity process.

protected void installapk (file file) {Intent Intent = new Intent (); Intent.setaction ("Android.intent.action.VIEW"); Intent.addcategory ("Android.intent.category.DEFAULT");/* * Intent.setdata (uri.fromfile (file)); * Intent.settype ("application/vnd.android.package-archive"); */
is equivalent to the commented code, but the following method is recommended, there is a finish problem with the annotated method Intent.setdataandtype (uri.fromfile (file), "application/ Vnd.android.package-archive "); StartActivity (intent);}

Uninstall the application the same way, find the corresponding source code, see how the Android system is implemented

<intent-filter>    <action android:name= "Android.intent.action.VIEW"/>    <action android:name= "Android.intent.action.DELETE"/>    <category android:name= "Android.intent.category.DEFAULT"/>    <data android:scheme= "Package"/></intent-filter>

Ibid., Open the intention

protected void Uninstall () {Intent Intent = new Intent (); intent.setaction (Intent.action_delete); Intent.setdata ( Uri.parse ("Application package name to uninstall"); StartActivity (intent);}

  

Download, install, and uninstall Android (original)

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.