java - 检查下载管理器是否下载了文件

标签 java android android-download-manager android-install-apk

如何检查文件是否已下载并运行其安装?我有一个代码:

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 中关闭)。希望这会有所帮助。

更新

您不需要调用我提到的方法。您只需要在您的 list 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>

关于java - 检查下载管理器是否下载了文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29335110/

相关文章:

java - 阻止应用程序调用我的 Activity

java - Slick2D 字体不起作用

java - FIleNotFound 打开失败 : EACCES (Permission denied)

java - Spring 和 ExtJS "400 Bad Request"使用 PUT 但不使用 POST

android - 无法在 Android Studio 中打开项目结构选项卡

android - 如何在Android(Glass)中使用外部麦克风作为音频源

Android:下载管理器和 COMPONENT_ENABLED_STATE_DISABLED_UNTIL_USED

android - DownloadManager 进度条(带百分比)

java.text.ParseException : Unparseable date: "531772200000"