سؤال

I have set alarm to get trigger after 60 seconds. but the alarm is getting triggered after 80/90 seconds (But not exactly after 60 seconds). How can i set alarm to get trigger exact at specified time?

 //calling method to set alarm after 60 seconds
    startAlarm(context, 1, System.currentTimeMillis()+60000);

    //method defination
    public static void startAlarm(Context context, int timerType, long nextScheduledTime) {
            EABLog.d(TAG, " ~~~INSIDE START ALARM~~~~~ "+timerType);
            Intent intent = new Intent(context, AlarmReceiver.class);
            intent.putExtra(Constants.KEY_TIMER_TYPE, timerType);
            intent.setAction(""+timerType);
            PendingIntent pollPendingIntent = PendingIntent.getBroadcast(context,
                    timerType, intent, PendingIntent.FLAG_ONE_SHOT);
            AlarmManager alarmManager = (AlarmManager) context
                    .getSystemService(Context.ALARM_SERVICE);
            alarmManager.setExact(AlarmManager.RTC_WAKEUP, nextScheduledTime,
                    pollPendingIntent);
            EABLog.d(TAG, System.currentTimeMillis()+" ALARM IS SET AT "+nextScheduledTime+" TYPE :"+timerType);
        }


//permission added in Android Manifest
    <uses-permission android:name="android.permission.WAKE_LOCK"/>
هل كانت مفيدة؟

المحلول

As docs say about setExact - The alarm will be delivered as nearly as possible to the requested trigger time. So it is not as exact as you think :)

Look at public void setWindow (int type, long windowStartMillis, long windowLengthMillis, PendingIntent operation) methods 3rd parameter - windowLengthMillis The length of the requested delivery window, in milliseconds. The alarm will be delivered no later than this many milliseconds after windowStartMillis. This might do the trick :)

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top