Intent Action_dial does not function in android 11
Asked Answered
D

1

6

I am currently developing an android app and needed a function that starts a phone call so I added this code.

 public void dialPhoneNumber(String phoneNumber) {
        Intent intent = new Intent(Intent.ACTION_DIAL);
            intent.setData(Uri.parse("tel:" + phoneNumber));
            if (intent.resolveActivity(getPackageManager()) != null) {
                startActivity(intent);
            }
        }

.. it seems to work perfectly in older android versions but when I test it in android 11 it doesn't function at all I tried action_call and added the permission <uses-permission android:name="android.permission.CALL_PHONE" /> still doesn't work.

Duplicity answered 4/8, 2021 at 23:45 Comment(0)
T
23

Your problem lies in the line of code intent.resolveActivity(getPackageManager()). When you call resolveActivity, you will get a warning like this:

Consider adding a declaration to your manifest when calling this method; see https://g.co/dev/packagevisibility for details

Check the document under PackageManager, you will see this note:

Note: If your app targets Android 11 (API level 30) or higher, the methods in this class each return a filtered list of apps. Learn more about how to manage package visibility.

So what does that mean?

In android 11, Google added package visibility policy. Apps now have tighter control over viewing other apps. Your application will not be able to view or access applications outside of your application.

What do you need to do?

All you need to do is add below line of code to AndroidManifest.xml:

<manifest>
    <queries>
        <!-- Specific intents you query for -->
        <intent>
            <action android:name="android.intent.action.DIAL" />
        </intent>
    </queries>
</manifest>

More information:

  1. Package visibility in Android 11
  2. Package visibility filtering on Android
Thanks answered 5/8, 2021 at 2:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.