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

设备必须是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:哦,那我去查查。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值