[英]Android Reflection - Android Jars
情況1 :看下面的代碼。 我能夠獲得Class和Method對象,並且運行良好。 我嘗試訪問的方法是android.view.View :: dispatchPointerEvent。
情況2 :當我用com.android.server.pm.PackageManagerService :: grantPermissionsLPw替換類/方法時,出現NoMethodFoundException。 雖然可以訪問類。
情況3 :當我用android.hardware.input.InputManager :: injectInputEvent替換類/方法時,我得到了NoMethodFoundException。 雖然可以訪問類。
問題是 :為什么某些android類/方法可以通過反射訪問而另一些則不能?
Class _class = null;
try {
_class = Class.forName("android.view.View");
Log.i("Test", "Class found");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
Method method = null;
try {
Log.i("Test", "Pre-Method found");
method = _class.getDeclaredMethod("dispatchPointerEvent",
MotionEvent.class);
Log.i("Test", "Method found");
} catch (Exception e) {
Log.i("Test","I failed."+e.getMessage()+e.toString());
//e.printStackTrace();
}
嘗試這個
Class _class = null;
try {
_class = Class.forName("com.android.server.pm.PackageManagerService");
Log.i("Test", "Class found");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
Method method = null;
try {
Log.i("Test", "Pre-Method found");
Class _class2 = Class.forName("android.content.pm.PackageParser$Package");
method = _class.getDeclaredMethod("grantPermissionsLPw",
_class2, boolean.class);
Log.i("Test", "Method found");
} catch (Exception e) {
Log.i("Test","I failed."+e.getMessage()+e.toString());
e.printStackTrace();
}
(對不起,但是我不能發表評論,所以必須發表評論)您是否放置了正確的方法參數? 您在哪里擁有MotionEvent.class 。
android.hardware.input.InputManager :: injectInputEvent需要android.permission.INJECT_EVENTS權限,這是應用程序無法使用的系統權限。 如果您需要執行此操作,則必須啟動設備並為應用程序簽名為系統應用程序。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.