如何修复java.lang.SecurityException:This PhoneAccountHandle is not enabled for this user异常
java.lang.SecurityException: This PhoneAccountHandle is not enabled for this user! Hey, I’ve run into this exact issue before! That error pops up because even though you’ve registered your PhoneAccount, the Android system doesn’t automatically enable it—especially on Android 10 and above. Plus, there’s a small mistake in your registration code that’s making things worse. Let’s break down the fixes step by step:
1. Fix the Capability Overwrite Bug
In your register() method, you’re calling setCapabilities() twice, which overwrites the first capability instead of combining them. You need to use the bitwise or operator to include both capabilities:
fun register(){ val phoneAccount = PhoneAccount.builder(phoneAccountHandle,"com.darkhorse.videocalltest") // Combine capabilities with bitwise OR instead of overwriting .setCapabilities(PhoneAccount.CAPABILITY_CALL_PROVIDER or PhoneAccount.CAPABILITY_CONNECTION_MANAGER) .build() telecomManager.registerPhoneAccount(phoneAccount) }
Without this, your account only gets the CAPABILITY_CONNECTION_MANAGER flag, which might prevent the system from recognizing it as a valid call provider.
2. Check if Your PhoneAccount is Enabled
After registration, you need to verify if the account is actually enabled. Add this helper method to your CallManager class:
fun isPhoneAccountEnabled(): Boolean { val registeredAccount = telecomManager.getPhoneAccount(phoneAccountHandle) return registeredAccount != null && registeredAccount.isEnabled }
3. Guide the User to Enable the Account
Android 10+ blocks apps from enabling PhoneAccounts automatically for security reasons. You have to redirect the user to the system settings to turn it on manually. Add this method:
fun requestEnablePhoneAccount() { val intent = Intent(TelecomManager.ACTION_CHANGE_PHONE_ACCOUNTS) intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK) context.startActivity(intent) }
Use this before calling incomingCall() to ensure the account is active:
// Example usage in an Activity/Fragment val callManager = CallManager(this) callManager.register() if (!callManager.isPhoneAccountEnabled()) { // Prompt user to enable your app's phone account callManager.requestEnablePhoneAccount() } else { // Now you can safely trigger incoming calls callManager.incomingCall("+1234567890") }
4. Verify Manifest Setup
Double-check that your ConnService is registered correctly in AndroidManifest.xml with the required permission and intent filter:
<service android:name=".ConnService" android:permission="android.permission.BIND_TELECOM_CONNECTION_SERVICE"> <intent-filter> <action android:name="android.telecom.ConnectionService" /> </intent-filter> </service>
Also, make sure you’ve requested the READ_PHONE_STATE permission in your manifest and requested it at runtime (for Android 6.0+). For Android 13+, you’ll also need the POST_NOTIFICATIONS permission to show incoming call notifications.
5. Wait for User to Enable the Account
Keep in mind that after sending the user to settings, you’ll need to wait for them to enable the account before calling incomingCall(). You can listen for the activity result (using registerForActivityResult in modern Android) to detect when they return from settings, then re-check the account status.
That should resolve the SecurityException. The key issues were overwriting the account capabilities and not ensuring the account was enabled by the user.
内容的提问来源于stack exchange,提问作者Lakshay Dulani

