Why isn't my alarm going off? (android)

痞子三分冷 提交于 2020-01-17 07:30:51

问题


My alarm does not go off at the specific time or at all (I have tried setting it to one minute of current time). I have made sure that setAlarm() is going off because the log Log.d("asas", "adasd"); goes off. I do not know what is wrong, if you see how I can fix this please let me know. Thanks

public void setAlarm(){
    AlarmManager alarmMgr = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
    Intent intent = new Intent(this, DrawOverAppsService.class);
    PendingIntent alarmIntent = PendingIntent.getActivity(MainActivity.this, 123, intent, 0);
    if(sp.getBoolean("startChecked",false) == true) {
        Log.d("asas", "adasd");
        java.util.Calendar calendar = java.util.Calendar.getInstance();
        calendar.setTimeInMillis(System.currentTimeMillis());
        calendar.set(java.util.Calendar.HOUR_OF_DAY, 19);
        alarmMgr.setRepeating(AlarmManager.RTC,
                calendar.getTimeInMillis(),
                AlarmManager.INTERVAL_DAY, alarmIntent);
    }
    if(sp.getBoolean("startChecked",false) == false) {
        Log.d("asdasd","asdasdasd");
        alarmMgr.cancel(alarmIntent);
    }
}

回答1:


Changed PendingIntent alarmIntent = PendingIntent.getActivity(MainActivity.this, 0, intent, 0); to PendingIntent alarmIntent = PendingIntent.getService(MainActivity.this, 0, intent, 0);



来源:https://stackoverflow.com/questions/44856939/why-isnt-my-alarm-going-off-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!