Android下载完成后,Android会检测到

时间:2014-01-21 21:14:14

标签: android

您好我正在下载文件,我想在完成下载后触发ACTION_VIEW Intent。所以有人知道如何检测DownloadManager何时完成。

继承人我是如何运行DownloadManager的

DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));
request.setTitle(filename);
// in order for this if to run, you must use the android 3.2 to compile your app
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
 request.allowScanningByMediaScanner();
 request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);

   }

ContextWrapper c = new ContextWrapper(getBaseContext());
String filePath = c.getFilesDir().getPath();
request.setDestinationInExternalFilesDir(getBaseContext(), filePath, filename);

// get download service and enqueue file
DownloadManager manager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
manager.enqueue(request);
Toast.makeText(getBaseContext(), "Downloading...", Toast.LENGTH_LONG).show();

BroadcastReceiver onComplete = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {

        String path = getFilesDir().getPath() +"/" + filename;
        File f = new File(path); 
        Intent myIntent = new Intent(Intent.ACTION_VIEW);

    myIntent.setData(Uri.fromFile(f));
        startActivity(myIntent);
    }
};

registerReceiver(onComplete, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));

2 个答案:

答案 0 :(得分:0)

对我来说看起来不错。那个例子不起作用?但不同的是你如何在onReceive方法中触发下一个Intent。请尝试使用您的操作 myIntent = new Intent(YourActionClass.class);

答案 1 :(得分:0)

自己想出来我需要设置mimeType,以便知道要查找的应用程序