2013-09-26 1 views

답변

1

이 작업을 수행하려면 많은 단계가 필요합니다.

처음에는 (물론) 장치가 루팅되어야합니다. 여러 가지 방법으로이를 확인할 수 있습니다.

다음 코드는 "su"명령이 명령을 찾을 수 없음 오류 (이진 파일이 존재 함)를 반환하고 요청한 후에 사용 권한을 부여하기 위해 수퍼 유저 응용 프로그램이 설치되어 있는지 확인합니다.

private boolean isDeviceRooted() { 

      // check for SU command in shell 
      if ((new ExecShell().executeCommand(ExecShell.SHELL_COMMAND.su_check) != null) && (appInstalled("eu.chainfire.supersu.nonag") || appInstalled("eu.chainfire.supersu") || appInstalled("com.noshufou.android.su") || appInstalled("com.koushikdutta.superuser"))) { 
       Log.i(TAG, "Device Rooted"); 
       return true; 
      } 

      // check for SU application installed 
      if (appInstalled("eu.chainfire.supersu.nonag") || appInstalled("eu.chainfire.supersu") || appInstalled("com.noshufou.android.su") || appInstalled("com.koushikdutta.superuser")) { 
       Log.i(TAG, "Device Rooted"); 
       return true; 
      } 

      Log.i(TAG, "Device Not Rooted"); 
      return false; 
     } 


     private boolean appInstalled(String uri) { 
      PackageManager pm = getPackageManager(); 
      boolean app_installed = false; 
      try { 
       pm.getPackageInfo(uri, PackageManager.GET_ACTIVITIES); 
       app_installed = true; 
      } catch (PackageManager.NameNotFoundException e) { 
       app_installed = false; 
      } 
      return app_installed; 
     } 

이 코드가 false를 반환하면 플래그 또는 표시 및 오류를 설정하고, 그렇지 않으면 계속할 수 있습니다.

그런 다음 장치가 루팅됨을 알게되면 필요한 루트 명령을 실행하여 필요한 작업을 수행하려고합니다.

다음 코드는 명령의 String []을 입력 받아 루트로 순차적으로 실행합니다.

public boolean RunAsRoot(String[] cmds) { 
    Process p; 
    try { 
     p = Runtime.getRuntime().exec("su"); 
     DataOutputStream os = new DataOutputStream(p.getOutputStream()); 
     try { 
      for (String tmpCmd : cmds) { 
       os.writeBytes(tmpCmd + "\n"); 
      } 
      os.writeBytes("exit\n"); 
      os.flush(); 
      return true; 
     } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
      return false; 
     } 
    } catch (IOException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
     return false; 
    } 
} 

경우에 따라/system을 rw로 마운트하려고합니다. 이 원하는 명령을 찾을 수 있도록 웹에 정보를 많이입니다하지만 당신은 다음 mv 또는 cp 중 하나를 사용하여 원하는 파일을 이동할 mount -o remount rw /system mount -o remount rw /system

처럼 보일 것입니다.

루트 명령의 사용 예는이 루트 기능이다 "하드"비트를 포함

String[] cmds = {"mount -o remount rw /system mount -o remount rw /system", "cp /sdcard/myfile /system/framework/myfile"}; 
if(!RunAsRoot(cmds)){ 
    //Commands failed to run, show an error/retry 
} 

될 것이다.

버튼에 대한 간단한 자습서는 here에서 찾을 수 있습니다.

프로그램의 흐름이

onCreate(){ 
    checkIsRooted(); 
    Button x = (Button) findViewById(R.id.x); 
    x.setOnClickListener(onClickListener()); 
} 

onClickListener(){ 
    onClick(){ 
     String[] cmds = {...}; 
     if(!runAsRoot(cmds)) 
      AlertDialog.Builder.makeText(...).show(); 
    } 
} 

참고가 될 수있다, 이것은 의사 코드, 당신은 복사 및 IT가 작동하도록이 코드를 붙여 넣을 수 없습니다, 제대로 너 자신 IT를 할 필요가!