第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

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

服務啟動后觸發(fā)警報而不是設(shè)置時間

服務啟動后觸發(fā)警報而不是設(shè)置時間

Qyouu 2021-10-20 15:01:09
我試圖在每天早上 5 點設(shè)置一個觸發(fā)器,以使手機為 USER_PRESENT 廣播做好準備,但是一旦服務啟動,觸發(fā)器就會關(guān)閉。服務 (PAService) 通過使用stopService和startService打開主活動來關(guān)閉和打開。此代碼在模擬器中運行良好,但不適用于實際的 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條經(jīng)驗 獲得超2個贊

manager.setRepeating(AlarmManager.RTC_WAKEUP,

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

            // in every 24

            // hours

            pendingIntent);

的2nd parameter,觸發(fā)時間觸發(fā)報警器立即如果calendar.getTimeInMillis()是在過去。所以發(fā)生的事情是你可能在晚上 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 關(guān)注
  • 159 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學習伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號