尝试从BroadcastReceiver中触发Intent.ACTION_VIEW时,Android Activity崩溃

时间:2011-08-05 13:16:12

标签: android android-activity crash android-intent broadcastreceiver

我的活动应该下载一个文件,然后通过发送Intent.ACTION_VIEW打开它。下载本身工作正常,我可以在下载部分访问它。 如果我使用DownloadManager.ACTION_VIEW_DOWNLOADS Intent而不是Intent.ACTION_VIEW,它也可以正常工作。

但是当使用 Intent.ACTION_VIEW 时,活动会崩溃。

public class RESTTestDownloadActivity extends SOFAActivity {

private DownloadManager downloadManager;
private IntentFilter downloadFilter;
private BroadcastReceiver downloadReceiver;
private long downloadID;

@Override
public void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);

    setTitle("RESTTestDownloadActivity");

    downloadManager = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);

    TestGetAttachment();
}

private void TestGetAttachment(){

    downloadFilter = new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE); 

    DownloadManager.Request request = new DownloadManager.Request(Uri.parse("http://address:port/directory/testGetAttachment"));
    downloadID = downloadManager.enqueue(request);

    downloadReceiver = new BroadcastReceiver() {

        @Override
        public void onReceive(Context context, Intent intent) {
            // TODO Auto-generated method stub
            Toast.makeText(RESTTestDownloadActivity.this, "ACTION_DOWNLOAD_COMPLETE received.", Toast.LENGTH_LONG);
            DownloadManager.Query query = new DownloadManager.Query();
            query.setFilterById(downloadID);
            Cursor cursor = downloadManager.query(query);
            if(cursor.moveToFirst()){
                System.out.println("Download matches.");
                int columnIndex = cursor.getColumnIndex(DownloadManager.COLUMN_STATUS);
                int status = cursor.getInt(columnIndex);
                int columnReason = cursor.getColumnIndex(DownloadManager.COLUMN_REASON);
                int reason = cursor.getInt(columnReason);

                if(status!=DownloadManager.STATUS_SUCCESSFUL){
                    System.out.println("Download != STATUS_SUCCESSFUL.");
                    AlertDialogBuilder.setTitle("Error")
                        .setMessage(reason)
                        .setPositiveButton("OK", null)
                        .show();
                } else {
                    System.out.println("Download = STATUS_SUCCESSFUL.");
                    Toast.makeText(RESTTestDownloadActivity.this, "Download successful.", Toast.LENGTH_LONG).show();
                    Uri dlUri = Uri.parse(cursor.getString(cursor.getColumnIndex(DownloadManager.COLUMN_LOCAL_URI)));
                    System.out.println("Download-URI: " + dlUri.toString());
                    unregisterReceiver(downloadReceiver);
                    downloadReceiver = null;
                    // This is working perfectly fine
                    startActivity(new Intent(DownloadManager.ACTION_VIEW_DOWNLOADS));
                    // But this is crashing
//                  Intent i = new Intent(Intent.ACTION_VIEW, dlUri);
//                  i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//                  startActivity(i);
                }
                downloadManager.remove(downloadID);
                downloadReceiver = null;
            } 
        }
    };
    registerReceiver(downloadReceiver, downloadFilter);

}

@Override
public void onPause(){
    super.onPause();
    if(downloadReceiver!=null){
        System.out.println("Unregistering downloadReceiver...");
        unregisterReceiver(downloadReceiver);
    }
}
@Override
public void onResume(){
    super.onResume();
    if(downloadReceiver!=null){
        System.out.println("Registering downloadReceiver...");
        registerReceiver(downloadReceiver, downloadFilter);
    }
}

如您所见,我已尝试按照here添加FLAG_ACTIVITY_NEW_TASK。

LogCat会输出以下内容:

  

08-05 13:04:35.437:INFO / System.out(2449):* .RESTTestDownloadActivity.onResume()

     

08-05 13:04:35.446:INFO / System.out(2449):注册downloadReceiver ...

     

08-05 13:04:35.846:INFO / ActivityManager(77):显示* .RESTTestDownloadActivity:+ 561ms

     

08-05 13:04:36.046:INFO / DownloadManager(274):发起下载请求24

     

08-05 13:04:40.956:DEBUG / dalvikvm(216):GC_EXPLICIT释放26K,47%免费3220K / 6023K,外部6059K / 7285K,暂停68ms

     

08-05 13:04:45.855:DEBUG / dalvikvm(157):GC_CONCURRENT释放795K,56%免费2872K / 6471K,外部2402K / 2630K,暂停10ms + 15ms

     

08-05 13:04:46.135:INFO / System.out(2449):下载gefunden。

     

08-05 13:04:46.135:INFO / System.out(2449):Download = STATUS_SUCCESSFUL。

     

08-05 13:04:46.155:INFO / System.out(2449):Download-URI:content:// downloads / my_downloads / 24

     

