---
title: "Android使用代码实现关机/重启"
author: "haliluya4"
date: "2012-08-09T12:26:13Z"
tags: []
categories: ["移动开发"]
slug: "android"
draft: false
lastmod: "2013-08-21T23:44:36Z"
id: 1626543
url: "https://johnsonxu.iteye.com/blog/1626543"
---

主要思路来自网络！

 

1、需要源码才能编译

 

2、修改项目的Android.mk文件，添加

LOCAL\_CERTIFICATE := platform

 

3、AndroidManifest.xml中添加权限

  3.1 manifest标签中添加

    android:sharedUserId="android.uid.system"

  3.2 使用权限

    &lt;uses-permission android:name="android.permission.SHUTDOWN"/&gt;

 

4、java代码

```
// 创建Intent
// 如果是要重启，则使用Intent.ACTION_REBOOT
Intent intent = new Intent(Intent.ACTION_REQUEST_SHUTDOWN);
// 设置是否需要用户确认，若不需要，可以不设置或设置为false
intent.putExtra(Intent.EXTRA_KEY_CONFIRM, true);
// 当作新任务执行
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
// 启动
startActivity(intent);
```

 

据说还可以使用Broadcast的方式调用，不过我试了一下，窗口是出来了，但一直停在关机的进度条那。不知道是不是机子的问题。

代码如下：

```
Intent i = new Intent(Intent.ACTION_REBOOT);
i.putExtra("nowait", 1);
i.putExtra("interval", 1);
i.putExtra("window", 0);
sendBroadcast(i);
```