0

According to this question need the background service.

How to keep a service running in background even after user quits the app?

I tried to do that, but i can not identify where should i call the alarm manager inside the service.

MyService.java

This is i tried service class.

public class MyService extends Service{

    PendingIntent pendingIntent;
    AlarmManager alarmManager;

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        // Query the database and show alarm if it applies

        // I don't want this service to stay in memory, so I stop it
        // immediately after doing what I wanted it to do.
        pendingIntent = PendingIntent.getBroadcast(
                this.getApplicationContext(), 280192, intent, PendingIntent.FLAG_CANCEL_CURRENT);

        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(System.currentTimeMillis());
        calendar.set(Calendar.HOUR_OF_DAY, 11);
        calendar.set(Calendar.MINUTE, 55);
        calendar.set(Calendar.SECOND,00);

        alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);

        alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
                AlarmManager.INTERVAL_FIFTEEN_MINUTES, pendingIntent);
//        stopSelf();

        return START_NOT_STICKY;
    }

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

    @Override
    public void onDestroy() {
//        // I want to restart this service again in one minute
//        AlarmManager alarm = (AlarmManager)getSystemService(ALARM_SERVICE);
//        alarm.set(
//                alarm.RTC_WAKEUP,
//                System.currentTimeMillis() + (1000 * 60 * 1),
//                PendingIntent.getService(this, 0, new Intent(this, MyService.class), 0)
//        );
    }
}

Please guide me to give the reminder to user to specific time period even application not running. Thank You

SonhnLab
  • 321
  • 1
  • 11
  • 1
    Possible duplicate of [Android AlarmManager not working on some devices when the app is closed](https://stackoverflow.com/questions/39739669/android-alarmmanager-not-working-on-some-devices-when-the-app-is-closed) – Gowthaman M Jun 28 '18 at 06:42

2 Answers2

1

You don't need to create your own service for this use case , you can use Alarmservice from Android framework for this, which can start the app(even if app is not running) and time set provided the phone is ON State. Now to set the alarm, call can be in any view (Activity/Fragment) . You can create a button and set action to set Alarm as you like. On invocation of alarm you use a pending broadcast receiver for any subsquent action. Call for setting Alarm as below, you can use Pending Broadcast intent to do your stuff. And trust me it will work even if your application is not running. Note - AlarmBroadCastReceiver should be a manifest receiver i.e. declared in manifest file.

private void setAlarm(int type) {


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

    // Alarm type
    int alarmType = AlarmManager.RTC;

    Calendar time = Calendar.getInstance();

    time.setTimeInMillis(System.currentTimeMillis());


    switch (type) {

    case 1:
        // Set Alarm for next 20 seconds
        time.add(Calendar.SECOND, 20);
        break;

    case 2:
        // Set Alarm for next 2 min
        time.add(Calendar.MINUTE, 2);
        break;

    case 3:
      // Set Alarm for next 30 mins
        time.add(Calendar.MINUTE, 30);
        break;

    }


    Intent broadcastIntent = new Intent(this, AlarmBroadCastReceiver.class);

    broadcastIntent.putExtras(sourceIntent);

    Random generator = new Random();

    PendingIntent pendingAlarmIntent = PendingIntent.getBroadcast(this,
            generator.nextInt(), broadcastIntent,
            PendingIntent.FLAG_ONE_SHOT);
    alarmManager.set(alarmType, time.getTimeInMillis(), pendingAlarmIntent);

}
Abhi
  • 227
  • 2
  • 5
0

First create BroadcastReceiver like below :

public class SchedulerSetupReceiver extends BroadcastReceiver {
    private static final String APP_TAG = "com.hascode.android";

    private static final int EXEC_INTERVAL = 20 * 1000;

    @Override
    public void onReceive(final Context ctx, final Intent intent) {
        Log.d(APP_TAG, "SchedulerSetupReceiver.onReceive() called");
        AlarmManager alarmManager = (AlarmManager) ctx
                .getSystemService(Context.ALARM_SERVICE);
        Intent i = new Intent(ctx, SchedulerEventReceiver.class); // explicit
                                                                    // intent
        PendingIntent intentExecuted = PendingIntent.getBroadcast(ctx, 0, i,
                PendingIntent.FLAG_CANCEL_CURRENT);
        Calendar now = Calendar.getInstance();
        now.add(Calendar.SECOND, 20);
        alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,
                now.getTimeInMillis(), EXEC_INTERVAL, intentExecuted);
    }

}

Now we’re creating a second broadcast receiver to catch our explicit intent fired from the AlarmManager.

public class SchedulerEventReceiver extends BroadcastReceiver {
private static final String APP_TAG = "com.hascode.android";

@Override
public void onReceive(final Context ctx, final Intent intent) {
    Log.d(APP_TAG, "SchedulerEventReceiver.onReceive() called");
    Intent eventService = new Intent(ctx, SchedulerEventService.class);
    ctx.startService(eventService);
}

}

Handle service like :

public class SchedulerEventService extends Service {
private static final String APP_TAG = "com.hascode.android.scheduler";

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

@Override
public int onStartCommand(final Intent intent, final int flags,
        final int startId) {
    Log.d(APP_TAG, "event received in service: " + new Date().toString());
    return Service.START_NOT_STICKY;
}

}

This can help you to handle periodically scheduled tasks.

Android
  • 1,420
  • 4
  • 13
  • 23