apk更新、下载、安装(四)---DownloadManager ui方式

点击打开链接,下载demo............

activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.demo.MainActivity" >

    <Button
        android:id="@+id/mBut_download"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView1"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="30dp"
        android:text="Button" />

</RelativeLayout>


MainActivity

package com.example.demo;

import java.io.File;

import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.app.Activity;
import android.app.DownloadManager;
import android.app.DownloadManager.Request;
import android.content.Context;
import android.content.SharedPreferences;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

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) {
				intoDownloadManager();

			}

		});

	}

	private boolean isFolderExist(String dir) {
		File folder = Environment.getExternalStoragePublicDirectory(dir);
		return (folder.exists() && folder.isDirectory()) ? true : folder
				.mkdirs();
	}

	@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");

		final String EXTERNAL_DIR = "download";
		boolean folderExist = isFolderExist(EXTERNAL_DIR);

		Request request = new Request(uri);

		// 表示设置下载地址为sd卡的download文件夹,文件名为time2plato.apk。

		if (folderExist) {
			request.setDestinationInExternalPublicDir("download",
					"time2plato.apk");
		}

		request.setDescription("柏拉图新版本下载");

		request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);

		request.setMimeType("application/vnd.android.package-archive");

		// 设置为可被媒体扫描器找到

		request.allowScanningByMediaScanner();

		// 设置为可见和可管理

		request.setVisibleInDownloadsUi(true);

		long refernece = dManager.enqueue(request);

		// 把当前下载的ID保存起来

		SharedPreferences sPreferences = getSharedPreferences("downloadplato",
				0);

		sPreferences.edit().putLong("plato", refernece).commit();

	}

}

DownLoadBroadcastReceiver

package com.example.demo;

import android.annotation.SuppressLint;
import android.app.DownloadManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;

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);
		}

	}

}

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.demo"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="18"
        android:targetSdkVersion="18" />

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.INSTALL_PACKAGES" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".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>

        <receiver android:name="com.example.demo.DownLoadBroadcastReceiver" >
            <intent-filter>
                <action android:name="android.intent.action.DOWNLOAD_COMPLETE" />
            </intent-filter>
        </receiver>
    </application>

</manifest>


你可能感兴趣的:(apk更新、下载、安装(四)---DownloadManager ui方式)