내가 아는 한, 루트 권한 만 사용하여 명령 줄 명령을 실행할 수 있습니다. 당신은 당신의 코드에서 루트 액세스를 래핑 내가 만든이 제네릭 클래스를 사용할 수 있습니다 http://muzikant-android.blogspot.com/2011/02/how-to-get-root-access-and-execute.html
당신이해야 할 모든이 클래스를 확장하고 루트로 실행하려는 명령을 반환 getCommandsToExecute
메소드를 오버라이드 (override)합니다.
public abstract class ExecuteAsRootBase
{
public static boolean canRunRootCommands()
{
boolean retval = false;
Process suProcess;
try
{
suProcess = Runtime.getRuntime().exec("su");
DataOutputStream os = new DataOutputStream(suProcess.getOutputStream());
DataInputStream osRes = new DataInputStream(suProcess.getInputStream());
if (null != os && null != osRes)
{
// Getting the id of the current user to check if this is root
os.writeBytes("id\n");
os.flush();
String currUid = osRes.readLine();
boolean exitSu = false;
if (null == currUid)
{
retval = false;
exitSu = false;
Log.d("ROOT", "Can't get root access or denied by user");
}
else if (true == currUid.contains("uid=0"))
{
retval = true;
exitSu = true;
Log.d("ROOT", "Root access granted");
}
else
{
retval = false;
exitSu = true;
Log.d("ROOT", "Root access rejected: " + currUid);
}
if (exitSu)
{
os.writeBytes("exit\n");
os.flush();
}
}
}
catch (Exception e)
{
// Can't get root !
// Probably broken pipe exception on trying to write to output stream (os) after su failed, meaning that the device is not rooted
retval = false;
Log.d("ROOT", "Root access rejected [" + e.getClass().getName() + "] : " + e.getMessage());
}
return retval;
}
public final boolean execute()
{
boolean retval = false;
try
{
ArrayList<String> commands = getCommandsToExecute();
if (null != commands && commands.size() > 0)
{
Process suProcess = Runtime.getRuntime().exec("su");
DataOutputStream os = new DataOutputStream(suProcess.getOutputStream());
// Execute commands that require root access
for (String currCommand : commands)
{
os.writeBytes(currCommand + "\n");
os.flush();
}
os.writeBytes("exit\n");
os.flush();
try
{
int suProcessRetval = suProcess.waitFor();
if (255 != suProcessRetval)
{
// Root access granted
retval = true;
}
else
{
// Root access denied
retval = false;
}
}
catch (Exception ex)
{
Log.e("ROOT", "Error executing root action", ex);
}
}
}
catch (IOException ex)
{
Log.w("ROOT", "Can't get root access", ex);
}
catch (SecurityException ex)
{
Log.w("ROOT", "Can't get root access", ex);
}
catch (Exception ex)
{
Log.w("ROOT", "Error executing internal operation", ex);
}
return retval;
}
protected abstract ArrayList<String> getCommandsToExecute();
}
실제로 기존 프로세스의 권한을 높이는 방법은 없으며 루트로 실행중인 새 프로세스 만 시작한다는 의미입니다. su 명령에 실행 명령이 전달 된 예제는 http://stackoverflow.com/questions/4846241/more-than-one-superuser-command-android/4846312 –
을 참조하십시오. @Chris 새로운 활동/태스크/안드로이드 자바 애플 리케이션/등? –
안드로이드에 엄청난 변화가 필요합니다. 일반적으로 안드로이드 자바 실행을위한 프로세스를 생성하지 않는다. 안드로이드에게 'zygote'에게 그 애플리케이션의 사용자 아이디로 포크 오프를 요청하라. 응용 프로그램 대신 핵심 작업을 수행하는 원시 실행 파일 또는 쉘 스크립트로 루트 작업을 유지하는 것이 더 간단합니다. 또는 시스템을 권한있는 시스템 서비스로 추가하십시오. 가장 좋은 방법은 유닉스 그룹을 만들어 특정 기능 (장치 파일에 대한 액세스 권한)에 액세스하고 해당 그룹에서 서비스를 실행하거나 안드로이드 권한으로 페어링하는 것입니다. –