Android DownloadManager下載並自動彈出安裝(轉載)
1.版本2.3以上
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"/>
只能貼程式碼,本想上傳demo的,找不到地方
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);
// 設定下載路徑和檔名
request.setDestinationInExternalPublicDir("download", "time2plato.apk");//備註(1)
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();
}
}
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); }
}
}
最後記者一定要註冊廣播
<receiver
android:name="com.example.apkdownloadmanager.DownLoadBroadcastReceiver">
<intent-filter>
<action android:name="android.intent.action.DOWNLOAD_COMPLETE" />
</intent-filter>
</receiver>
備註(1):
file是通過file.mkdir()建立的,這樣如果上級目錄不存在就會新建資料夾異常。所以下載前我們最好自己呼叫File的mkdirs方法遞迴建立子目錄,如下:
Java12 | File folder=newFile(folderName);return(folder.exists()&&folder.isDirectory())?true:folder.mkdirs(); |
否則,會報異常
1 2 | java.lang.IllegalStateException:Unabletocreatedirectory:/storage/sdcard0/Trinea/aa atandroid.app.DownloadManager$Request.setDestinationInExternalPublicDir(DownloadManager.java) |