sandra burgle sandra burgle - 1 month ago 10
Android Question

How to have two alarms start two services?

I am trying to have two alarms start their own separate services. They seem to be interfering with each other though, because only one of them work at a time. Is there something I should change in my code? Or is it not possible to do this?

public void setAlarm(){
AlarmManager alarmMgr1 = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(this, DrawOverAppsService.class);
Float alpha2 = alpha.getProgress()/100f;
intent.putExtra("alpha", alpha2);

PendingIntent alarmIntent1 = PendingIntent.getService(MainActivity.this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
if(start.isChecked()) {
alarmMgr1.cancel(alarmIntent1);
java.util.Calendar calendar = java.util.Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(java.util.Calendar.HOUR_OF_DAY, 19);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
alarmMgr1.setRepeating(AlarmManager.RTC_WAKEUP,
calendar.getTimeInMillis(),
AlarmManager.INTERVAL_DAY, alarmIntent1);
Log.d("a","b");
}
if(start.isChecked() == false) {
alarmMgr1.cancel(alarmIntent1);
}
}
public void stopAlarm(){
AlarmManager alarmMgr = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Intent intent5 = new Intent(this, stopOverlay.class);
PendingIntent alarmIntent = PendingIntent.getService(MainActivity.this, 2, intent5, 0);
if(stop.isChecked()) {
alarmMgr.cancel(alarmIntent);
java.util.Calendar calendar = java.util.Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(java.util.Calendar.HOUR_OF_DAY, 5);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
alarmMgr.setRepeating(AlarmManager.RTC_WAKEUP,
calendar.getTimeInMillis(),
AlarmManager.INTERVAL_DAY, alarmIntent);
}
if(stop.isChecked() == false) {
alarmMgr.cancel(alarmIntent);
}
}

Answer Source

This code will schedule two alarms to start two separate services at different times.

The alarms are set for 60 and 120 seconds from the time the MainActivity's onCreate method completes. (Note: the alarm will not necessarily fire exactly 60 and 120 seconds later because of AlarmManager's inexact scheduling. Sometimes it takes a surprising amount of patience to see the alarms go off.)

You can see that the two service's onStart commands are being executed by watching the app's log output.

MainActivity.java

public class MainActivity extends AppCompatActivity {

    private static final String TAG = "MainActivity";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        setAlarm(Service1.class, 60);
        setAlarm(Service2.class, 120);

    }

    public void setAlarm(Class serviceClass, int delay){
        AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
        Intent intent = new Intent(this, serviceClass);
        PendingIntent alarmIntent = PendingIntent.getService(MainActivity.this,
                0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
        alarmManager.cancel(alarmIntent);
        java.util.Calendar calendar = java.util.Calendar.getInstance();
        calendar.add(Calendar.SECOND, delay);
        alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,
                calendar.getTimeInMillis(),
                AlarmManager.INTERVAL_DAY, alarmIntent);

        Log.d(TAG,"alarm set:" + serviceClass.getSimpleName());
    }
}

Service1.java

public class Service1 extends Service {

    private static final String TAG = "service1";

    @Override
    public IBinder onBind(Intent intent) {
        throw new UnsupportedOperationException("Not yet implemented");
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.v(TAG, "service started");
        return super.onStartCommand(intent, flags, startId);
    }

}

Service2.java

public class Service2 extends Service {

    private static final String TAG = "service2";

    @Override
    public IBinder onBind(Intent intent) {
        throw new UnsupportedOperationException("Not yet implemented");
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.v(TAG, "service started");
        return super.onStartCommand(intent, flags, startId);
    }

}