android 设备实现定时重启(无root权限或已root)

1、在manifest.xml 中添加权限

<uses-permission android:name="android.permission.ACCESS_SUPERUSER" />

2、新建DeviceAutoRebootService文件,并在manifest.xml 添加service

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;

import androidx.annotation.Nullable;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

public class DeviceAutoRebootService extends Service {
    private ScheduledExecutorService threadPool = null;
    private int betweenTime = 59;//间隔59秒执行一次
    private int delayTime = 2;//线程池开启5秒后执行
    private String time = "03:30";//重启时间一
    SimpleDateFormat sdf = new SimpleDateFormat("HH:mm", Locale.CHINA);
    String[] rebootArray = {"su", "-c", "reboot"};//执行重启的命令
    String dateStr = "";//获取的时间

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        threadPool = Executors.newScheduledThreadPool(3);
        executeShutDown();

    }

    public void executeShutDown() {
        threadPool.scheduleAtFixedRate(new Runnable() {
            @Override
            public void run() {
                dateStr = sdf.format(new Date());
                Log.d("重启", dateStr.equals(time) + "=" + dateStr);
                // root机子
                if (dateStr.equals(time)) {
                    try {
                        Runtime.getRuntime().exec(rebootArray);
                        exec("reboot");
                    } catch (IOException io) {
                        io.printStackTrace();
                    }
                }
            }
        }, delayTime, betweenTime, TimeUnit.SECONDS);
    }

    private String exec(String command) {

        Process process = null;
        BufferedReader reader = null;
        InputStreamReader is = null;
        DataOutputStream os = null;

        try {
            process = Runtime.getRuntime().exec("su");
            is = new InputStreamReader(process.getInputStream());
            reader = new BufferedReader(is);
            os = new DataOutputStream(process.getOutputStream());
            os.writeBytes(command + "\n");
            os.writeBytes("exit\n");
            os.flush();
            int read;
            char[] buffer = new char[4096];
            StringBuilder output = new StringBuilder();
            while ((read = reader.read(buffer)) > 0) {
                output.append(buffer, 0, read);
            }
            process.waitFor();
            return output.toString();
        } catch (IOException | InterruptedException e) {
            throw new RuntimeException(e);
        } finally {
            try {
                if (os != null) {
                    os.close();
                }

                if (is != null) {
                    is.close();
                }

                if (reader != null) {
                    reader.close();
                }

                if (process != null) {
                    process.destroy();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public void onDestroy() {
        threadPool.shutdown();
        threadPool = null;
        dateStr = null;
        super.onDestroy();
    }
}


3、MainActivity  onCreate方法中开启service

startService(new Intent(MainActivity.this, DeviceAutoRebootService.class));

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

少十步

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值