gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-11-01 08:13:43 26 4
gpt4 key购买 nike

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

但是当使用 Intent.ACTION_VIEW 时, Activity 崩溃了。

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 here .

LogCat 会输出以下内容:

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}

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

最佳答案

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 为您找到合适的 Activity 。

关于尝试从 BroadcastReceiver 中触发 Intent.ACTION_VIEW 时 Android Activity 崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6956891/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com