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

发布于 2024-11-28 11:57:48 字数 7606 浏览 2 评论 0原文

我的活动应该下载一个文件,然后通过发送 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);
    }
}

正如您所看到的,我已经尝试添加 此处提到的 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):下载 = STATUS_SUCCESSFUL。

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

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

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

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

08-05 13:04:46.195: 错误/AndroidRuntime(2449): 致命异常:main

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: 错误/AndroidRuntime(2449): 在 android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:722)

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

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

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

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

08-05 13:04:46.195: 错误/AndroidRuntime(2449): 在 java.lang.reflect.Method.invokeNative(本机方法)

08-05 13:04:46.195: 错误/AndroidRuntime(2449): 在 java.lang.reflect.Method.invoke(Method.java:507)

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

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

08-05 13:04:46.195: 错误/AndroidRuntime(2449): 在dalvik.system.NativeStart.main(本机方法)

08-05 13:04:46.195:错误/AndroidRuntime(2449):导致:android.content.ActivityNotFoundException:找不到处理 Intent 的 Activity { act=android.intent.action.VIEW dat=content://下载/my_downloads/24 flg=0x10000000 }

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

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

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

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

08-05 13:04:46.195: 错误/AndroidRuntime(2449): 在 ***.RESTTestDownloadActivity$1.onReceive(RESTTestDownloadActivity.java:75)

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

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

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

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

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

My activity should download a file and afterwards open it by sending Intent.ACTION_VIEW. The download itself works fine, I can access it afterwards in the Downloads section.
If I'm using a DownloadManager.ACTION_VIEW_DOWNLOADS Intent instead of Intent.ACTION_VIEW it also works fine.

But when using Intent.ACTION_VIEW the activity crashes.

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

As you can see I already tried to add the FLAG_ACTIVITY_NEW_TASK as mentioned here.

LogCat does output the following:

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

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

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

08-05 13:04:36.046: INFO/DownloadManager(274): Initiating request for download 24

08-05 13:04:40.956: DEBUG/dalvikvm(216): GC_EXPLICIT freed 26K, 47% free 3220K/6023K, external 6059K/7285K, paused 68ms

08-05 13:04:45.855: DEBUG/dalvikvm(157): GC_CONCURRENT freed 795K, 56% free 2872K/6471K, external 2402K/2630K, paused 10ms+15ms

08-05 13:04:46.135: INFO/System.out(2449): Download 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): Starting: Intent { act=android.intent.action.VIEW dat=content://downloads/my_downloads/24 flg=0x10000000 } from pid 2449

08-05 13:04:46.175: DEBUG/AndroidRuntime(2449): Shutting down VM

08-05 13:04:46.185: WARN/dalvikvm(2449): threadid=1: thread exiting with uncaught exception (group=0x40015560)

08-05 13:04:46.195: ERROR/AndroidRuntime(2449): FATAL EXCEPTION: main

08-05 13:04:46.195: ERROR/AndroidRuntime(2449): java.lang.RuntimeException: Error receiving broadcast Intent { act=android.intent.action.DOWNLOAD_COMPLETE pkg=* (has extras) } in *.RESTTestDownloadActivity$1@405376e8

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

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

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

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

08-05 13:04:46.195: ERROR/AndroidRuntime(2449): at 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): Caused by: android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.VIEW dat=content://downloads/my_downloads/24 flg=0x10000000 }

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

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

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

08-05 13:04:46.195: ERROR/AndroidRuntime(2449): at 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): at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:709)

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

08-05 13:04:46.216: WARN/ActivityManager(77): Force finishing activity *.RESTTestDownloadActivity

08-05 13:04:46.739: WARN/ActivityManager(77): Activity pause timeout for HistoryRecord{408abbf0 *.RESTTestDownloadActivity}

Any idea why only Intent.ACTION_VIEW results in a crash?
Thanks in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

手心的海 2024-12-05 11:57:48
android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.VIEW dat=content://downloads/my_downloads/24 flg=0x10000000 }

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

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

Please add a MIME type to your Intent, to help Android find the appropriate activity for you.

雪化雨蝶 2024-12-05 11:57:48

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

Try saving the file on your SD card and then firing an intent with ACTION_VIEW. For this to work, you need to set the intent data with Uri.fromFile(file_object_pointing_to_your_file) method. (Uri.parse() method will cause a crash in this case)

乖乖哒 2024-12-05 11:57:48

您收到此错误是因为您使用的是 DownloadManager.ACTION_VIEW_DOWNLOADS Intent 而不是 Intent.ACTION_VIEW,并且对于 Intent.ACTION_VIEW 中有一个活动您的应用程序或任何其他可以处理您的查看请求的应用程序。我的意思是您正在下载的项目无法通过任何活动打开。

因此,您要么创建自己的活动来打开您的项目,要么必须安装外部 apk 来打开您的项目。

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

You are getting this error because You are using using DownloadManager.ACTION_VIEW_DOWNLOADS Intent instead of Intent.ACTION_VIEW and for Intent.ACTION_VIEW there is an activity in your app or any other app which can handle your view request.I mean item which you are downloading can not be opened by any activity.

SO you have either create your own activity which can open your item or you have to install external apk which can open your item.

So best choice is that you have to use DownloadManager.ACTION_VIEW_DOWNLOADS

优雅的叶子 2024-12-05 11:57:48

我有一个类似的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);
}

I had a similar android.content.ActivityNotFoundException. For me, it helped to convert the "content" URI to a "file" URI first:

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);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文