简体   繁体   English

检查Download Manager是否下载了该文件

[英]Check if Download Manager downloaded the file

How can I check if file has been downloaded and run its installation? 如何检查文件是否已下载并运行其安装? I have a code: 我有一个代码:

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

To check whether the download manager downloaded the file, you must implements your BroatcastReceiver. 要检查下载管理器是否已下载该文件,您必须实现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.
                }
            }
        }
    }
}

However, I am not sure whether you can install the APK programmatically. 但是,我不确定您是否可以通过编程方式安装APK。 For security reason, I don't think you can. 出于安全考虑,我认为你不能。 For application update, I think you should use google versioning control. 对于应用程序更新,我认为您应该使用谷歌版本控制。 When you re-deploy your app using different version number, the users should able to update automatically (unless user turn off at google play). 当您使用不同的版本号重新部署应用时,用户应该能够自动更新(除非用户在Google Play上关闭)。 Hope that this will help. 希望这会有所帮助。

Update 更新

You do not need to call the method that I mention. 你不需要调用我提到的方法。 You just need to declare your broadcast receiver at your manifest xml file and DownloadManager will call at when download complete. 您只需要在清单xml文件中声明广播接收器,DownloadManager将在下载完成时调用。 The xml look something like below: 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>

This is relatively an easy method. 这是一种相对简单的方法。 It worked for me: You need to add the <receiver> tag in the manifest file as follows: 它对我有用:您需要在清单文件中添加<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>

This will register a Broadcast Receiver for an event where your download is completed. 这将为您下载完成的事件注册广播接收器。 This will call the onReceive() method in your class as soon as download completes. 这将在下载完成后立即调用类中的onReceive()方法。 Remember that you need to extend the BroadcastReceiver class, but not implement it. 请记住,您需要extend BroadcastReceiver类,但不要implement它。 I declared a boolean variable as a toggle to check completion of download. 我将布尔变量声明为切换以检查下载完成情况。 Hence, your Java class will be something like: 因此,您的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));
     }

}

To wait until or check whether your download has completed from any other class, use the following simple code in the desired appropriate place: 要等到或检查您的下载是否已从其他任何类完成,请在所需的适当位置使用以下简单代码:

while(!checkDownloadComplete.isDownloadComplete){

    // add necessary code to be executed before completion of download
}
//code after completion of download

But remember that if you need to check it multiple times in your project, then you'll need to reset the value of isDownloadComplete beforehand. 但请记住,如果您需要在项目中多次检查,那么您需要事先重置isDownloadComplete的值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM