2014-01-21 85 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類型,所以它知道哪些程序來尋找

相關問題