Search code examples
androidalarmmanagerandroid-alarms

How run the Alarm without open the Application


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


Solution

  • 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);
    
    }