Service run on specific Time Android

送分小仙女□ 提交于 2019-12-04 01:59:55

问题


I am trying to run service on every day only on Specific time, lets say every day 8 :00 am and here I found two methods as

public static void setScheduleMessage(Context context, int hours,
        int minuts, int seconds) {
    Calendar calendar = Calendar.getInstance();
    // 8 AM
    calendar.set(Calendar.HOUR_OF_DAY, hours);
    calendar.set(Calendar.MINUTE, minuts);
    calendar.set(Calendar.SECOND, seconds);
    PendingIntent pendingIntent = PendingIntent.getService(context, 0,
            new Intent(context, MessageSchduledService.class),
            PendingIntent.FLAG_UPDATE_CURRENT);
    AlarmManager alarmManager = (AlarmManager) context
            .getSystemService(Context.ALARM_SERVICE);
    alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,
            calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY,
            pendingIntent);
}

But problem with this method is, it is running every hour after specified time.

and here is second method I used which is not executing my service ?

public static void setScheduleMessage2(Context context, int hours,
        int minuts, int seconds) {
    Calendar cur_cal = new GregorianCalendar();
    cur_cal.setTimeInMillis(System.currentTimeMillis());// set the current

     // time and date for

     // this calendar
    Calendar cal = new GregorianCalendar();
    cal.add(Calendar.DAY_OF_YEAR, cur_cal.get(Calendar.DAY_OF_YEAR));
    cal.set(Calendar.HOUR_OF_DAY, hours);
    cal.set(Calendar.MINUTE, minuts);
    cal.set(Calendar.SECOND, cur_cal.get(Calendar.SECOND));
    cal.set(Calendar.MILLISECOND, cur_cal.get(Calendar.MILLISECOND));
    cal.set(Calendar.DATE, cur_cal.get(Calendar.DATE));
    cal.set(Calendar.MONTH, cur_cal.get(Calendar.MONTH));
    Intent intent = new Intent(context, MessageSchduledService.class);

    PendingIntent pintent = PendingIntent.getService(context, 0, intent, 0);
    AlarmManager alarm = (AlarmManager) context
            .getSystemService(Context.ALARM_SERVICE);

    alarm.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(),
            AlarmManager.INTERVAL_DAY, pintent);

} 

POST : In my app I just want to push message from my app Database to Message Data base at specific time every day. please if anybody having an idea about this, then let me know what i am doing wrong in these methods ?


回答1:


Have a look Alarms
@ RTC examples

// Set the alarm to start at approximately 2:00 p.m.
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR_OF_DAY, 14);

// With setInexactRepeating(), you have to use one of the AlarmManager interval
// constants--in this case, AlarmManager.INTERVAL_DAY.
alarmMgr.setInexactRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
        AlarmManager.INTERVAL_DAY, alarmIntent);

or

In your code

alarm.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(),
            AlarmManager.INTERVAL_DAY, pintent);

replace

AlarmManager.INTERVAL_DAY

to

24 * 60 * 60 * 1000

. which means repeat alarm exactly after 24 hours



来源:https://stackoverflow.com/questions/20159304/service-run-on-specific-time-android

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