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


posted @ 2013-09-11 19:41  一个学渣  阅读(387)  评论(0编辑  收藏  举报

 

CSDNGitHub 联系邮箱:aikongmeng@gmail.com


Copyright 2008-2019 Government All Rights Reserved