08-05 13:04:46.165:INFO / ActivityManager(77):从pid 2449开始:Intent {act = android.intent.action.VIEW dat = content:// downloads / my_downloads / 24 flg = 0x10000000}

     

08-05 13:04:46.175:DEBUG / AndroidRuntime(2449):关闭虚拟机

     

08-05 13:04:46.185:WARN / dalvikvm(2449):threadid = 1:线程退出时未捕获异常(组= 0x40015560)

     

08-05 13:04:46.195:ERROR / AndroidRuntime(2449):致命异常:主

     

08-05 13:04:46.195:ERROR / AndroidRuntime(2449):java.lang.RuntimeException:在*中接收广播Intent {act = android.intent.action.DOWNLOAD_COMPLETE pkg = *(has extras)}时出错。 RESTTestDownloadActivity $ -1 @ 405376e8

     

08-05 13:04:46.195:ERROR / AndroidRuntime(2449):在android.app.LoadedApk $ ReceiverDispatcher $ Args.run(LoadedApk.java:722)

     

08-05 13:04:46.195:ERROR / AndroidRuntime(2449):在android.os.Handler.handleCallback(Handler.java:587)

     

08-05 13:04:46.195:ERROR / AndroidRuntime(2449):在android.os.Handler.dispatchMessage(Handler.java:92)

     

08-05 13:04:46.195:ERROR / AndroidRuntime(2449):在android.os.Looper.loop(Looper.java:123)

     

08-05 13:04:46.195:ERROR / AndroidRuntime(2449):在android.app.ActivityThread.main(ActivityThread.java:3683)

     

08-05 13:04:46.195:ERROR / AndroidRuntime(2449):at java.lang.reflect.Method.invokeNative(Native Method)

     

08-05 13:04:46.195:ERROR / AndroidRuntime(2449):at java.lang.reflect.Method.invoke(Method.java:507)

     

08-05 13:04:46.195:ERROR / AndroidRuntime(2449):at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:839)

     

08-05 13:04:46.195:ERROR / AndroidRuntime(2449):at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)

     

08-05 13:04:46.195:ERROR / AndroidRuntime(2449):at dalvik.system.NativeStart.main(Native Method)

     

08-05 13:04:46.195:ERROR / AndroidRuntime(2449):引起:android.content.ActivityNotFoundException:找不到处理Intent的Activity {act = android.intent.action.VIEW dat = content:// downloads / my_downloads / 24 flg = 0x10000000}

     

08-05 13:04:46.195:ERROR / AndroidRuntime(2449):在android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1409)

     

08-05 13:04:46.195:ERROR / AndroidRuntime(2449):在android.app.Instrumentation.execStartActivity(Instrumentation.java:1379)

     

08-05 13:04:46.195:ERROR / AndroidRuntime(2449):在android.app.Activity.startActivityForResult(Activity.java:2827)

     

08-05 13:04:46.195:ERROR / AndroidRuntime(2449):在android.app.Activity.startActivity(Activity.java:2933)

     

08-05 13:04:46.195:ERROR / AndroidRuntime(2449):at ***。RESTTestDownloadActivity $ 1.onReceive(RESTTestDownloadActivity.java:75)

     

08-05 13:04:46.195:ERROR / AndroidRuntime(2449):在android.app.LoadedApk $ ReceiverDispatcher $ Args.run(LoadedApk.java:709)

     

08-05 13:04:46.195:ERROR / AndroidRuntime(2449):... 9更多

     

08-05 13:04:46.216:WARN / ActivityManager(77):强制完成活动* .RESTTestDownloadActivity

     

08-05 13:04:46.739:WARN / ActivityManager(77):HistoryRecord的活动暂停超时{408abbf0 * .RESTTestDownloadActivity}

知道为什么只有Intent.ACTION_VIEW导致崩溃? 提前谢谢!

4 个答案:

答案 0 :(得分:3)

android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.VIEW dat=content://downloads/my_downloads/24 flg=0x10000000 }

请为您的Intent添加MIME类型,以帮助Android为您找到合适的活动。

答案 1 :(得分:1)

尝试将文件保存在SD卡上,然后使用ACTION_VIEW触发意图。为此,您需要使用Uri.fromFile(file_object_pointing_to_your_file)方法设置intent数据。 (在这种情况下,Uri.parse()方法会导致崩溃)

答案 2 :(得分:0)

您收到此错误是因为您使用的是DownloadManager.ACTION_VIEW_DOWNLOADS Intent而不是Intent.ACTION_VIEW,而Intent.ACTION_VIEW正在使用您的应用或任何其他可以处理您的查看请求的应用中的活动。我的意思是你正在下载的项目不能被任何活动打开。

所以你要么创建自己的活动,可以打开你的项目,要么你必须安装可以打开你的项目的外部apk。

所以最好的选择是你必须使用DownloadManager.ACTION_VIEW_DOWNLOADS

答案 3 :(得分:0)

我有一个类似的android.content.ActivityNotFoundException。对我来说,它首先将“内容”URI转换为“文件”URI:

Uri uri = Uri.parse(cursor.getString(cursor.getColumnIndex(DownloadManager.COLUMN_LOCAL_URI)));
if ("content".equals(uri.getScheme())) {
    Cursor cursor = getContentResolver().query(uri, new String[] { android.provider.MediaStore.Images.ImageColumns.DATA }, null, null, null);
    cursor.moveToFirst();   
    final String filePath = cursor.getString(0);
    cursor.close();
    uri = Uri.fromFile(new File(filePath));
}
if ("file".equals(uri.getScheme()) {
    Intent installIntent = new Intent(Intent.ACTION_VIEW);
    installIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); // Required if launching outside of an activity
    installIntent.setDataAndType(uri, downloadManager.getMimeTypeForDownloadedFile(downloadId));
    startActivity(installIntent);
}