在模擬按鍵處理時用到了:
java代碼:
IBinder wmbinder = ServiceManager.getService( "window" );
final IWindowManager wm = IWindowManager.Stub.asInterface( wmbinder );
需要引入的類是:
java代碼:
import Android.os.ServiceManager;
import android.view.IWindowManager;
方法如下: 1. 首先查找源碼,找到了IWindowManager.aidl文件,將源碼按照相同的路徑在自己的工程中創建;
2. 添加代碼替換原來的實現方法
java代碼:
try{
Object object = new Object();
Method getService = Class.forName("android.os.ServiceManager").getMethod("getService", String.class);
Object obj = getService.invoke(object, new Object[]{new String("window")});
//System.out.println(obj.toString());
windowMger = IWindowManager.Stub.asInterface((IBinder)obj);
}catch(ClassNotFoundException ex){
//ignored
}catch(NoSuchMethodException ex){
//ignored
}catch(IllegalAccessException ex){
//ignored
}catch(InvocationTargetException ex){
//ignored
}
反射機制的應用1:
java代碼:
import android.os.ServiceManager;
ITelephony.Stub.asInterface(ServiceManager.getService("phone")).answerRingingCall();