[英]Check if Download Manager downloaded the file
如何檢查文件是否已下載並運行其安裝? 我有一個代碼:
public void downloadUpdate(String url){
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));
request.setDescription("Downloading...");
request.setTitle("App Update");
request.allowScanningByMediaScanner();
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
String name = URLUtil.guessFileName(url, null, MimeTypeMap.getFileExtensionFromUrl(url));
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, name);
DownloadManager manager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
manager.enqueue(request);
}
要檢查下載管理器是否已下載該文件,您必須實現BroatcastReceiver。
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (action.equals(DownloadManager.ACTION_DOWNLOAD_COMPLETE)) {
DownloadManager.Query query = new DownloadManager.Query();
query.setFilterById(intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, 0));
DownloadManager manager = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE);
Cursor cursor = manager.query(query);
if (cursor.moveToFirst()) {
if (cursor.getCount() > 0) {
int status = cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_STATUS));
if (status == DownloadManager.STATUS_SUCCESSFUL) {
String file = cursor.getString(cursor.getColumnIndex(DownloadManager.COLUMN_LOCAL_FILENAME));
// So something here on success
} else {
int message = cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_REASON));
// So something here on failed.
}
}
}
}
}
但是,我不確定您是否可以通過編程方式安裝APK。 出於安全考慮,我認為你不能。 對於應用程序更新,我認為您應該使用谷歌版本控制。 當您使用不同的版本號重新部署應用時,用戶應該能夠自動更新(除非用戶在Google Play上關閉)。 希望這會有所幫助。
更新
你不需要調用我提到的方法。 您只需要在清單xml文件中聲明廣播接收器,DownloadManager將在下載完成時調用。 xml看起來如下所示:
<receiver
android:name=".BroadcastReceiver"
android:enabled="true"
android:exported="true" >
<intent-filter>
<action android:name="android.intent.action.DOWNLOAD_COMPLETE" />
<action android:name="android.intent.action.DOWNLOAD_NOTIFICATION_CLICKED" />
</intent-filter>
</receiver>
這是一種相對簡單的方法。 它對我有用:您需要在清單文件中添加<receiver>
標記,如下所示:
<application>
<receiver
android:name= "com.example.checkDownloadComplete" <!-- add desired full name here -->
android:exported="true" >
<intent-filter>
<action android:name="android.intent.action.DOWNLOAD_COMPLETE" />
</intent-filter>
</receiver>
</application>
這將為您下載完成的事件注冊廣播接收器。 這將在下載完成后立即調用類中的onReceive()
方法。 請記住,您需要extend
BroadcastReceiver類,但不要implement
它。 我將布爾變量聲明為切換以檢查下載完成情況。 因此,您的Java類將類似於:
public static class checkDownloadComplete extends BroadcastReceiver{
public static boolean isDownloadComplete= false;
@Override
public void onReceive(Context context, Intent intent) {
isDownloadComplete = true;
Log.i("Download completed?", String.valueOf(isDownloadComplete));
}
}
要等到或檢查您的下載是否已從其他任何類完成,請在所需的適當位置使用以下簡單代碼:
while(!checkDownloadComplete.isDownloadComplete){
// add necessary code to be executed before completion of download
}
//code after completion of download
但請記住,如果您需要在項目中多次檢查,那么您需要事先重置isDownloadComplete
的值。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.