1

我试图用反射方法改变我的小部件的亮度,但是这段代码在 setBacklightBrightness.invoke(power, new Object[]{Brightness}); 并写调用错误。请帮忙!

public static void LoadIPowerClass(Context context)
{   
    try{
    //Load classes and objects

    Object power;
    Context fContext = context;
    Class <?> ServiceManager = Class.forName("android.os.ServiceManager");
    Class <?> Stub = Class.forName("android.os.IPowerManager$Stub");


    Method getService = ServiceManager.getMethod("getService", new Class[] {String.class});
    //Method asInterface = GetStub.getMethod("asInterface", new Class[] {IBinder.class});//of this class?
    Method asInterface = Stub.getMethod("asInterface", new Class[] {IBinder.class});    //of this class?
    IBinder iBinder = (IBinder) getService.invoke(null, new Object[] {Context.POWER_SERVICE});//
    power = asInterface.invoke(null,iBinder);//or call constructor Stub?//

    Method setBacklightBrightness = power.getClass().getMethod("setBacklightBrightness", new Class[]{int.class}); 

    int Brightness = 5;

    setBacklightBrightness.invoke(power, new Object[]{Brightness});//HERE Failen

    Log.i(TAG, "Load internal IPower classes Ok");               
    }catch(InvocationTargetException e){                     //HERE catch!!!!

....
4

1 回答 1

1

非常感谢您的代码,它工作得很好!

关于您的例外,您可能缺少DEVICE_POWER应用程序中的权限。为了获得此权限,您必须使用 root uid。android:sharedUserId="android.uid.system"在标签内添加<manifest>并使用手机的 OEM 密钥(构造函数使用的私钥,或开发平台的私钥)对您的应用程序进行签名。

问候

于 2012-04-13T09:01:18.133 回答