Android下载app完成后自动安装

来源:互联网 发布:天敏网络机顶盒应用 编辑:程序博客网 时间:2024/04/27 23:16

直接上代码了public class MainActivity extends Activity {private DownloadManager dowanloadmanager = null;private DownloadChangeObserver downloadObserver;private long lastDownloadId = 0;public static final Uri CONTENT_URI = Uri.parse("content://downloads/my_downloads");@Overrideprotected void onCreate(Bundle savedInstanceState) {// TODO Auto-generated method stubsuper.onCreate(savedInstanceState);setContentView(R.layout.activity_main);String serviceString = Context.DOWNLOAD_SERVICE;dowanloadmanager = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);Uri uri = Uri.parse("http://filelx.liqucn.com/upload/2014/sheji/ms_hdl_daiji_lq_20140811.apk");Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).mkdir();lastDownloadId = dowanloadmanager.enqueue(new DownloadManager.Request(uri).setAllowedNetworkTypes(DownloadManager.Request.NETWORK_MOBILE| DownloadManager.Request.NETWORK_WIFI).setAllowedOverRoaming(false).setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "test.apk"));registerReceiver(receiver, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));downloadObserver = new DownloadChangeObserver(null);getContentResolver().registerContentObserver(CONTENT_URI, true,downloadObserver);}class DownloadChangeObserver extends ContentObserver {public DownloadChangeObserver(Handler handler) {super(handler);// TODO Auto-generated constructor stub}@Overridepublic void onChange(boolean selfChange) {queryDownloadStatus();}}private BroadcastReceiver receiver = new BroadcastReceiver() {@Overridepublic void onReceive(Context context, Intent intent) {// 这里可以取得下载的id,这样就可以知道哪个文件下载完成了。适用与多个下载任务的监听long myDwonloadID = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, -1);if (lastDownloadId == myDwonloadID) {try {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);} catch (Exception e) {// TODO: handle exceptione.printStackTrace();Log.e("异常", "install error ", e);}}queryDownloadStatus();}};private void queryDownloadStatus() {DownloadManager.Query query = new DownloadManager.Query();query.setFilterById(lastDownloadId);Cursor c = dowanloadmanager.query(query);if (c != null && c.moveToFirst()) {int status = c.getInt(c.getColumnIndex(DownloadManager.COLUMN_STATUS));int reasonIdx = c.getColumnIndex(DownloadManager.COLUMN_REASON);int titleIdx = c.getColumnIndex(DownloadManager.COLUMN_TITLE);int fileSizeIdx = c.getColumnIndex(DownloadManager.COLUMN_TOTAL_SIZE_BYTES);int bytesDLIdx = c.getColumnIndex(DownloadManager.COLUMN_BYTES_DOWNLOADED_SO_FAR);String title = c.getString(titleIdx);int fileSize = c.getInt(fileSizeIdx);int bytesDL = c.getInt(bytesDLIdx);// Translate the pause reason to friendly text.int reason = c.getInt(reasonIdx);StringBuilder sb = new StringBuilder();sb.append(title).append("\n");sb.append("Downloaded ").append(bytesDL).append(" / ").append(fileSize);// Display the statusLog.d("tag", sb.toString());switch (status) {case DownloadManager.STATUS_PAUSED:Log.v("tag", "STATUS_PAUSED");case DownloadManager.STATUS_PENDING:Log.v("tag", "STATUS_PENDING");case DownloadManager.STATUS_RUNNING:// 正在下载,不做任何事情Log.v("tag", "STATUS_RUNNING");break;case DownloadManager.STATUS_SUCCESSFUL:// 完成Log.v("tag", "下载完成");// dowanloadmanager.remove(lastDownloadId);break;case DownloadManager.STATUS_FAILED:// 清除已下载的内容,重新下载Log.v("tag", "STATUS_FAILED");dowanloadmanager.remove(lastDownloadId);break;}}}@Overrideprotected void onDestroy() {// TODO Auto-generated method stubsuper.onDestroy();unregisterReceiver(receiver);getContentResolver().unregisterContentObserver(downloadObserver);}


0 0
原创粉丝点击