Android upgrade download Their definition updates compatible version
Last Update:2015-10-16
Source: Internet
Author: User
<span id="Label3"></p><strong><strong><span style="font-size:18px">Android Update module their definition update</span></strong></strong><span style="font-size:14px"><span style="font-size:14px"><br></span></span><br><span style="font-size:14px"><span style="font-size:14px"><br></span></span><span style="font-size:14px"><span style="font-size:14px">write this summary is due to the project encountered in the Android system compatibility bug</span></span><span style="font-size:14px"><span style="font-size:14px"><br></span></span><span style="font-size:14px">the <span style="font-size:14px">Android Project was originally using an api-provided download method</span></span><span style="font-size:14px"><span style="font-size:14px">for example, the following:</span></span><span style="font-size:14px"><span style="font-size:14px"></span></span><pre name="code" class="java"><pre name="code" class="java">Downloadmanager Downloadmanager = (downloadmanager) Getsystemservice (activity.download_service);D Ownloadmanager.request request = new request (uri.parse (dl)); Request.settitle (getString (r.string.app_name)); Request.setdestinationuri (getdownloadname (getintent () getstringextra ("version_name")); Long reference = Downloadmanager.enqueue (request); Usersettinghelper.getinstance (). Setupgradekey (reference);</pre></pre><br><br><span style="font-size:14px"><span style="font-size:14px">however, such as Samsung Huawei and other mobile phones its own system to remove the Google services so as to cause a crash</span></span><span style="font-size:14px"><span style="font-size:14px"><br></span></span><span style="font-size:14px"><span style="font-size:14px"><br></span></span><span style="font-size:14px"><span style="font-size:14px">find the information on the Internet and change it. the bug was fixed at high speed:</span></span><span style="font-size:14px"><span style="font-size:14px"><br></span></span><span style="font-size:14px"><span style="font-size:14px">1) main Activity</span></span><span style="font-size:14px"><span style="font-size:14px"></span></span><pre name="code" class="java">Package Com.example.updataapk;import Android.app.activity;import Android.content.intent;import android.net.Uri; Import Android.os.bundle;import android.view.view;import android.view.view.onclicklistener;/** * * @author Baozi * */PU Blic class Mainactivity extends Activity {//address private String dl = "http://17shihui.cn/download/shihui.apk"; @ overrideprotected void onCreate (Bundle Savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview ( r.layout.activity_main); Function_utility.setappcontext (getapplicationcontext ()); Findviewbyid (r.id.button1). setOnClickListener (new Onclicklistener () {@Overridepublic void OnClick (View v) {uri uri = uri.parse (dl); Intent Intent = new Intent (intent.action_ VIEW, uri); startactivity (intent);}}); Findviewbyid (r.id.button2). setonclicklistener (new onclicklistener () {@Overridepublic void OnClick (View v) {try { Download (dl);} Catch (Exception e) {e.printstacktrace ();}}});} private void Download (String Dl) throws Exception {if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES. Gingerbread) {Intent service = new Intent (this, downloadservice.class); Service.putextra (downloadservice.intent_url, dl); StartService (service);} else {Intent Intent = new Intent (intent.action_view, uri.parse (dl)); startactivity (Intent);}}}</pre><br><span style="font-size:14px"><span style="font-size:14px"><br></span></span><span style="font-size:14px"><span style="font-size:14px">2) Download Module</span></span><span style="font-size:14px"><span style="font-size:14px"></span></span><pre name="code" class="java">Package Com.example.updataapk;import Java.io.file;import Java.io.fileoutputstream;import java.io.InputStream; Import Java.net.httpurlconnection;import Java.net.url;import Android.app.notification;import Android.app.notificationmanager;import Android.app.pendingintent;import Android.app.service;import Android.content.context;import Android.content.intent;import Android.net.uri;import Android.os.Handler;import Android.os.ibinder;import Android.os.message;import Android.util.log;import Android.widget.remoteviews;public Class Downloadservice extends Service {//notification name private String notify_name = "apk downloading ...";p ublic static final Str ing intent_url = "URL";p rivate Context mcontext = this; Notification mnotification;private static final int notify_id = 0;private Notificationmanager mnotificationmanager;/* Download package installation path */private static final string savepath = Function_utility.getupgradepath ();p rivate static final string Savefilena me = Savepath + "demo.apk";p rivate String apkurl;private int PROgress;boolean canceled;private Thread downloadthread; @Overridepublic ibinder onbind (Intent Intent) {return null;} @Overridepublic void onCreate () {super.oncreate (); mnotificationmanager = (notificationmanager) Getsystemservice ( android.content.Context.NOTIFICATION_SERVICE);} public int Onstartcommand (Intent Intent, int flags, int startid) {log.i ("downloadservice", "intent=" + intent.tostring () + " ; flags= "+ Flags +"; Startid "+ startid)" if (intent.hasextra (downloadservice.intent_url)) {apkurl = (String) Intent.getextras (). Get ( downloadservice.intent_url);} progress = 0;setupnotification (); new Thread () {public void run () {//start download Startdownload ();};}. Start (); return startid;}; private void startdownload () {canceled = false;downloadapk ();} Private Handler Mhandler = new Handler () {@Overridepublic void handlemessage (Message msg) {super.handlemessage (msg); Switch (msg.what) {case 0://complete download/cancel notification mnotificationmanager.cancel (notify_id); installapk (); break;case 2:// This is a manual cancellation of the user interface, so it passes the Activity OnDestroy (); method//cancel notification mnotificationmanager.cancel (notify_id); break;case 1:int rate = Msg.arg1;if (rate < 100) { Remoteviews Contentview = mnotification.contentview;contentview.settextviewtext (R.id.tv_progress, rate + "%"); Contentview.setprogressbar (r.id.progressbar, +, rate, false);} else {//after Completing the download change notification form mnotification.flags = Notification.flag_auto_cancel;mnotification.contentview = null; Mnotification.setlatesteventinfo (mcontext, "complete download", "file completed download", null); stopself ();//stop service itself}pendingintent ContentIntent2 = pendingintent.getactivity (getapplicationcontext (), 0, new Intent (), Pendingintent.flag_update_ current); mnotification.contentintent = contentintent2;mnotificationmanager.notify (notify_id, mNotification); ; Case 3:mnotification.flags = notification.flag_auto_cancel; Remoteviews Contentview = new Remoteviews (getpackagename (), r.layout.update_download_notification_layout); Contentview.settextviewtext (r.id.name, "download failed");//specify personalization View Mnotification.contentview = contentview;intent inteNT = new Intent (getapplicationcontext (), mainactivity.class); Pendingintent contentintent = pendingintent.getactivity (getapplicationcontext (), 0, intent, pendingintent.flag_ Update_current);//specify Content Intent Mnotification.contentintent = contentintent;mnotificationmanager.notify (NOTIFY_ID, mnotification); stopself ();//stop Service itself break;}}};/ * * Install apk * * @param url */private void installapk () {File apkfile = new File (savefilename); if (!apkfile.exists ()) {retu rn;} Intent i = new Intent (intent.action_view); i.setflags (intent.flag_activity_new_task); i.setdataandtype (uri.parse (" file://"+ apkfile.tostring ())," application/vnd.android.package-archive "); mcontext.startactivity (i);} private int lastrate = 0;private InputStream is = null;private FileOutputStream fos = null;/** * Download apk * * @param URL */p rivate void downloadapk () {downloadthread = new Thread (mdownapkrunnable);d Ownloadthread.start ();} Private Runnable mdownapkrunnable = new Runnable () {@Overridepublic void run () {try {url url = new URL (apkurl); HttpURLConnection conn = (httpurlconnection) url.openconnection (); conn.connect (); int length = Conn.getcontentlength (); is = Conn.getinputstream (); File File = new file (savepath), if (!file.exists ()) {file.mkdirs ();} String Apkfile = savefilename; File Apkfile = new file (apkfile); fos = new FileOutputStream (apkfile); int count = 0;byte buf[] = new byte[1024];d o {int num Read = Is.read (buf); count + = Numread;progress = (int) (((float) Count/length) * 100);//update Progress message msg = Mhandler.obtai Nmessage (); msg.what = 1;msg.arg1 = progress;if (progress >= lastrate + 1) {mhandler.sendmessage (msg); lastrate = progres s;} If (numread <= 0) {mhandler.sendemptymessage (0);//complete download notification installation//download finished, cancelled also set canceled = true;break;} Fos.write (buf, 0, numread);} While (!canceled);//click Cancel to stop the Download. LOG.I ("downloadservice----------canceled", canceled + ""); fos.close (); is.close ();} Catch (Exception e) {Message msg = mhandler.obtainmessage (); msg.what = 3;mhandler.sendmessage (msg); e.printstacktrace () ;} Finally {try {if (fos!= Null) {fos.close ();} Is.close (); If (is = = Null) {is.close ();}} Catch (Exception e) {e.printstacktrace ();}}}};/ * * Create notification */private void setupnotification () {int icon = r.drawable.ic_launcher; Charsequence Tickertext = "start download"; Long when = system.currenttimemillis (); mnotification = new Notification (icon, tickertext, when);; /placed in the "executing" column Mnotification.flags = notification.flag_ongoing_event; Remoteviews Contentview = new Remoteviews (getpackagename (), r.layout.update_download_notification_layout); Contentview.settextviewtext (r.id.name, notify_name);//specify personalization View Mnotification.contentview = contentview; Pendingintent contentintent = pendingintent.getactivity (this, 0, new Intent (), pendingintent.flag_update_current);// Specify content Intent Mnotification.contentintent = contentintent;mnotificationmanager.notify (notify_id, mNotification);}}</pre><br>3) Download the address to your phone<span style="font-size:14px"><span style="font-size:14px"></span></span><pre name="code" class="java">Package Com.example.updataapk;import Java.io.file;import Java.io.ioexception;import Android.annotation.suppresslint;import android.content.context;import android.os.Environment; @SuppressWarnings (" Deprecation ") @SuppressLint ({" defaultlocale "," simpledateformat "}) public class function_utility {private static Context mappcontext;public static void Setappcontext (context Context) {mappcontext = context;} public static Context Getappcontext () {return mappcontext;} /** * Download to SD card address */public static string Getupgradepath () {string FilePath = Getapprootpath () + "/upgrade/"; File File = new file (filePath), if (!file.isdirectory ()) {file.mkdirs ();} File = Null;return filePath;} public static string Getapprootpath () {string filePath = "/weimicommunity"; if (environment.getexternalstoragestate (). equals (ENVIRONMENT.MEDIA_MOUNTED)) {filePath = environment.getexternalstoragedirectory () + filePath;} Else {filePath = Getappcontext (). getcachedir () + filePath;} File File = new file (filePath); if (!file.exists ()) {fiLe.mkdirs ();} File = null; File Nomedia = new file (filePath + "/.nomedia"), if (!nomedia.exists ()) try {nomedia.createnewfile ();} catch (ioexception E ) {e.printstacktrace ();} Return filePath;}}</pre><br>4) Androidmanifest.xml<span style="font-size:14px"><span style="font-size:14px"></span></span><pre name="code" class="html"><?xml version= "1.0" encoding= "utf-8"? ><manifest xmlns:android= "http://schemas.android.com/apk/res/ Android "package=" com.example.updataapk "android:versioncode=" 1 "android:versionname=" 1.0 "> <USES-SDK android:minsdkversion= "8" android:targetsdkversion= "/> <uses-permission android:name=" android.pe Rmission. INTERNET "/> <uses-permission android:name=" Android.permission.WRITE_EXTERNAL_STORAGE "/> <uses-permiss Ion Android:name= "android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/> <application android:allowbackup= "true" android:icon= "@drawable/ic_launcher" android:label= "@string/app_name" android:theme= "@style/apptheme "> <activity android:name=" com.example.updataapk.MainActivity "android:label=" @string/ App_name "> <intent-filter> <action android:name=" Android.intent.action.MAIN "/> <category andRoid:name= "android.intent.category.LAUNCHER"/> </intent-filter> </activity> <s Ervice android:name= "com.example.updataapk.DownloadService" > </service> </application></manif Est></pre><br><span style="font-size:14px"><span style="font-size:14px">:</span></span><span style="font-size:14px"><span style="font-size:14px"><p>watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqvywfhd3fxcq==/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/ Dissolve/70/gravity/southeast "><br><span style="font-size:14px"><br></span><span style="font-size:14px">install it yourself when you are finished downloading</span> <span style="font-size:14px">Demo:</span><span style="font-size:14px"><br></span><span style="font-size:14px">http://download.csdn.net/detail/aaawqqq/8040081<br></span><br></p><p style="font-size:12px;">Copyright Notice: This article Bo master original articles, blogs, without consent may not be reproduced.</p><p>Android upgrade download Their definition updates compatible version</p></span></span></span>