In fact, it is very simple, just a few lines of code, first to the server to download the APK, and then to install, of course, not a fool should know, I use here is httputils to download,
You need some permission here.
<uses-permission android:name= "Android.permission.INTERNET"/> <uses-permission android:name= " Android.permission.WRITE_EXTERNAL_STORAGE "/> <uses-permission android:name=" android.permission.MOUNT_ Unmount_filesystems "/> <uses-permission android:name=" Android.permission.INSTALL_PACKAGES "/>
Direct Sticker Code
Package Com.exmple.httpxutil;import Java.io.file;import Android.annotation.suppresslint;import Android.app.activity;import Android.content.intent;import Android.content.pm.packageinfo;import Android.content.pm.packagemanager;import Android.content.pm.packagemanager.namenotfoundexception;import Android.content.pm.signature;import Android.net.uri;import Android.os.bundle;import Android.os.Environment;import Android.view.view;import Android.view.view.onclicklistener;import Android.widget.button;import Android.widget.progressbar;import Com.lidroid.xutils.httputils;import com.lidroid.xutils.exception.HttpException ; Import Com.lidroid.xutils.http.httphandler;import Com.lidroid.xutils.http.responseinfo;import Com.lidroid.xutils.http.callback.requestcallback;public class Mainactivity extends Activity {httphandler<file > H;private ProgressBar pa; @SuppressLint ("Sdcardpath") @Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (r.layout.Activity_main); Button button = (button) Findviewbyid (R.id.button);p a = (ProgressBar) Findviewbyid (R.id.pro); button more = (Button) Findviewbyid (R.id.more); GetCode (); More.setonclicklistener (new Onclicklistener () {@ overridepublic void OnClick (View v) {Intent ints = new Intent (mainactivity.this, Newactivity.class); StartActivity (INTs) ;}}); Button.setonclicklistener (New Onclicklistener () {@Overridepublic void OnClick (View v) {httputils http = new Httputils (); Final String Path = Environment.getexternalstoragedirectory (). GetPath (); System.out.println (path), H = http.download ("http://101.200.142.201:8080/tqyb/baidumap.apk", path+ "/badumap.apk", True, True,new requestcallback<file> () {@Overridepublic void OnStart () {System.out.println ("= = ="); SYSTEM.OUT.PRINTLN ("Started downloading the ++++++++++++++");} @Overridepublic void onloading (long total, long Current,boolean isuploading) {System.out.println (total + "====="); SYSTEM.OUT.PRINTLN (current + "====="); System.out.println (isuploading + "=====");p A.setmax ((int)Total);p a.setprogress ((int.) current);} @Overridepublic void OnFailure (HttpException error,string msg) {System.out.println (Error + "+++++++" + msg+ "+++++++++") ;} @Overridepublic void onsuccess (responseinfo<file> responseinfo) {h.cancel (); installapk (path + "/badumap.apk");});} });} Installed method private void installapk (String filename) {File File = new file (filename); Intent Intent = new Intent (); Intent.addfla GS (Intent.flag_activity_new_task); intent.setaction (Intent.action_view); String type = "Application/vnd.android.package-archive"; Intent.setdataandtype (uri.fromfile (file), type); StartActivity (intent);} Wait until the version number of the Code private void GetCode () {Packagemanager Manager; PackageInfo info = Null;manager = This.getpackagemanager (); try {info = manager.getpackageinfo (this.getpackagename (), 0) ; String name = Info.versionname;int Versioncode = Info.versioncode; String PackageName = info.packagename; signature[] signatures = info.signatures; SYSTEM.OUT.PRINTLN (name + "++++++++++" + Versioncode + "+++++" + PackagenaMe + "========nnn" + signatures+ "=============");/* * INFO.VERSIONNAME; * * INFO.PACKAGENAME; * * INFO.SIGNATURES; */} catch (Namenotfoundexception e) {//TODO auto-generated catch Blocke.printstacktrace ();}}}
Automatically install programs after downloading the app