1. Version 2.3 or higher
2. <uses-permission android:name= "Android.permission.INTERNET"/>
<uses-permission android:name= "Android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name= "Android.permission.DOWNLOAD_WITHOUT_NOTIFICATION"/>
Can only paste code, I want to upload the demo, can't find the place
public class Mainactivity extends Activity {
Private Button mbut_download;
@Override
protected void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.activity_main);
Mbut_download = (Button) Findviewbyid (r.id.mbut_download);
Mbut_download.setonclicklistener (New Onclicklistener () {
@Override
public void OnClick (View v) {
TODO auto-generated Method Stub
Intodownloadmanager ();
}
});
}
@SuppressLint ("Newapi")
@TargetApi (build.version_codes. Gingerbread)
private void Intodownloadmanager () {
Downloadmanager Dmanager = (downloadmanager) getsystemservice (Context.download_service);
Uri uri = uri.parse ("http://dingphone.ufile.ucloud.com.cn/apk/guanwang/time2plato.apk");
Request Request = new request (URI);
Set the download path and file name
Request.setdestinationinexternalpublicdir ("Download", "time2plato.apk");
Request.setdescription ("Peratu version download");
Request.setnotificationvisibility (DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
Request.setmimetype ("application/vnd.android.package-archive");
Set to be found by media scanner
Request.allowscanningbymediascanner ();
Set to visible and manageable
Request.setvisibleindownloadsui (TRUE);
Long refernece = dmanager.enqueue (request);
Save the current download ID.
Sharedpreferences spreferences = getsharedpreferences ("Downloadplato", 0);
Spreferences.edit (). Putlong ("Plato", Refernece). commit ();
}
}
public class Downloadbroadcastreceiver extends Broadcastreceiver {
@SuppressLint ("Newapi")
public void OnReceive (context context, Intent Intent) {
Long Mydwonloadid = Intent.getlongextra (downloadmanager.extra_download_id,-1);
Sharedpreferences spreferences = context.getsharedpreferences ("Downloadplato", 0);
Long refernece = Spreferences.getlong ("Plato", 0);
if (refernece = = Mydwonloadid) {
String servicestring = Context.download_service;
Downloadmanager Dmanager = (downloadmanager) context.getsystemservice (servicestring);
Intent install = new Intent (Intent.action_view);
Uri Downloadfileuri = Dmanager.geturifordownloadedfile (Mydwonloadid);
Install.setdataandtype (Downloadfileuri, "application/vnd.android.package-archive");
Install.addflags (Intent.flag_activity_new_task);
Context.startactivity (install); }
}
}
Finally, reporters must register for the broadcast.
<receiver
Android:name= "Com.example.apkdownloadmanager.DownLoadBroadcastReceiver" >
<intent-filter>
<action android:name= "Android.intent.action.DOWNLOAD_COMPLETE"/>
</intent-filter>
</receiver>
Android downloadmanager Download and automatically eject the installation