set repeated alarm manager for every hour in andro

2020-05-07 02:56发布

I want to fetch location every hours in android . For that i use alarm manager and set repeated alarm for every hour and just want to write into file after fix time i.e at 8 AM and 12 PM . I got a problem in setting alarm manager , while i set for every one hour but it execute in 1/2 hour .

on button click i start service :

       serviceButton.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {

            Intent myIntent = new Intent(AutoMainActivity.this, TrackerService.class);

            pendingIntent = PendingIntent.getService(AutoMainActivity.this, 0, myIntent, 0);

            AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
            alarmManager.setInexactRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, ALARM_TRIGGER_AT_TIME,
                    3600000, pendingIntent);

            //3600000 1hrs

            finish();
        }
    });

And Service Class are as :

Tracker Service.class

String FINAL_STRING;
SharedPreferences pref;
static final int START_TIME = 8;
static final int MID_TIME = 12;

    java.util.Date systemDates = Calendar.getInstance().getTime(); 
    int hour = Calendar.getInstance().get(Calendar.HOUR_OF_DAY);

    if(hour == START_TIME)
        {
            edit.putString("smsdata", FINAL_STRING);
            edit.commit();

            //sendSms(START_TAG+pref.getString("smsdata", ""));
            edit.putString("smsdata", "");
            edit.commit();
        }else {

            System.out.println("currentdate:"+simpleDateFormat.toString());
            System.out.println("current_time:"+currentTime);

            Editor edit = pref.edit();
            edit.putString("smsdata", pref.getString("smsdata", "")+FINAL_STRING+"#");
            edit.commit();

            if(hour==MID_TIME)
            {
                //sendSms(START_TAG+pref.getString("smsdata", ""));
                generateNoteOnSD("\n"+START_TAG+pref.getString("smsdata", ""));
                edit.putString("smsdata", "");
                edit.commit();
                System.out.println("mid time");

            }

        }

When i execute this the service start on every 30min. but i want on every 60min.

2条回答
女痞
2楼-- · 2020-05-07 03:21

setInexactRepeating() is the reason why it is not working as you expected. Try following:

AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
am.setRepeating(AlarmManager.RTC_WAKEUP, firstStart, interval, pendingIntent );
查看更多
做自己的国王
3楼-- · 2020-05-07 03:34

First, you really want to use one of the available constants, like INTERVAL_HOUR, with setInexactRepeating().

Second, setInexactRepeating() is inexact. Android reserves the right to flex the times of the alarms to coalesce events with other scheduled inexact alarms.

So, try switching briefly to setRepeating(). If it now works as you expect, your behavior is due to the "inexact" nature of setInexactRepeating().

Also, you can use adb shell dumpsys alarm to examine the scheduled alarms. It may be that you have two alarms scheduled, each going off once per hour.

查看更多
登录 后发表回答