2014-01-21 45 views
0

嗨,我正在下载一个文件,并且我想要在完成下载后触发一个ACTION_VIEW Intent。所以没有人知道如何检测DownloadManager何时完成。当DownloadManager下载完成时,Android会检测到

继承人如何我正在下载管理器

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

回答

0

它看起来OK我。那个例子不起作用?但不同的是你如何在onReceive方法中引发下一个Intent。请尝试用你的行动 myIntent = new Intent(YourActionClass.class);

0

想通了我自己,我需要设置mime类型,所以它知道哪些程序来寻找

相关问题