于是Java提供了一个这样的接口RunTime;
Runtime
Class Overview
--------------------------------------------------------------------------------
Allows Java applications to interface with the environment in which they are running. Applications can not create an instance of this class, but they can get a singleton instance by invoking getRuntime().
大概就是提供了一个Java操作底层操作系统的接口
修改时间的测试程序:
MainActivity.java
public class MainActivity extends Activity {
private Context mContext;
private Button btnShowTime;
private Button btnSetTime;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mContext = this;
btnSetTime = (Button) findViewById(R.id.button1);
btnSetTime.setText("Set To : 2015-11-20 12:12:12");
findViewById(R.id.button1).setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View v) {
SetTime("2015-11-20 12:12:12");
}
});
btnShowTime = (Button) findViewById(R.id.btnShowTime);
findViewById(R.id.btnShowTime).setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View v) {
Calendar cal = Calendar.getInstance();
btnShowTime.setText(cal.get(Calendar.YEAR)+"-");
btnShowTime.append(cal.get(Calendar.MONTH)+"-");
btnShowTime.append(cal.get(Calendar.DAY_OF_MONTH)+" ");
btnShowTime.append(cal.get(Calendar.HOUR_OF_DAY)+":");
btnShowTime.append(cal.get(Calendar.MINUTE)+":");
btnShowTime.append(cal.get(Calendar.SECOND)+"");
}
});
}
public void SetTime(String datetime) {
try {
String time = datetime.replaceAll("-", "");
time = time.replaceAll(" ", ".");
time = time.replaceAll(":", "");
Process process = Runtime.getRuntime().exec("su");
DataOutputStream os = new DataOutputStream(
process.getOutputStream());
os.writeBytes("setprop persist.sys.timezone GMT\n");
// os.writeBytes("/system/bin/date -s " + time + "\n");// 效果一样
os.writeBytes("date -s " + time + "\n");
os.writeBytes("clock -w\n");
os.writeBytes("exit\n");
os.flush();
} catch (IOException e) {
e.printStackTrace();
}
}
}
程序中使用的replace是把时间格式化为: 20151120.121212 ;
关于最后时间为2015-10-20,原因是月份是从0开始算的,而其他年,日,时分秒都是符合日常习惯的;
修改权限:
private void changePermission(String filePath) {
String command = "chmod 777 " + filePath;
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();
} catch (Exception e) {
e.printStackTrace();
}
}
在AndroidManifest.xml中不需要配置任何权限;但是前提是系统已经ROOT了;
部分我们常用的一些命令:
1 重启系统:reoot
2 修改文件权限:chmod 777 system/test; chmod 777 system/test/abc.txt ;
3 删除文件及文件夹:rm test.txt ; rmdir test; rm -rf * ;
4 输入返回键:input keyevent 4 ;
5 以太网开启/关闭:ifconfig eth0 down; ifconfig eth0 up;
以下为个人看法不经核实望纠正:
RunTime是在一个独立的进程Process中执行
所以以下两种情况是不一样的:
方法一:
Runtime runtime = Runtime.getRuntime();
Process proc = runtime.exec("su");
runtime.exec("cmd1");
runtime.exec("cmd2");
方法二:
Process proc = runtime.exec("su");
DataOutputStream os = new DataOutputStream(proc.getOutputStream());
os.writeBytes("cmd1\n");
os.writeBytes("cmd2\n");
os.writeBytes("exit\n");
os.flush();
以上两种方法中
第一种方法中su,cmd1和cmd2分别在不同的进程中进行,所以cmd1和cmd2没有得到ROOT权限(出现permission denied)。
而第二种方法中cmd1和cmd2在同一个进程中进行,而该进程一开始拥有了ROOT权限,所以cmd1和cmd2就是ROOT权限执行。