1

Reflected メソッドを使用してウィジェットから明るさを変更しようとしていますが、このコードは 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あなたのアプリに許可がありません。このパーミッションを取得するには、ルート uid を使用する必要があります。android:sharedUserId="android.uid.system"タグ内に追加<manifest>し、電話の OEM キー (コンストラクターによって使用される秘密キー、または開発プラットフォームの秘密キー) を使用してアプリケーションに署名します。

よろしく

于 2012-04-13T09:01:18.133 に答える