设为首页 加入收藏

TOP

Android在代码中实现重启(reboot)
2014-11-24 01:09:13 来源: 作者: 【 】 浏览:3
Tags:Android 代码 实现 重启 reboot

public void rebootAction(){
new AlertDialog.Builder(this)
.setTitle("Warning")
.setMessage("Sure to reboot ")
.setNegativeButton("Cancel", null)
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
String cmd = "su -c reboot";
try {
Runtime.getRuntime().exec(cmd);
} catch (Exception e){
Toast.makeText(getApplicationContext(), "Error! Fail to reboot.", Toast.LENGTH_SHORT).show();
}
}
})
.show();
}


还有一种方法,但是需要系统级程序才可以使用,否则会提示权限不够。


try {
Intent intent = new Intent(Intent.ACTION_REBOOT);
intent.putExtra("nowait", 1);
intent.putExtra("interval", 1);
intent.putExtra("window", 0);
this.sendBroadcast(intent);
} catch (Exception e){
new AlertDialog.Builder(this).setTitle("Error").setMessage(e.getMessage()).setPositiveButton("OK", null).show();
}


在普通app中调用会报错:


java.lang.SecurityException: Permission Denial: not allowed to send broadcast android.intent.action.REBOOT from pid=11892, uid=10146


】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇Python实现UDP通信 下一篇Naive Bayes 朴素贝叶斯的Java代..

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: