【问题标题】:Why do not work in android setBacklightBrightness(int)为什么在 android setBacklightBrightness(int) 中不起作用
【发布时间】:2011-08-03 13:45:28
【问题描述】:

我正在尝试通过我的小部件更改亮度 用反射的方法但是 这段代码在 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!!!!

....

【问题讨论】:

    标签: android brightness


    【解决方案1】:

    非常感谢您的代码,它运行良好!

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

    问候

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-01-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-10-26
      • 1970-01-01
      相关资源
      最近更新 更多