Actually was yesterday anti-compile an apk, give it to add an automatic update function to use. In order to Smali under the convenient view, code write not standard, anyway to the smali are a hanging sample ~ ~ ~
Permissions: <uses-permission android:name= "Android.permission.WRITE_EXTERNAL_STORAGE"/> <uses-permission Android:name= "Android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/> <uses-permission android:name= " Android.permission.INTERNET "/>
//start=========== Public voidUpdata () {NewThread () {@Override Public voidrun () {request (Apiurl); }}.start (); } String newapkname= "updata.apk"; String Apiurl= "Http://192.168.1.101:8080/udate.txt"; //Request Interface Public voidrequest (String httpurl) {URL URL=NULL; Try{URL=NewURL (Httpurl); HttpURLConnection Conn=(HttpURLConnection) url.openconnection (); InputStream is=Conn.getinputstream (); String s=toString (IS); Jsonobject Jsonobject=NewJsonobject (s); Double Versioncode= Jsonobject.getdouble ("Versioncode");//Remote VersionString Versionname =Getversionname (); Doublev =double.parsedouble (versionname); if(Versioncode >v) { //Update DownloadDownLoadFile (jsonobject.getstring ("UpdateUrl")); } } Catch(Exception e) {e.printstacktrace (); }} Handler Handler=NewHandler () {@Override Public voidhandlemessage (Message msg) {Switch(msg.what) { Case1: Showupdatadilg (); Break; } } }; //version prompt dialog box Public voidShowupdatadilg () {NewAlertdialog.builder (mainactivity. This). Settitle (Prompted). Setmessage ("Do you want to install the latest version?" "). Setpositivebutton ("OK",NewDialoginterface.onclicklistener () { Public voidOnClick (Dialoginterface Dialog,intwhich) {uodate (); }}). Setnegativebutton ("Cancel",NewDialoginterface.onclicklistener () { Public voidOnClick (Dialoginterface Dialog,intwhich) {}}). Show (); } //Get version PrivateString Getversionname ()throwsException {Packagemanager Packagemanager=Getpackagemanager (); //Getpackagename () is the package name of your current class, and 0 means to get version informationPackageInfo packinfo = Packagemanager.getpackageinfo (Getpackagename (), 0); String version=Packinfo.versionname; returnversion; } //Install apk Public voiduodate () {//String str = "/" + Newapkname;//String fileName = environment.getexternalstoragedirectory () + str;//Intent Intent = new Intent (intent.action_view);//Intent.setdataandtype (Uri.fromfile (New File), "application/vnd.android.package-archive");//startactivity (intent);String str= "/sdcard/update/" +Newapkname; Intent Intent=NewIntent (Intent.action_view); Intent.setdataandtype (Uri.fromfile (NewFile (str)),"Application/vnd.android.package-archive"); StartActivity (Intent); }//dialog box determines Public voidisupdate () {String str= "/" +Newapkname; String FileNames= Environment.getexternalstoragedirectory () +str; Intent Intent=NewIntent (Intent.action_view); Intent.setdataandtype (Uri.fromfile (NewFile (FileNames)), "Application/vnd.android.package-archive"); StartActivity (Intent); } protectedfile DownLoadFile (String httpurl) {file Tmpfile=NewFile ("/sdcard/update"); if(!tmpfile.exists ()) {Tmpfile.mkdir (); } FinalFile File =NewFile ("/sdcard/update/" +newapkname); Try{URL URL=NewURL (Httpurl); Try{HttpURLConnection conn=(HttpURLConnection) url.openconnection (); InputStream is=Conn.getinputstream (); FileOutputStream Fos=Newfileoutputstream (file); byte[] buf =New byte[256]; Conn.connect (); DoubleCount = 0; if(Conn.getresponsecode () >= 400) {Toast.maketext (mainactivity. This, "Connection timed out", Toast.length_short). Show (); } Else { while(Count <= 100) { if(Is! =NULL) { intNumread =Is.read (BUF); if(Numread <= 0) { Break; } Else{fos.write (buf,0, Numread); } } Else { Break; }}} fos.close (); Is.close (); Conn.disconnect (); //Download SuccessfulHandler.sendemptymessage (1); } Catch(IOException e) {e.printstacktrace (); } } Catch(malformedurlexception e) {e.printstacktrace (); } returnfile; } //input stream converted to string Publicstring toString (InputStream is) {string result=NULL; Try { //constructs an output stream (byte queue output stream)Bytearrayoutputstream BAOs =NewBytearrayoutputstream (); //a byte array that is used to store the resulting byte data byte[] Bys =New byte[1024]; intLen = 0; //iterate through the full byte data while(len = Is.read (bys))! =-1) {baos.write (bys,0, Len); } //convert byte data to a stringresult =NewString (Baos.tobytearray ()); Is.close (); Baos.close (); } Catch(IOException e) {e.printstacktrace (); } returnresult; } //end===========
Android Download Update code