2014-01-27 5 views
0

사례 1 : 아래 코드를 확인하십시오. Class 및 Method 객체를 가져올 수 있으며 제대로 작동합니다. 액세스하려고하는 메서드는 android.view.View :: dispatchPointerEvent입니다.Android Reflection - Android Jars

사례 2 : 클래스/메소드를 com.android.server.pm.PackageManagerService :: grantPermissionsLPw로 바꿀 때 NoMethodFoundException이 발생합니다. 클래스 액세스 할 수있었습니다.

사례 3 : 클래스/메소드를 android.hardware.input.InputManager :: injectInputEvent로 바꿀 때 NoMethodFoundException이 발생합니다. 클래스 액세스 할 수있었습니다.

질문은 : 왜 안드로이드 클래스/메소드 중 일부는 리플렉션을 통해 액세스 할 수 있으며 다른 방법은 아닌지?

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(); 
    } 

답변

1

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(); 
    } 
0

(미안하지만 난 의견을 게시, 그래서 응답 게시 할 수 없습니다) 는 당신이 바로 그 방법 매개 변수를 넣어 했는가를? 어디 있니 MotionEvent.class.

+0

예, 적절한 매개 변수를 대체 않았다 시도하고 또한 사실 SetAccessible을 통해 개인 방법 중 처리했다 .. – Jailbroken

+0

당신은 응용 프로그램 또는 에뮬레이터 내에서 실행하거나에서 호출 IDE 프로젝트? 당신이 에뮬레이터에 없다면 나는 당신이 스텁을 호출하고 있다고 생각합니다. –

+0

기기에서 직접 실행 중입니다. – Jailbroken

0

android.hardware.input.InputManager :: injectInputEvent에는 응용 프로그램에서 사용할 수없는 시스템 권한 인 android.permission.INJECT_EVENTS 권한이 필요합니다. 이 작업을 수행해야하는 경우 기기를 루트하고 앱에 시스템 앱으로 서명해야합니다. 다른 SO 질문에 표시된대로

+0

그러나 나는 그것을 부르고 있지 않다. 반사를 통해 나는 단지 그것을 접근하려고 노력하고있다. 내가 부르면 실패 할 수 있습니다. 하지만 NoMethodFoundException은 매우 혼란 스럽습니다. – Jailbroken

+0

작동하지 않는 코드를 게시해야합니다. –

+0

케이스 2 또는 3의 클래스 및 메소드 이름을 바꾼다면 작동하지 않습니다. 코드는 동일하게 유지됩니다. 클래스 및 메서드 이름 만 변경되고 NoMethodFoundException이 발생합니다. 동일한 코드를 반복하여 이해하기가 복잡해지기를 원하지 않습니다. – Jailbroken