【问题标题】:Check if Download Manager downloaded the file检查下载管理器是否下载了文件
【发布时间】:2015-06-02 19:31:09
【问题描述】:

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

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

【问题讨论】:

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


    【解决方案1】:

    要检查下载管理器是否下载了文件,您必须实现您的 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>
    

    【讨论】:

    • 这个方法怎么调用?
    • 您不需要调用,只需扩展 BroadcastReceiver 并在您的清单 xml 中进行配置。我已经更新了答案以获取更多详细信息。
    • 请问如何检查下载的多个文件..我必须使用while循环我不知道如何使用请更新多个文件的答案先生...
    【解决方案2】:

    这是一种相对简单的方法。它对我有用: 你需要在manifest文件中添加&lt;receiver&gt;标签,如下:

     <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 的值。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-01-06
      • 1970-01-01
      • 2011-09-20
      • 2018-06-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多