Android 判定手机是否root

Android获取手机root的状态

package com.app.demo;

import java.io.File;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class AndroiddemoActivity extends Activity {
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);  
	}
	public void onclick(View v) {
		Button button = (Button)v;
		if (isRootSystem()) {
			button.setText("root");
		} else {
			button.setText("not");
		}  
	} 

	private final static int kSystemRootStateUnknow=-1;
	private final static int kSystemRootStateDisable=0;
	private final static int kSystemRootStateEnable=1;
	private static int systemRootState=kSystemRootStateUnknow;

	public static boolean isRootSystem()
	{
		if(systemRootState==kSystemRootStateEnable)
		{
			return true;
		}
		else if(systemRootState==kSystemRootStateDisable)
		{

			return false;
		}
		File f=null;
		final String kSuSearchPaths[]={"/system/bin/","/system/xbin/","/system/sbin/","/sbin/","/vendor/bin/"};
		try{
			for(int i=0;i<kSuSearchPaths.length;i++)
			{
				f=new File(kSuSearchPaths[i]+"su");
				if(f!=null&&f.exists())
				{
					systemRootState=kSystemRootStateEnable;
					return true;
				}
			}
		}catch(Exception e)
		{
		}
		systemRootState=kSystemRootStateDisable;
		return false;
	}
}


原文地址:https://www.cnblogs.com/aikongmeng/p/3697374.html