用Java调用shell命令实现,界面用Java写的。只是一个练手的小项目,代码写的不好。而且有些系统不能正常使用,比如moto,魅族倒是能使用。这可能关系到bin的安全问题,这个就不懂了。
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import java.io.IOException;
public class MainActivity extends Activity {
private ListView listView; //显示操作
private AlertDialog.Builder builder; //提示窗
private static final String[] strs = new String[]{"关机", "重启", "重启到recovery",
"重启到fastboot", "重启到bootloader"}; //操作
private static final String[] command = new String[]{"reboot -p", "reboot", "reboot recovery",
"reboot fastboot", "reboot bootloader"}; //命令
int a;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
listView = new ListView(this);
listView.setAdapter(new ArrayAdapter(this, //设置listview的值
android.R.layout.simple_expandable_list_item_1, strs));
setContentView(listView);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView adapterView, View view, int i, long l) {
a = i;
builder = new AlertDialog.Builder(MainActivity.this); //初始化提示窗
builder.setTitle("提示"); //设置提示窗
builder.setMessage("确定" + strs[a] + "?"); //i为当前item值,用来取数组中的数值
builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
dialogInterface.dismiss();
try { //执行预先设置的数组中的值
Runtime.getRuntime().exec(command[a]);
} catch (IOException e) {
e.printStackTrace();
}
}
});
builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
dialogInterface.dismiss();
}
});
builder.create().show(); //show提示窗
}
});
}
}
网友评论