【问题标题】:Error using StatusBarManagerService - java.lang.SecurityException on android.permission.STATUS_BAR_SERVICE使用 StatusBarManagerService 时出错 - android.permission.STATUS_BAR_SERVICE 上的 java.lang.SecurityException
【发布时间】:2011-12-10 22:01:26
【问题描述】:

我想使用StatusBarManagerService,但是SDK上没有,所以我尝试“作弊”,使用反射来访问它。

我能够访问它,但现在在访问其中一种方法时它给了我一个错误:

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

是否有可能获得此权限,或者根本不可能?

这是我的代码:

    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();
    }

这是我在 AndroidManifest 中添加的内容:

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

是否有可能获得此权限,或者根本不可能?

【问题讨论】:

    标签: java android reflection permissions


    【解决方案1】:

    来自平台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 属性,其值为signature,这意味着只有使用平台签名签名的应用程序以及设备ROM 的一部分才能被授予访问此权限的权限。您无法从使用 SDK 的自签名应用程序中获得此权限。

    【讨论】:

    • 感谢您的回答。 :)
    • 没问题。实际上有趣的是,据我所知,您真正想要的权限是 STATUS_BAR,而不是 STATUS_BAR_SERVICE 或 EXPAND_STATUS_BAR。 STATUS_BAR 是一个系统权限,您的应用必须持有并位于 /system/app 中才能被授予,因此您仍然不走运。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-04
    • 1970-01-01
    相关资源
    最近更新 更多