import java.io.DataOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.RandomAccessFile; import java.util.ArrayList; import android.app.Activity; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.net.wifi.WifiInfo; import android.net.wifi.WifiManager; import android.os.Bundle; import android.os.Handler; import android.telephony.TelephonyManager; import android.util.Log; import android.view.View; import android.view.Window; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.Button; import android.widget.ListView; import android.widget.TextView; import android.widget.Toast; /** * 获取root权限,读取并修改系统文件 * */ public class RootTestActivity extends Activity { private final static int CHANGE_OK = 1; private final static int WIFI_OK = 2; private ListView contentList; private FileAdapter adapter; private ArrayList<String> datas = new ArrayList<String>(); private TextView resultTxt; private WifiManager wifi; private WifiReceiver wifiReceiver = null; private Button send; private Button change; /** * 是否是第一次执行 */ private boolean isFirst = true; private Handler handler = new Handler(){ public void handleMessage(android.os.Message msg) { switch (msg.what) { case CHANGE_OK: // 改完后重启wifi changeWifiState(); break; case WIFI_OK: // 重启wifi结束 resultTxt.setText("修改完成,mac为: " + getLocalMacAddress()); changeBtnState(); break; default: break; } }; }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.activity_su); wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE); wifiReceiver = new WifiReceiver(); IntentFilter filter=new IntentFilter(); filter.addAction(WifiManager.WIFI_STATE_CHANGED_ACTION); registerReceiver(wifiReceiver, filter); send = (Button) findViewById(R.id.send); change = (Button) findViewById(R.id.change); resultTxt = (TextView) findViewById(R.id.content_edit); contentList = (ListView) findViewById(R.id.content); searchFile(""); adapter = new FileAdapter(this, datas); contentList.setAdapter(adapter); contentList.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { String filePath = datas.get(position); runRootCommand("chmod 755 " + filePath); searchFileByPath(filePath); adapter.notifyDataSetChanged(); contentList.setSelection(0); } }); resultTxt.setText(getIMEI()); } public String searchFile(String keyword) { datas.clear(); String result = ""; File parentFile = new File("/"); File[] files = parentFile.listFiles(); for (File file : files) { if (file.getName().indexOf(keyword) >= 0) { result += file.getPath() + "\n"; datas.add(file.getPath()); } } if (result.equals("")) { result = "找不到文件!!"; } return result; } public String searchFileByPath(String path) { String result = ""; File parentFile = new File(path + "/"); if (parentFile.isFile()) { runRootCommand("chmod 777 " + path + "/"); try { // 重写文件 // reWriteFile(path + "/"); writeSDFile(path, parentFile); // result = readSDFile(parentFile); // runRootCommand("chmod 660 " + path + "/"); isFirst = false; changeBtnState(); handler.sendEmptyMessageDelayed(CHANGE_OK, 500); resultTxt.setText("请稍等..."); } catch (IOException e) { e.printStackTrace(); resultTxt.setText("修改失败"); } return result; } File[] files = parentFile.listFiles(); if (files != null && files.length > 0) { datas.clear(); for (File file : files) { datas.add(file.getPath()); } } else { Toast.makeText(RootTestActivity.this, "找不到文件!!", Toast.LENGTH_SHORT).show(); } return result; } public static boolean runRootCommand(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 ***", "Unexpected error - Here is what I know: " + e.getMessage()); return false; } finally { try { if (os != null) { os.close(); } process.destroy(); } catch (Exception e) { // nothing } } return true; } public void onClick(View view) { switch (view.getId()) { case R.id.send: String filePath = datas.get(0); int index = filePath.lastIndexOf("/"); if (index > 1) { filePath = filePath.substring(0, index); index = filePath.lastIndexOf("/"); // if (index > 1) { filePath = filePath.substring(0, index); runRootCommand("chmod 755 " + filePath); searchFileByPath(filePath); adapter.notifyDataSetChanged(); // } else { // Toast.makeText(RootTestActivity.this, "已经是最上层", // Toast.LENGTH_SHORT).show(); // } } else { Toast.makeText(RootTestActivity.this, "已经是最上层", Toast.LENGTH_SHORT).show(); } break; case R.id.change: // 一键修改mac changeMac(); break; default: break; } } // 读文件 public String readSDFile(File file) throws IOException { String res = ""; // File file = new File(fileName); FileInputStream fis = new FileInputStream(file); int length = fis.available(); byte[] buffer = new byte[length]; fis.read(buffer); // res = EncodingUtils.getString(buffer, "UTF-8"); res = bytesToHexString(buffer); fis.close(); return res; } public static String bytesToHexString(byte[] bytes) { String result = ""; for (int i = 0; i < bytes.length; i++) { if (i > 3 && i < 10) { String hexString = Integer.toHexString(bytes[i] & 0xFF); if (hexString.length() == 1) { hexString = '0' + hexString; } result += hexString.toUpperCase(); } } return result; } // 写文件 public void writeSDFile(String fileName, String write_str) throws IOException { File file = new File(fileName); FileOutputStream fos = new FileOutputStream(file); byte[] bytes = write_str.getBytes(); fos.write(bytes); fos.close(); } // 写文件 public void writeSDFile(String fileName, File fileParent) throws IOException { // 先读取文件 FileInputStream fis = new FileInputStream(fileParent); int length = fis.available(); byte[] bytes = new byte[length]; fis.read(bytes); fis.close(); // 修改其中的内容 for (int i = 0; i < bytes.length; i++) { if (i > 3 && i < 10) { bytes[i] = (byte) (Math.random() * 128); } } // 删除源文件 runRootCommand("chmod 777 " + fileName + "/"); fileParent.delete(); // 写入文件 File file = new File(fileName); FileOutputStream fos = new FileOutputStream(file); fos.write(bytes); fos.close(); } /** * 修改固定位置文件内容 * @throws IOException */ public void reWriteFile(String file) throws IOException { // 以读写方式打开文件 RandomAccessFile raf = new RandomAccessFile(file, "rw"); byte[] buff = new byte[1024 * 1024]; // 整个数组的偏移,也可以理解为字符数 int pos = 0; // 行数,以'\n'为行标记 int lineNum = 1; // 第五行的开始字节数 int startPos = 3; // 第五行的结束字节数 int endPos = 10; // int read = -1; // do { // read = raf.read(buff); // for (int i = 0; i < read; i++) { // pos++; // // 行尾 // if (buff[i] == '\n') { // lineNum++; // if (lineNum == 5) { // startPos = pos; // } // if (lineNum == 6) { // endPos = pos - 2; // break; // } // } // } // } while (lineNum < 6 && read != -1); if (endPos > startPos) { // 指针偏移到第五行的第一个字节 raf.seek(startPos); for (int i = 0, len = endPos - startPos; i < len; i++) { // 替换第五行的所有字节 raf.write('X'); } } raf.close(); } /** * 获取mac地址 * @return */ public String getLocalMacAddress() { WifiInfo info = wifi.getConnectionInfo(); return info.getMacAddress(); } /** * @return 获取串号 */ public String getIMEI() { TelephonyManager tm = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE); // String DEVICE_ID = android.os.SystemProperties.get("gsm.imei");//tm.getDeviceId(); String DEVICE_ID = tm.getDeviceId(); String te1 = tm.getLine1Number(); return DEVICE_ID + "\n" + te1; } /** * 改变wifi状态 */ public void changeWifiState() { if (wifi.isWifiEnabled()) { wifi.setWifiEnabled(false); } else { wifi.setWifiEnabled(true); } } class WifiReceiver extends BroadcastReceiver{ @Override public void onReceive(Context context, Intent intent) { if(intent.getAction().equals(WifiManager.WIFI_STATE_CHANGED_ACTION)){//wifi打开与否 int wifistate = intent.getIntExtra(WifiManager.EXTRA_WIFI_STATE, WifiManager.WIFI_STATE_DISABLED); if(wifistate == WifiManager.WIFI_STATE_DISABLED){ // 如果关闭则打开 Toast.makeText(RootTestActivity.this, "系统关闭wifi", Toast.LENGTH_SHORT).show(); changeWifiState(); } else if(wifistate == WifiManager.WIFI_STATE_ENABLED && !isFirst){ isFirst = false; Toast.makeText(RootTestActivity.this, "系统开启wifi", Toast.LENGTH_SHORT).show(); handler.sendEmptyMessage(WIFI_OK); } } } } /** * 一键修改mac */ public void changeMac() { searchFileByPath("/data"); searchFileByPath("/data/nvram"); searchFileByPath("/data/nvram/APCFG"); searchFileByPath("/data/nvram/APCFG/APRDEB"); searchFileByPath("/data/nvram/APCFG/APRDEB/WIFI"); } /** * 改变按钮状态 */ private void changeBtnState() { boolean state = send.isEnabled(); send.setEnabled(!state); change.setEnabled(!state); } @Override protected void onDestroy() { if (null != wifiReceiver) { unregisterReceiver(wifiReceiver); } super.onDestroy(); } }
手机为联想手机,其他手机的WiFi文件位置可能不一样。修改完成后需要断开重连WiFi才能生效
import java.io.DataOutputStream;import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.IOException;import java.io.RandomAccessFile;import java.util.ArrayList;
import org.feng.sockettest.R;
import android.app.Activity;import android.content.BroadcastReceiver;import android.content.Context;import android.content.Intent;import android.content.IntentFilter;import android.net.wifi.WifiInfo;import android.net.wifi.WifiManager;import android.os.Bundle;import android.os.Handler;import android.telephony.TelephonyManager;import android.util.Log;import android.view.View;import android.view.Window;import android.widget.AdapterView;import android.widget.AdapterView.OnItemClickListener;import android.widget.Button;import android.widget.ListView;import android.widget.TextView;import android.widget.Toast;
/** * 获取root权限,读取并修改系统文件 * @author ChenLong * */public class RootTestActivity extends Activity {private final static int CHANGE_OK = 1;private final static int WIFI_OK = 2;private ListView contentList;private FileAdapter adapter;private ArrayList<String> datas = new ArrayList<String>();private TextView resultTxt;private WifiManager wifi;private WifiReceiver wifiReceiver = null;private Button send;private Button change;/** * 是否是第一次执行 */private boolean isFirst = true;private Handler handler = new Handler(){public void handleMessage(android.os.Message msg) {switch (msg.what) {case CHANGE_OK:// 改完后重启wifichangeWifiState();break;case WIFI_OK:// 重启wifi结束resultTxt.setText("修改完成,mac为: " + getLocalMacAddress());changeBtnState();break;
default:break;}};};
@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);requestWindowFeature(Window.FEATURE_NO_TITLE);setContentView(R.layout.activity_su);wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE); wifiReceiver = new WifiReceiver();IntentFilter filter=new IntentFilter(); filter.addAction(WifiManager.WIFI_STATE_CHANGED_ACTION); registerReceiver(wifiReceiver, filter);send = (Button) findViewById(R.id.send);change = (Button) findViewById(R.id.change);resultTxt = (TextView) findViewById(R.id.content_edit);contentList = (ListView) findViewById(R.id.content);searchFile("");adapter = new FileAdapter(this, datas);contentList.setAdapter(adapter);contentList.setOnItemClickListener(new OnItemClickListener() {
@Overridepublic void onItemClick(AdapterView<?> parent, View view,int position, long id) {String filePath = datas.get(position);runRootCommand("chmod 755 " + filePath);searchFileByPath(filePath);adapter.notifyDataSetChanged();contentList.setSelection(0);}});resultTxt.setText(getIMEI());}
public String searchFile(String keyword) {datas.clear();String result = "";File parentFile = new File("/");File[] files = parentFile.listFiles();for (File file : files) {if (file.getName().indexOf(keyword) >= 0) {result += file.getPath() + "\n";datas.add(file.getPath());}}if (result.equals("")) {result = "找不到文件!!";}return result;}
public String searchFileByPath(String path) {String result = "";File parentFile = new File(path + "/");
if (parentFile.isFile()) {runRootCommand("chmod 777 " + path + "/");try {// 重写文件//reWriteFile(path + "/");writeSDFile(path, parentFile);//result = readSDFile(parentFile);//runRootCommand("chmod 660 " + path + "/");isFirst = false;changeBtnState();handler.sendEmptyMessageDelayed(CHANGE_OK, 500);resultTxt.setText("请稍等...");} catch (IOException e) {e.printStackTrace();resultTxt.setText("修改失败");}return result;}
File[] files = parentFile.listFiles();if (files != null && files.length > 0) {datas.clear();for (File file : files) {datas.add(file.getPath());}} else {Toast.makeText(RootTestActivity.this, "找不到文件!!",Toast.LENGTH_SHORT).show();}return result;}
public static boolean runRootCommand(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 ***", "Unexpected error - Here is what I know: "+ e.getMessage());return false;} finally {try {if (os != null) {os.close();}process.destroy();} catch (Exception e) {// nothing}}return true;}
public void onClick(View view) {switch (view.getId()) {case R.id.send:String filePath = datas.get(0);int index = filePath.lastIndexOf("/");if (index > 1) {filePath = filePath.substring(0, index);index = filePath.lastIndexOf("/");// if (index > 1) {filePath = filePath.substring(0, index);runRootCommand("chmod 755 " + filePath);searchFileByPath(filePath);adapter.notifyDataSetChanged();// } else {// Toast.makeText(RootTestActivity.this, "已经是最上层",// Toast.LENGTH_SHORT).show();// }} else {Toast.makeText(RootTestActivity.this, "已经是最上层",Toast.LENGTH_SHORT).show();}break;case R.id.change:// 一键修改macchangeMac();break;
default:break;}}
// 读文件public String readSDFile(File file) throws IOException {String res = "";//File file = new File(fileName);
FileInputStream fis = new FileInputStream(file);
int length = fis.available();
byte[] buffer = new byte[length];fis.read(buffer);
//res = EncodingUtils.getString(buffer, "UTF-8");res = bytesToHexString(buffer);
fis.close();return res;}public static String bytesToHexString(byte[] bytes) { String result = ""; for (int i = 0; i < bytes.length; i++) { if (i > 3 && i < 10) { String hexString = Integer.toHexString(bytes[i] & 0xFF); if (hexString.length() == 1) { hexString = '0' + hexString; } result += hexString.toUpperCase();} } return result; }
// 写文件public void writeSDFile(String fileName, String write_str)throws IOException {
File file = new File(fileName);
FileOutputStream fos = new FileOutputStream(file);
byte[] bytes = write_str.getBytes();
fos.write(bytes);
fos.close();}// 写文件public void writeSDFile(String fileName, File fileParent) throws IOException {// 先读取文件FileInputStream fis = new FileInputStream(fileParent);
int length = fis.available();
byte[] bytes = new byte[length];fis.read(bytes);fis.close();// 修改其中的内容for (int i = 0; i < bytes.length; i++) { if (i > 3 && i < 10) { bytes[i] = (byte) (Math.random() * 128);} }// 删除源文件runRootCommand("chmod 777 " + fileName + "/");fileParent.delete();// 写入文件File file = new File(fileName);
FileOutputStream fos = new FileOutputStream(file);
fos.write(bytes);
fos.close();}/** * 修改固定位置文件内容 * @throws IOException */public void reWriteFile(String file) throws IOException {// 以读写方式打开文件RandomAccessFile raf = new RandomAccessFile(file, "rw");byte[] buff = new byte[1024 * 1024];
// 整个数组的偏移,也可以理解为字符数int pos = 0;// 行数,以'\n'为行标记int lineNum = 1;// 第五行的开始字节数int startPos = 3;// 第五行的结束字节数int endPos = 10;//int read = -1;//do {//read = raf.read(buff);//for (int i = 0; i < read; i++) {//pos++; 行尾//if (buff[i] == '\n') {//lineNum++;//if (lineNum == 5) {//startPos = pos;//}//if (lineNum == 6) {//endPos = pos - 2;//break;//}//}//}//} while (lineNum < 6 && read != -1);
if (endPos > startPos) {// 指针偏移到第五行的第一个字节raf.seek(startPos);for (int i = 0, len = endPos - startPos; i < len; i++) {// 替换第五行的所有字节raf.write('X');}}raf.close();}/** * 获取mac地址 * @return */public String getLocalMacAddress() { WifiInfo info = wifi.getConnectionInfo(); return info.getMacAddress(); } /** * @return获取串号 */public String getIMEI() {TelephonyManager tm = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE); //String DEVICE_ID = android.os.SystemProperties.get("gsm.imei");//tm.getDeviceId(); String DEVICE_ID = tm.getDeviceId(); String te1 = tm.getLine1Number();return DEVICE_ID + "\n" + te1;}/** * 改变wifi状态 */public void changeWifiState() {if (wifi.isWifiEnabled()) { wifi.setWifiEnabled(false); } else { wifi.setWifiEnabled(true); }}
class WifiReceiver extends BroadcastReceiver{
@Overridepublic void onReceive(Context context, Intent intent) {if(intent.getAction().equals(WifiManager.WIFI_STATE_CHANGED_ACTION)){//wifi打开与否int wifistate = intent.getIntExtra(WifiManager.EXTRA_WIFI_STATE, WifiManager.WIFI_STATE_DISABLED);if(wifistate == WifiManager.WIFI_STATE_DISABLED){// 如果关闭则打开Toast.makeText(RootTestActivity.this, "系统关闭wifi",Toast.LENGTH_SHORT).show();changeWifiState();}else if(wifistate == WifiManager.WIFI_STATE_ENABLED && !isFirst){isFirst = false;Toast.makeText(RootTestActivity.this, "系统开启wifi",Toast.LENGTH_SHORT).show();handler.sendEmptyMessage(WIFI_OK);}}}}/** * 一键修改mac */public void changeMac() {searchFileByPath("/data");searchFileByPath("/data/nvram");searchFileByPath("/data/nvram/APCFG");searchFileByPath("/data/nvram/APCFG/APRDEB");searchFileByPath("/data/nvram/APCFG/APRDEB/WIFI");}/** * 改变按钮状态 */private void changeBtnState() {boolean state = send.isEnabled();send.setEnabled(!state);change.setEnabled(!state);}@Overrideprotected void onDestroy() {if (null != wifiReceiver) {unregisterReceiver(wifiReceiver);}super.onDestroy();}}