0

I'm currently trying to change the default dialer package of the machine to my application. When running the code that is supposed to change it, however, it results in the default dialer package retaining a value of NULL. This is loosely following the guide here, but some of it is deprecated so I've had to make some adjustments.

EDIT: has REQUEST_CODE_DEFAULT_DIALER been changed somehow? I can't find it referenced anywhere other than outdated tutorials on how to do this, so I'm not sure what it would even have been changed to. I'm getting a "cannot resolve symbol" error whenever I try to use it.

I've included the following under the relevant activity in my AndroidManifest.xml:

<intent-filter>
    <action android:name="android.intent.action.DIAL" />
    <category android:name="android.intent.category.DEFAULT" />
</intent-filter>
<intent-filter>
    <action android:name="android.intent.action.DIAL" />
    <category android:name="android.intent.category.DEFAULT" />
    <data android:scheme = "tel"/>
</intent-filter>

The following code executes the package change and then prints its value to logcat so I can see what happens:

Log.i("Before", "Before default dialer change");
startActivity(new Intent(TelecomManager.ACTION_CHANGE_DEFAULT_DIALER).putExtra(TelecomManager.EXTRA_CHANGE_DEFAULT_DIALER_PACKAGE_NAME,getPackageName()));
TelecomManager telecomManager = ((TelecomManager) getSystemService(Context.TELECOM_SERVICE));
if(telecomManager.getDefaultDialerPackage() == null) {
    Log.i("Default dialer package:", "NULL");
} else {
    Log.i("Default Dialer Package:", telecomManager.getDefaultDialerPackage());

}

I'm getting "Default dialer package:: NULL" in logcat every time I try this.

Jacob Z
  • 17
  • 2
  • I think there is a full working code example you can refer and create your own from here https://github.com/SimpleMobileTools/Simple-Dialer – Dev007 Nov 15 '22 at 18:24
  • Appreciated @Gabe answer. Meanwhile in your code you have to add that check once you got any result code. it should be call with startResultForActivity or ResultContracts. – Dev007 Nov 15 '22 at 18:30

1 Answers1

1

Intents are asynchronous. If you start an activity to do something, you have to wait for that intent to finish before using the results of it. You're instead immediately querying for the default dialer package. That will always fail, because it wouldn't even have shown the picker on screen yet.

Gabe Sechan
  • 90,003
  • 9
  • 87
  • 127
  • How do I go about waiting for the response? I'm very new to android so sorry if that's an obvious question. The tutorial has "startActivityForResponse", but that function is depreciated. – Jacob Z Nov 15 '22 at 19:27
  • startActivityForResult is the older way of doing it. The newer way is registerForActivityResult – Gabe Sechan Nov 15 '22 at 19:37
  • The function is basically the same right? I'm getting a "cannot resolve symbol REQUEST_CODE_SET_DEFAULT_DIALER" when I use registerForActivityResult((new Intent(TelecomManager.ACTION_CHANGE_DEFAULT_DIALER).putExtra(TelecomManager.EXTRA_CHANGE_DEFAULT_DIALER_PACKAGE_NAME,getPackageName())),REQUEST_CODE_SET_DEFAULT_DIALER); Has that request code also been deprecated? – Jacob Z Nov 15 '22 at 19:55