Android中通过代码检测系统是否有root权限


RootManager.java

 

package lizm.root;

import java.io.DataOutputStream;

import android.app.Activity;
import android.util.Log;

public class RootManager{
	 public static boolean RootCommand(String command)
	    {
	        Process process = null;
	        DataOutputStream os = null;
	        try
	        {
	            process = Runtime.getRuntime().exec("su");
	            os = new DataOutputStream(process.getOutputStream());
	            os.writeBytes(command + "\n");
	            os.writeBytes("exit\n");
	            os.flush();
	            process.waitFor();
	        } catch (Exception e)
	        {
	            Log.d("*** DEBUG ***", "ROOT REE" + e.getMessage());
	            return false;
	        } finally
	        {
	            try
	            {
	                if (os != null)
	                {
	                    os.close();
	                }
	                process.destroy();
	            } catch (Exception e)
	            {
	            }
	        }
	        Log.d("*** DEBUG ***", "Root SUC ");
	        return true;
	    }

}


 

 

MainActivity.java

package lizm.root;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.Toast;

public class MainActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        String apkRoot="chmod 777 "+getPackageCodePath();
        boolean root = RootManager.RootCommand(apkRoot);
        Toast.makeText(this, "root: "+root, Toast.LENGTH_SHORT).show();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
}


 

 

 

 

你可能感兴趣的:(android)