查看intent.resolveActivity != null 但启动 Intent 会引发 ActivityNotFound 异常,我在打开浏览器或具有深度链接的应用程序时写道:
private fun openUrl(url: String) {
val intent = Intent().apply {
action = Intent.ACTION_VIEW
data = Uri.parse(url)
// setDataAndType(Uri.parse(url), "text/html")
// component = ComponentName("com.android.browser", "com.android.browser.BrowserActivity")
// flags = Intent.FLAG_ACTIVITY_CLEAR_TOP + Intent.FLAG_GRANT_READ_URI_PERMISSION
}
val activityInfo = intent.resolveActivityInfo(packageManager, intent.flags)
if (activityInfo?.exported == true) {
startActivity(intent)
} else {
Toast.makeText(
this,
"No application can handle the link",
Toast.LENGTH_SHORT
).show()
}
}
Run Code Online (Sandbox Code Playgroud)
它不起作用。在 API 30 模拟器中找不到浏览器,而一个常见的解决方案有效:
private fun openUrl(url: String) {
val intent …Run Code Online (Sandbox Code Playgroud) android android-manifest android-intent activitynotfoundexception android-11
我在这里发布这个问题用于教育目的,因为我无法在任何地方找到答案,最终找到了原因,即我自己.
这是有问题的代码:
// initially getting the intent from polling the PackageManager about activities resolving Search intent.
ComponentName componentName = intent.resolveActivity(pm);
if (componentName != null) {
context.startActivity(intent);
}
Run Code Online (Sandbox Code Playgroud)
尽管检查我得到一个ActivityNotFound异常.
编辑:显然这一点对每个人来说并不明显:为什么有一个活动解决了意图,但试图启动它会抛出一个ActivityNotFound异常 - 两个事实显然是矛盾的?
我需要播放存储在我的SD卡位置的3gp音频文件,以便在My HTC设备中使用我的默认媒体播放器播放它.
请帮忙
代码:
public class AudioRecorder extends Activity {
private static final String CAMERA_STATUS = "camera_upload";
private static final String GALLERY_STATUS = "gallery_upload";
MediaRecorder recorder = new MediaRecorder();
static String path = "audio-android.3gp";
Button startRecording;
Button stopRecording;
Button save;
Button palyAudio;
private Context context;
private String sanitizePath(String path) {
if (!path.startsWith("/")) {
path = "/" + path;
}
if (!path.contains(".")) {
path += ".3gp";
}
return Environment.getExternalStorageDirectory().getAbsolutePath()
+ path;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated …Run Code Online (Sandbox Code Playgroud)