4

I am trying some codes of codelabs in android studio 4.0 and used the following codes in a app:

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

I have tried the above code in two different AVD and both of them showing the toast message when i am trying to execute the above code in a button click event. Which means resolveActivity() is returning a null. I could not find any probable reason as I find that google chrome is installed on both the AVDs. The Logcat is showing following lines:

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"]

From Logcat it seems that may be something inside android studio going wrong.I did some research in google but couldn't find any suitable answer. Any one please help me in this issue as I am stuck here. Any help is highly appreciable. Thanks in advance.

rafsanahmad007
  • 23,683
  • 6
  • 47
  • 62
DebasishDatta
  • 41
  • 1
  • 2

1 Answers1

10

From Android 11 - There is a new restriction on package visibility using package manager. getPackageManager().resolveActivity(). --- returns null

More Information

In order to get the available packages - Queries has to be added into your manifest.

Example:

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

This should be placed outside of the <application></application> tag in AndroidManifest.xml

Here is a list of use cases for different queries.

rafsanahmad007
  • 23,683
  • 6
  • 47
  • 62