Skip to content Skip to sidebar Skip to footer

How To Use Permission Inside Android Framework?

I register an BroadcastReceiver to receive SMS in a SystemService, but we don't have the permisson 'android.permission.RECEIVE_SMS'. So how to use permission inside android framewo

Solution 1:

You can add this permission in AndroidManifest.xml placed at frameworks/base/core/res

<uses-permissionandroid:name="android.permission.RECEIVE_SMS" />

Solution 2:

We had the same problem and tried to find the best way to access a permission protected system service from another system service.

In this case the solution usually is already in the Android code, for example have a look in frameworks/base/services/java/com/android/server/NotificationManagerService.java which is using the VibratorService.java from the same package.

You can see that the calling structure is something like this:

long identity = Binder.clearCallingIdentity();
try {
    mVibrator.cancel();
}
finally {
    Binder.restoreCallingIdentity(identity);
}

According to the docs clearCallingIdentity() is exactly what you need:

Blockquote Reset the identity of the incoming IPC on the current thread. This can be useful if, while handling an incoming call, you will be calling on interfaces of other objects that may be local to your process and need to do permission checks on the calls coming into them (so they will check the permission of your own local process, and not whatever process originally called you).

Solution 3:

You can try to do the following:

privatestaticfinalStringANDROID_PERMISSION_RECEIVE_SMS="android.permission.RECEIVE_SMS";

publicvoidsetUpPermissions() {
    PermissionInfopermissionInfo=newPermissionInfo();
    permissionInfo.name = ANDROID_PERMISSION_RECEIVE_SMS;
    getContext().getPackageManager().addPermission(permissionInfo);
}

Solution 4:

Declare the permissions you'll need in your application's AndroidManifest.xml.

That permission is actually in the exact example from the docs (along with other helpful things if you search for it):

<manifestxmlns:android="http://schemas.android.com/apk/res/android"package="com.android.app.myapp" ><uses-permissionandroid:name="android.permission.RECEIVE_SMS" /></manifest>

Solution 5:

This is perhaps the wrong way to go about whatever you are trying to do.

Rather than cutting a new "doorway" from the outside world into a core part of the system, it might be a better idea to add a new interface to the system which SDK apps can utilize, and then write an SDK app which receives SMS and can use this new interface to accomplish something.

Post a Comment for "How To Use Permission Inside Android Framework?"