如何让两个警报启动两个服务?

时间:2017-07-02 17:23:37

标签: android alarmmanager

我正在尝试让两个警报启动它们各自的服务。他们似乎互相干扰,因为他们中只有一个一次工作。我的代码中是否应该更改一些内容?或者是不可能这样做?

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

1 个答案:

答案 0 :(得分:0)

此代码将安排两个警报,以在不同时间启动两个单独的服务。

从MainActivity的onCreate方法完成开始,闹钟设置为60秒和120秒。 (注意:由于AlarmManager的调度不准确,警报不一定会在60秒和120秒之后完全 。有时需要花费大量的耐心才能看到警报响起。)

通过观看应用程序的日志输出,您可以看到正在执行两个服务的onStart命令。

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

}