Android中定時(shí)重啟APP

小弟原創(chuàng)文章,轉(zhuǎn)載請注明本文出處

前言

公司之前做的一個(gè)電視機(jī)上的播報(bào)應(yīng)用(類似于銀行的那種播報(bào)“請某某某到幾號窗口”),現(xiàn)在要我做個(gè)定時(shí)重啟

正題

整體思路:定時(shí)任務(wù)在服務(wù)里執(zhí)行,當(dāng)時(shí)間等于某個(gè)特定的值就重啟
服務(wù)的代碼:

/**
 * Created by mk_who on 2017/8/3/0003.
 */
public class MyService extends Service {

    private IBinder mBinder=new Binder();
    private Timer mTimer;
    private TimerTask mTimerTask;
    private Intent intent = new Intent("com.example.restart.RECEIVER"); 
    
    @Override
    public IBinder onBind(Intent intent) {
        return mBinder;
    }

    @Override
    public void onCreate() {
        mTimer = new Timer();
        mTimerTask = new TimerTask() {
            @Override
            public void run() {
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
                        String dateStr = sdf.format(new Date());
                        if (dateStr.equals("16:20")){
              
                            sendBroadcast(intent);//發(fā)送廣播
                      
                        }
                    
                    }
                }).start();
            }
        };
        mTimer.schedule(mTimerTask,5000,59000);
        mTimer=null;
        mTimerTask=null;
    }
    @Override
    public void onDestroy() {
        super.onDestroy();
        mTimer.cancel();
        mTimerTask.cancel();
    }
}

開啟服務(wù)的頁面的部分代碼:

private void startMyService() {
        Intent intent = new Intent(getApplicationContext(), MyService.class);
        
        bindService(intent, connServiceconn, BIND_AUTO_CREATE);
    }
    private Serviceconn connServiceconn = new Serviceconn();
    private class Serviceconn implements ServiceConnection {

        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            Log.e("--------", "------開啟服務(wù)成功---");
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {

        }
    }
    @Override
    protected void onResume() {
        super.onResume();
        startMyService();//開啟服務(wù)
        filter.addAction("com.example.restart.RECEIVER");
        registerReceiver(reStartReceiver, filter);
    }

service跟activity通信的廣播代碼:

private BroadcastReceiver reStartReceiver = new BroadcastReceiver(){
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            if (action.equals("com.example.restart.RECEIVER")) {
                finish();
                Intent i = Main.this.getPackageManager()
                        .getLaunchIntentForPackage(Main.this.getPackageName());
                i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivity(i);
            }
        }
    };

當(dāng)時(shí)是下午五點(diǎn)半(六點(diǎn)鐘下班)接到的任務(wù),著急下班吃飯(就是不想加班),就用了能想到的最簡單的實(shí)現(xiàn)方式。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 179,319評論 25 708
  • 為了面試,為了高工資,廢話不多說,不定期更新。 1. Activity正常和異常情況下的生命周期分析。 Activ...
    24K男閱讀 884評論 0 0
  • 1.什么是Activity?問的不太多,說點(diǎn)有深度的 四大組件之一,一般的,一個(gè)用戶交互界面對應(yīng)一個(gè)activit...
    JoonyLee閱讀 5,868評論 2 51
  • 一、《金瓶梅》到底是一本什么樣的書? 《金瓶梅》真是一本好書,稱之為名著絕不為過。木心甚至認(rèn)為中國的四大名著應(yīng)該是...
    劉良昊閱讀 2,759評論 0 2
  • 古有半部論語治天下之說,向來為人所質(zhì)疑,其實(shí)半部論語都說多了,治國不一定要讀書,讀書不一定要認(rèn)識字。 漢高祖劉邦從...
    振興會閱讀 2,964評論 0 0

友情鏈接更多精彩內(nèi)容