Android downloadmanager Download and automatically eject the installation

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.