安卓开发 -- 定时重启设备

版权声明:没有申明版权,你们随意! https://blog.csdn.net/baby986911/article/details/84633231

设备必须是ROOT过的。

不废话。上代码!

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.support.annotation.Nullable;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;


/**
 * createdUser : xuyanyun
 * createData :  2018/11/29/029
 * remark : 定时重启的服务
 * hasBug? 佛祖保佑!
 * name :
 */

public class ShutDownDeviceService extends Service {
    private ScheduledExecutorService threadPool = null;
    private int betweenTime = 59;//间隔59秒执行一次
    private int delayTime = 2;//线程池开启5庙后执行
    private String oneTime = "06:00";//重启时间一
    private String twoTime = "12:00";//重启时间二
    SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
    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());
                if (dateStr.equals(oneTime) || dateStr.equals(twoTime)) {
                    try {
                        Runtime.getRuntime().exec(rebootArray);
                    } catch (IOException io) {
                    }
                }
            }
        }, delayTime, betweenTime, TimeUnit.SECONDS);
    }

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

A:service怎么启动?

B:哈哈哈哈,我不会!

A:哦,那我去查查。

猜你喜欢

转载自blog.csdn.net/baby986911/article/details/84633231