How to use permission inside android framework?

那年仲夏 提交于 2021-01-27 20:51:48

问题


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 framework? In the SystemService, it's context is get from ActivityManagerService.main().

 public void setWindowManager(WindowManagerService wm) {
    mWindowManager = wm;
}

public static final Context main(int factoryTest) {
    AThread thr = new AThread();
    thr.start();

    synchronized (thr) {
        while (thr.mService == null) {
            try {
                thr.wait();
            } catch (InterruptedException e) {
            }
        }
    }

    ActivityManagerService m = thr.mService;
    mSelf = m;
    ActivityThread at = ActivityThread.systemMain();
    mSystemThread = at;
    Context context = at.getSystemContext();
    m.mContext = context;
    m.mFactoryTest = factoryTest;
    PowerManager pm =
        (PowerManager)context.getSystemService(Context.POWER_SERVICE);
    m.mGoingToSleep = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "ActivityManager-Sleep");
    m.mLaunchingActivity = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "ActivityManager-Launch");
    m.mLaunchingActivity.setReferenceCounted(false);

    m.mBatteryStatsService.publish(context);
    m.mUsageStatsService.publish(context);

    synchronized (thr) {
        thr.mReady = true;
        thr.notifyAll();
    }

    m.startRunning(null, null, null, null);

    return context;
}

回答1:


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

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



回答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).




回答3:


You can try to do the following:

private static final String ANDROID_PERMISSION_RECEIVE_SMS = "android.permission.RECEIVE_SMS";

public void setUpPermissions() {
    PermissionInfo permissionInfo = new PermissionInfo();
    permissionInfo.name = ANDROID_PERMISSION_RECEIVE_SMS;
    getContext().getPackageManager().addPermission(permissionInfo);
}



回答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):

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.android.app.myapp" >

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

</manifest>



回答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.




回答6:


You don't need to declare permission if your system service is implemented correctly.



来源:https://stackoverflow.com/questions/4286921/how-to-use-permission-inside-android-framework

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!