亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

服務啟動后觸發警報而不是設置時間

服務啟動后觸發警報而不是設置時間

Qyouu 2021-10-20 15:01:09
我試圖在每天早上 5 點設置一個觸發器,以使手機為 USER_PRESENT 廣播做好準備,但是一旦服務啟動,觸發器就會關閉。服務 (PAService) 通過使用stopService和startService打開主活動來關閉和打開。此代碼在模擬器中運行良好,但不適用于實際的 Android 手機。public class PAService extends Service {static boolean is_ready_to_speak = false;PendingIntent pendingIntent;public PAService() {}public class ScreenReceiver extends BroadcastReceiver {    @Override    public void onReceive(Context context, Intent intent) {        if(is_ready_to_speak)        {            PASpeak paspeak = new PASpeak();            paspeak.sayit(getApplicationContext(),"You're using your phone for the first time this morning");           is_ready_to_speak = false;        }    }}public static class AlarmReceiver extends BroadcastReceiver {    public AlarmReceiver()    {        Log.d("AlarmReceiver func called","alarm receiver func called");    }    @Override    public void onReceive(Context context, Intent intent) {        Log.d("RECEIVED BROADCAST", "Sometype of ALARM Broadcast received");        PASpeak paspeak = new PASpeak();        paspeak.sayit(context.getApplicationContext(),"ALARM ALARM ALARM");        is_ready_to_speak = true;    }}
查看完整描述

1 回答

?
臨摹微笑

TA貢獻1982條經驗 獲得超2個贊

manager.setRepeating(AlarmManager.RTC_WAKEUP,

            calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, // for repeating

            // in every 24

            // hours

            pendingIntent);

的2nd parameter,觸發時間觸發報警器立即如果calendar.getTimeInMillis()是在過去。所以發生的事情是你可能在晚上 7 點打開應用程序。您希望鬧鐘在第二天早上 5 點響起,但您calendar.getTimeInMillis()會在同一天早上 5 點響起 。所以你需要為此添加一個檢查:


Calendar calendar = Calendar.getInstance();

// calendar.setTimeInMillis(System.currentTimeMillis()); // YOU DON'T NEED THIS LINE

calendar.set(Calendar.HOUR_OF_DAY, 5);

calendar.set(Calendar.MINUTE, 0);

calendar.set(Calendar.SECOND, 0);


Calendar current = Calendar.getInstance();


int curTime = current.getTimeInMillis();

int alarmTime = calendar.getTimeInMillis();


if (alarmTime >= curTime){

   manager.setRepeating(AlarmManager.RTC_WAKEUP,

        alarmTime, AlarmManager.INTERVAL_DAY, // for repeating

        // in every 24

        // hours

        pendingIntent);

}else{

   calendar.add(Calendar.DAY_OF_MONTH, 1);

   int alarmTime = calendar.getTimeInMillis();

   manager.setRepeating(AlarmManager.RTC_WAKEUP,

        alarmTime, AlarmManager.INTERVAL_DAY, // for repeating

        // in every 24

        // hours

        pendingIntent);

}


查看完整回答
反對 回復 2021-10-20
  • 1 回答
  • 0 關注
  • 170 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號