Android API 30中getPackageManager.resolveActivity()方法在使用ACTION_VIEW时返回null

4
我正在尝试在Android Studio 4.0中使用codelabs的一些代码,并在应用程序中使用了以下代码:
String uri = uritext.getText().toString();//uritext is an editText view
Uri webpage = Uri.parse(uri);
Intent intent = new Intent(Intent.ACTION_VIEW, webpage);
if(intent.resolveActivity(getApplicationContext().getPackageManager())!=null){
    startActivity(intent);
}
else {
    Log.d("implicitintent:","cant handle this implicit intent!");
    Toast toast = Toast.makeText(MainActivity.this,"cant open website:" + uri,Toast.LENGTH_SHORT);
    toast.show();
}

我已经在两个不同的AVD中尝试了上面的代码,并且在我尝试在按钮单击事件中执行上面的代码时,它们都显示了toast消息。这意味着resolveActivity()返回null。我找不到任何可能的原因,因为我发现Google Chrome已安装在两个AVDs上。Logcat显示以下行:

2020-07-14 17:39:05.829 392-392/? E/netmgr: Failed to open QEMU pipe 'qemud:network': Invalid argument
2020-07-14 17:39:08.229 395-395/? E/wifi_forwarder: RemoteConnection failed to initialize: RemoteConnection failed to open pipe
2020-07-14 17:39:28.490 1889-2010/system_process E/ClipboardService: Denying clipboard access to com.android.chrome, application is not in focus neither is a system service for user 0
2020-07-14 17:39:31.234 1889-3470/system_process W/system_server: JNI critical lock held for 18.962ms on Thread[120,tid=3470,Runnable,Thread*=0xadd6a010,peer=0x13dc0a38,"Binder:1889_13"]

从Logcat来看,似乎是Android Studio内部出了问题。我在谷歌上进行了一些研究,但没有找到任何合适的答案。请有人帮助我解决这个问题,因为我被卡住了。非常感谢您提前的帮助。

1个回答

10

从Android 11开始,使用包管理器对包的可见性有了新的限制。 getPackageManager().resolveActivity() --- 返回null

更多信息

为了获取可用的包 - 必须在您的清单中添加查询。

示例:

<queries>
        <!-- WebView -->
        <intent>
            <action android:name="android.intent.action.VIEW" />
            <category android:name="android.intent.category.BROWSABLE" />
            <data android:scheme="http" />
        </intent>
        <intent>
            <action android:name="android.intent.action.VIEW" />
            <category android:name="android.intent.category.BROWSABLE" />
            <data android:scheme="https" />
        </intent>

        <!-- Camera -->
        <intent>
            <action android:name="android.media.action.IMAGE_CAPTURE" />
        </intent>

        <!-- Gallery -->
        <intent>
            <action android:name="android.intent.action.GET_CONTENT" />
        </intent>
</queries>

这应该放置在AndroidManifest.xml<application></application>标签之外。 这里列出了不同查询用例的列表。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接