Android learning----using Downloadmanager for version upgrades
Last Update:2016-05-07
Source: Internet
Author: User
<span id="Label3"></p>Reference http://www.trinea.cn/android/android-downloadmanager/<p><p>http://jk2k.com/2016/01/install-apk-automatically-after-the-download-is-complete-with-download-manager/<br></p></p><p><p><br></p></p>Effect:<p><p><br></p></p><p><p>The Notification bar displays</p></p><p><p><br></p></p>Results<p><p>Do not write the demo directly to see the code needs can be directly used to</p></p><p><p></p></p><pre code_snippet_id="1674366" snippet_file_name="blog_20160506_1_4677678" name="code" class="java">public class Versionupdate {private downcompletereceiver downcompletereceiver; private static Versionupdate versionupdate = new Versionupdate (); public static versionupdate newinstance () {return versionupdate; } private versionupdate () {} public void gotoupdate (context context, string Url) {string appname=context . getresources (). getString (r.string.app_name); Downloadmanager Downloadmanager = (downloadmanager) Context.getsystemservice (context. download_service); Downloadmanager.request Request = new Downloadmanager.request (uri.parse (url)); Request.settitle (appName); Request.setnotificationvisibility (DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED); Request.setdescription (appname+ "upgrade ..."); Request.setvisibleindownloadsui (true); Long Downloadid = Downloadmanager.enqueue (request); Downcompletereceiver = new Downcompletereceiver (downloadid); Context.registerreceiver (downcompleteReceiver, New Intentfilter (downloadmanager.action_download_complete)); Request.setdestinationinexternalpublicdir (appName, appname+ ". apk"); public void Createdialogupdate (final context context, final String Url) {alertdialog.builder alert=new ALERTD Ialog. Builder (context); Alert.settitle ("update hint"). setmessage ("find new version, update Now?") . setcancelable (false). Setneutralbutton ("cancel", new Dialoginterface.onclicklistener () {@Override publi c void OnClick (dialoginterface dialog, int which) {dialog.dismiss (); }}). Setpositivebutton ("update now", new Dialoginterface.onclicklistener () {@Override public void OnClick (dialoginterface dialog, int which) {gotoupdate (context, url); Dialog.dismiss (); } }); Alert.create (). Show (); } public class Downcompletereceiver extends Broadcastreceiver {long enqueueid; PubLic downcompletereceiver (long enqueueid) {this.enqueueid = enqueueid; } @Override public void OnReceive (context context, Intent Intent) {downloadmanager dm = (downloa Dmanager) Context.getsystemservice (context. download_service); Long id = Intent.getextras (). getlong (downloadmanager.extra_download_id,-1); LOGCATUTILS.D (DownCompleteReceiver.class.getSimpleName (), "id =" + ID + ","); If (enqueueid! = Id) {return; } downloadmanager.query Query = new Downloadmanager.query (); Query.setfilterbyid (enqueueid); Cursor C = Dm.query (query); If (c! = null && c.movetofirst ()) {int columnindex = C.getcolumnindex (downloadmanager.column_statu S); The download failure will also return this broadcast, so be sure to determine if the download succeeds if (downloadmanager.status_successful = = C.getint (columnindex)) { Get the download good apk path String uristring = c.getstring (c.getcolumnindex (downloadmanager.column_local_filename)); Prompt user to install Installapp (uri.parse ("file://" + uristring), context); } c.close (); }} private void Installapp (Uri data, context Context) {Intent promptinstall = new Intent (Intent . action_view). Setdataandtype (data, "application/vnd.android.package-archive"); Flag_activity_new_task can ensure the installation is successful when the app Promptinstall.setflags (intent.flag_activity_new_task) can be opened normally; Context.startactivity (promptinstall); }} public void Unregisterreceiver (context Context) {if (downcompletereceiver!=null) context.unregiste Rreceiver (downcompletereceiver); }}</pre><br><br><p><p></p></p><p><p></p></p><p><p><br></p></p>Use: versionupdate.newinstance (). createdialogupdate (this,url);<p><p><br></p></p><p><p>finally, Remember to cancel the broadcast versionupdate.newinstance (). unregisterreceiver ()</p></p><p><p><br></p></p><br>End<br>Write a rough question to point out<p><p><br></p></p><p><p><br></p></p><p><p><br></p></p><p><p>Android learning----using Downloadmanager for version upgrades</p></p></span>