Java – Error using StatusBarManagerService – java.lang.SecurityException on android.permission.STATUS_BAR_SERVICE

Error using StatusBarManagerService – java.lang.SecurityException on android.permission.STATUS_BAR_SERVICE… here is a solution to the problem.

Error using StatusBarManagerService – java.lang.SecurityException on android.permission.STATUS_BAR_SERVICE

I wanted to use StatusBarManagerService, but it wasn’t available on the SDK, so I tried “cheating”, using reflection to access it.

I was able to access it, but now I get an error when accessing one of the methods:

java.lang.SecurityException: StatusBarManagerService: Neither user 10139 nor current process has android.permission.STATUS_BAR_SERVICE.

Is it possible to grant this permission, or is it not possible at all?

Here is my code:

    try {
        Class serviceManagerClass = Class.forName("android.os.ServiceManager");
        Method getService = serviceManagerClass.getMethod("getService", String.class);
        IBinder retbinder = (IBinder) getService.invoke(serviceManagerClass, "statusbar");
        Class statusBarClass = Class.forName(retbinder.getInterfaceDescriptor());
        Object statusBarObject = statusBarClass.getClasses()[0].getMethod("asInterface", IBinder.class).invoke(null, new Object[] { retbinder });
        Method clearAll = statusBarClass.getMethod("onClearAllNotifications");
        clearAll.setAccessible(true);
        clearAll.invoke(statusBarObject);
    } catch (ClassNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (SecurityException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (NoSuchMethodException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IllegalArgumentException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IllegalAccessException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (InvocationTargetException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (RemoteException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

Here’s what I added to the AndroidManifest:

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

Is it possible to grant this permission, or is it not possible at all?

Solution

From the platform AndroidManifest .xml:

<!-- Allows an application to be the status bar.  Currently used only by SystemUI.apk
@hide -->
<permission android:name="android.permission.STATUS_BAR_SERVICE"
    android:label="@string/permlab_statusBarService"
    android:description="@string/permdesc_statusBarService"
    android:protectionLevel="signature" />

protectionLevel Note property, whose value is This signature means that only applications signed with platform signatures and part of the device ROM can be granted access to this permission. You cannot obtain this permission from a self-signed application that uses the SDK.

Related Problems and Solutions