我想使用 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" />
是否可以授予此权限,还是根本不可能?
最佳答案
来自平台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 的自签名应用程序获得此权限。
关于java - 使用 StatusBarManagerService 时出错 - android.permission.STATUS_BAR_SERVICE 上的 java.lang.SecurityException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7821584/