How to grant MODIFY_PHONE_STATE permission for apps ran on Gingerbread

Bao Le picture Bao Le · Jan 17, 2011 · Viewed 65.2k times · Source

I write an application that attempts to modify phone call state. It works well on Android 2.2 or less, but throw an exception on Android 2.3 because of the lack of permission on android.permission.MODIFY_PHONE_STATE permission (I declared this permission on AndroidManifest.xml). Any idea? Below is the exception log:

01-15 09:14:23.210: ERROR/AndroidRuntime(404): FATAL EXCEPTION: main
01-15 09:14:23.210: ERROR/AndroidRuntime(404): java.lang.RuntimeException: Unable to start receiver test.PhoneReceiver: java.lang.SecurityException: Neither user 10031 nor current process has android.permission.MODIFY_PHONE_STATE.
01-15 09:14:23.210: ERROR/AndroidRuntime(404):     at android.app.ActivityThread.handleReceiver(ActivityThread.java:1780)
01-15 09:14:23.210: ERROR/AndroidRuntime(404):     at android.app.ActivityThread.access$2400(ActivityThread.java:117)
01-15 09:14:23.210: ERROR/AndroidRuntime(404):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:978)
01-15 09:14:23.210: ERROR/AndroidRuntime(404):     at android.os.Handler.dispatchMessage(Handler.java:99)
01-15 09:14:23.210: ERROR/AndroidRuntime(404):     at android.os.Looper.loop(Looper.java:123)
01-15 09:14:23.210: ERROR/AndroidRuntime(404):     at android.app.ActivityThread.main(ActivityThread.java:3647)
01-15 09:14:23.210: ERROR/AndroidRuntime(404):     at java.lang.reflect.Method.invokeNative(Native Method)
01-15 09:14:23.210: ERROR/AndroidRuntime(404):     at java.lang.reflect.Method.invoke(Method.java:507)
01-15 09:14:23.210: ERROR/AndroidRuntime(404):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
01-15 09:14:23.210: ERROR/AndroidRuntime(404):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
01-15 09:14:23.210: ERROR/AndroidRuntime(404):     at dalvik.system.NativeStart.main(Native Method)

Answer

Skylar Sutton picture Skylar Sutton · Feb 23, 2011

The problem you're having was introduced in Android 2.3 (Gingerbread). Any code you have that requires MODIFY_PHONE_STATE will work all the way up to (and including) Android 2.2, but will break for Android 2.3+.

A change was checked in by David Brown that limits the use of the MODIFY_PHONE_STATE permission to system apps. System apps are either

  1. Pre-installed into a system folder on the ROM
  2. Compiled by a manufacturer using their security certificate

I suspect you're trying to use a hidden API like ITelephony. I was - and I got burned by this change. The Android team's justification is that it was a hidden API that you shouldn't have been using it in the first place.

That said, there was an enhancement request opened to create a proper public Telephony API, but Google killed the ticket. It appears their stance is that they do not intend to reverse direction and these APIs are not for public consumption.