• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

How do I list my phone for the phone caller default app?

thecowmilk

Lurker
Dec 22, 2019
1
0
How do I make one app appear as the default app choice in Android? I'd like this app handles the calls, and accept and decline a call. So far I have tried to add these lines at manifest but no luck at all. My App doesn't appear at Settings > Apps > Default Apps > Phone app. I have this android_manifest.xml file:
XML:
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.CALL_PHONE" />
<uses-permission android:name="android.permission.ANSWER_PHONE_CALLS" />
<uses-permission android:name="android.permission.ACTION_CHANGE_DEFAULT_DIALER" />
<uses-feature android:name="android.software.PHONE_APP" android:required="true" />

<!-- I made this new activity to handle calls-->
<activity
android:name=".IncomingCallActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.ANSWER" />
<action android:name="android.intent.action.CALL" />
<category android:name="android.intent.category.DEFAULT" />
<data android:scheme="tel" />
</intent-filter>
</activity>


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

<action android:name="android.intent.action.CALL_BUTTON" />
</intent-filter>



Kotlin code attempting:
Code:
class MainActivity : ComponentActivity() {

private val permissions = arrayOf(
android.Manifest.permission.READ_PHONE_STATE,
android.Manifest.permission.CALL_PHONE
)
private val requestCode = 123


@OptIn(ExperimentalMaterial3Api::class)
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)



if (ContextCompat.checkSelfPermission(
this,
android.Manifest.permission.READ_PHONE_STATE
) == PackageManager.PERMISSION_GRANTED
) {
val telecomManager = getSystemService(TelecomManager::class.java)
val defaultDialerPackage = telecomManager?.defaultDialerPackage

// Check if the app is the default dialer
if (packageName != defaultDialerPackage) {
val intent = Intent(TelecomManager.ACTION_CHANGE_DEFAULT_DIALER)
intent.putExtra(
TelecomManager.EXTRA_CHANGE_DEFAULT_DIALER_PACKAGE_NAME,
packageName
)
startActivity(intent)
}
}




if (!arePermissionsGranted()) {
requestPermissions(permissions, requestCode)
} else {
// Permissions are already granted, register as the default dialer
registerAsDefaultDialer()
}
}

private fun arePermissionsGranted(): Boolean {
val permissionsToRequest = mutableListOf<String>()
for (permission in permissions) {
if (ContextCompat.checkSelfPermission(this, permission) != PackageManager.PERMISSION_GRANTED) {
permissionsToRequest.add(permission)
}
}

if (permissionsToRequest.isNotEmpty()) {
// Request missing permissions
ActivityCompat.requestPermissions(this, permissionsToRequest.toTypedArray(), requestCode)
return false
}

// If all permissions are granted, register as the default dialer
registerAsDefaultDialer()

return permissionsToRequest.isEmpty()
}

private fun registerAsDefaultDialer() {
val telecomManager = getSystemService(Context.TELECOM_SERVICE) as TelecomManager
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
if (!packageName.equals(telecomManager.defaultDialerPackage)) {
val intent = Intent(TelecomManager.ACTION_CHANGE_DEFAULT_DIALER)
intent.putExtra(
TelecomManager.EXTRA_CHANGE_DEFAULT_DIALER_PACKAGE_NAME,
packageName
)
startActivity(intent)
}
} else {
// Prior to Android Q, there is no need to register as the default dialer
}
}

override fun onRequestPermissionsResult(
requestCode: Int,
permissions: Array<out String>,
grantResults: IntArray
) {
when (requestCode) {
this.requestCode -> {
if (grantResults.isNotEmpty() && grantResults.all { it == PackageManager.PERMISSION_GRANTED }) {
// Permissions granted, now register as the default dialer
registerAsDefaultDialer()
}
}
else -> super.onRequestPermissionsResult(requestCode, permissions, grantResults)
}
}

}


Is still not able to be listed in there. Please note that I'm currently testing this in Android Studio Emulator so if that will impact the result please let me know.
 

BEST TECH IN 2023

We've been tracking upcoming products and ranking the best tech since 2007. Thanks for trusting our opinion: we get rewarded through affiliate links that earn us a commission and we invite you to learn more about us.

Smartphones