1

Hi I am working on demo app in which I am using AlarmManager to schedule a service at 8:00 daily. I am using Calendar to schedule service at 8:00 clock but this AlarmManager is not running a service at scheduled time and even after 30 mins.

Below is my code to do this :-

public static void scheduleVerseNotificationService(Context mContext) {
    AlarmManager alarmManager = (AlarmManager) mContext.getSystemService(Context.ALARM_SERVICE);
    Intent intent = new Intent(mContext, DailyVerseNotificationService.class);
    PendingIntent pendingIntent = PendingIntent.getBroadcast(mContext, 0, intent, 0);

    // Set the alarm to start at approximately 08:00 morning.
    Calendar calendar = Calendar.getInstance();
    calendar.setTimeInMillis(System.currentTimeMillis());
    calendar.set(Calendar.HOUR_OF_DAY, 8);

    alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent);

}

TestService.java

public class TestService extends Service {
    public TestService() {
    }

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onCreate() {
        try {
            // Code
        } catch (Exception e) {
            Logger.e(Constants.TAG, e.getMessage());
        }
        stopSelf();
    }
}

AndroidManifest.xml

<service
    android:name="com.service.TestService"
    android:enabled="true"
    android:exported="true" />

Thanks in advance.

N Sharma
  • 33,489
  • 95
  • 256
  • 444

2 Answers2

1

Two solutions: 1) Instead of

    PendingIntent pendingIntent = PendingIntent.getBroadcast(mContext, 0, intent, 0);

use

    PendingIntent pendingIntent = PendingIntent.getService(mContext, 0, intent, 0);

2) if that does not work then you need to create an alarm receiver

 public class AlarmReceiver extends WakefulBroadcastReceiver {
    @Override
     public void onReceive(Context context, Intent intent) {

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

      ComponentName comp = new ComponentName(context.getPackageName(),
        DailyVerseNotificationService.class.getName());
      startWakefulService(context, (intent.setComponent(comp)));
      setResultCode(Activity.RESULT_OK);
     }

 }

change your pending intent to

     Intent intent = new Intent(mContext, AlarmReciever.class);
     PendingIntent pendingIntent = PendingIntent.getBroadcast(mContext, 0, intent, 0);

Register the receiver in manifest

     <receiver android:name="com.example.myApp.AlarmReceiver" /> 

And lastly put permissions in manifest

    <uses-permission android:name="android.permission.WAKE_LOCK" /> 

Please refer this as well: https://developer.android.com/training/scheduling/alarms.html

the above link will guide you to create a boot receiver in section "Start an Alarm When the Device Boots".

Also make sure you set:

    calendar.set(Calendar.MINUTE, 0); // for 0 minutes
    calendar.set(Calendar.SECOND, 0); // for 0 seconds
Bhushan
  • 205
  • 2
  • 14
0

Just to add to Bhushan answer.

Make sure to set alarm on boot as all registered alarms are cleared if device is turned off and rebooted.