Android开发:代码实现系统关机和重启

/ Android / 没有评论 / 1908浏览

Android开发:代码实现系统关机和重启

情景分析

需求:在系统没有root的条件下,应用程序控制系统关机和重启

解决方法

测试环境:Android 4.4手机 ;Android Studio

1 将自己的应用程序获取系统权限

参考:Android应用如何获取系统权限

2 代码实现如下

package com.smart.rebootdemo;

import android.content.Context;
import android.content.Intent;
import android.os.PowerManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;

import java.lang.reflect.Method;

public class MainActivity extends AppCompatActivity {

    private String TAG = MainActivity.class.getSimpleName();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        findViewById(R.id.btn_reBoot).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                reBoot3();
            }
        });
        findViewById(R.id.btn_shutDown).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                shutDowm();
            }
        });
    }


    /**
     * 通过广播的方式,重启系统,不推荐这种方式,因为我在系统日志,发现这是系统异常关机
     */
    private void reBoot() {
        Intent i = new Intent(Intent.ACTION_REBOOT);
        i.putExtra("nowait", 1);
        i.putExtra("interval", 1);
        i.putExtra("window", 0);
        sendBroadcast(i);
    }

    /**
     * 通过Runtime,发送指令,重启系统,测试结果,不起作用,可能需要root
     */
    private void reBoot2() {
        try {
            Log.v(TAG, "root Runtime->reboot");
            Process proc = Runtime.getRuntime().exec(new String[]{"su", "-c", "reboot "}); //关机
            proc.waitFor();
        } catch (Exception ex) {
            ex.printStackTrace();
        }

    }

    private void reBoot3() {
        /* 弹出重启设备菜单 */
        Log.v(TAG, "reBoot3");
        PowerManager pManager = (PowerManager) getSystemService(Context.POWER_SERVICE); //重启到fastboot模式
        pManager.reboot("重启");
    }
    
    /**
     * 关机
     */
    private void shutDowm() {
        Log.v(TAG, "shutDowm");
        try {
            //获得ServiceManager类
            Class ServiceManager = Class
                .forName("android.os.ServiceManager");
            //获得ServiceManager的getService方法
            Method getService = ServiceManager.getMethod("getService", java.lang.String.class);
            //调用getService获取RemoteService
            Object oRemoteService = getService.invoke(null, Context.POWER_SERVICE);
            //获得IPowerManager.Stub类
            Class cStub = Class.forName("android.os.IPowerManager$Stub");
            //获得asInterface方法
            Method asInterface = cStub.getMethod("asInterface", android.os.IBinder.class);
            //调用asInterface方法获取IPowerManager对象
            Object oIPowerManager = asInterface.invoke(null, oRemoteService);
            //获得shutdown()方法
            Method shutdown = oIPowerManager.getClass().getMethod("shutdown", boolean.class, boolean.class);
            //调用shutdown()方法
            shutdown.invoke(oIPowerManager, false, true);
        } catch (Exception e) {
            Log.e(TAG, e.toString(), e);
        }

    }
}