我想使用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" />
是否可以授予此权限,或者根本不可能?
答案 0 :(得分:8)
从平台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
,这意味着只有使用平台签名签名的应用程序才能被授予对此权限的访问权限。您无法从使用SDK的自签名应用程序获取此权限。