你好,游客 登录 注册 搜索
背景:
阅读新闻

Android在代码中实现重启(reboot)

[日期:2014-02-24] 来源:Linux社区  作者:youngwhz1 [字体:大 中 小]

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

更多Android相关信息见Android 专题页面 http://www.linuxidc.com/topicnews.aspx?tid=11

linux
相关资讯       Android代码重启  Android reboot 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款