Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Alarm Manager not firing on screen is off

I set alarm manager for time control.My purpose continue to time controlling on screen off. While screen is on everything is ok but when i locked to device(screen of) my broadcast receiver not receiver anything

i tried lots of different solution (in Manifest export:false or process:":remote") about similar problem but problem not solved. I need to help. I hope someone solved that problem before.

For set alarm manager

    AlarmManager alarmManager = (AlarmManager)             
    context.getSystemService(Context.ALARM_SERVICE);

    Intent intent = new Intent(context, TimeService.class);
    intent.setAction(SETUP_TIMER_FOR_NOTIFICATION);

    PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 112, intent, PendingIntent.FLAG_UPDATE_CURRENT);

    long now = System.currentTimeMillis();
    long minute = 1000 * 60;

    alarmManager.cancel(pendingIntent);
    alarmManager.setInexactRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime() , minute, pendingIntent);

My Receiver

public class TimeService extends WakefulBroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {



Log.i("TimeService", "onReceive");

 Intent timeIntent = new Intent(context, NotificationService.class);
        timeIntent.setAction(SETUP_TIMER_FOR_NOTIFICATION);

        startWakefulService(context, timeIntent);


   }
}

Manifest.xml

<receiver
            android:name="service.TimeService"
            android:enabled="true"
            android:exported="false"
            >

            <intent-filter>
                <action android:name="alarm_timer_for_notification" />
            </intent-filter>
        </receiver>
like image 605
gokhan Avatar asked Oct 22 '25 07:10

gokhan


1 Answers

setAndAllowWhileIdle this is worked when screen is off and phone in idle mode

In marshmallow introduce the doze mode for battery saving. So the alarm not fired correctly in repeat mode.

So we use https://developer.android.com/reference/android/app/AlarmManager.html#setAndAllowWhileIdle(int,%20long,%20android.app.PendingIntent)

From marshmallow and up, alarm waked up only 10 min interval,setAndAllowWhileIdle also fire service or notification 10 minutes once when phone is idle (screen is off).

In normal mode time interval working fine (I have checked 3 min's interval)

public class TimeService extends WakefulBroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {

    Log.i("TimeService", "onReceive");

    Intent timeIntent = new Intent(context, NotificationService.class);
    timeIntent.setAction(SETUP_TIMER_FOR_NOTIFICATION);

    startWakefulService(context, timeIntent);

    setAlarm(context);
}

public void setAlarm(Context context)
{
    Timber.v(TAG +"set Alarm");
    PreferencesHelper preferencesHelper =new PreferencesHelper(context);
    AlarmManager alarmManager = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
    Intent intent = new Intent(context, TimeService.class);
    PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, 0);
    int intervalInMinutes = preferencesHelper.getInt(context.getResources().getString(R.string.sha_loc_intervals), 1)* 60000;
      if (android.os.Build.VERSION.SDK_INT >= 23) {
        alarmManager.setAndAllowWhileIdle(AlarmManager.ELAPSED_REALTIME_WAKEUP,
                SystemClock.elapsedRealtime()+intervalInMinutes , pendingIntent);
    } else if (android.os.Build.VERSION.SDK_INT >= 19
            && android.os.Build.VERSION.SDK_INT < 23) {
        alarmManager.setExact(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime()+intervalInMinutes , pendingIntent);
    } else {
        alarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime()+intervalInMinutes , pendingIntent);
    }
}

public void cancelAlarm(Context context)
{
    Timber.v(TAG +"cancel Alarm");
    Intent intent = new Intent(context, TimeService.class);
    PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, 0);
    AlarmManager alarmManager = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
    alarmManager.cancel(pendingIntent);
}}

<receiver android:name=".TimeService"
              android:enabled="true"
              android:exported="true"
              android:process=":remote">
</receiver>

In MainActivity.class

TimeService timeService =new TimeService();

timeService.setAlarm(MainActivity.this);

like image 109
Saravanan Avatar answered Oct 24 '25 23:10

Saravanan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!