2017-07-02 51 views
1

我想讓兩個鬧鐘啓動他們自己的單獨服務。它們似乎互相干擾,因爲一次只有其中一個工作。有什麼我應該改變我的代碼?或者是不可能做到這一點?如何讓兩個警報啓動兩項服務?

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

回答

0

此代碼將安排兩個警報以在不同的時間啓動兩個單獨的服務。

從MainActivity的onCreate方法完成時起,警報設置爲60秒和120秒。 (注意:報警不一定會火正是 60和120秒後,因爲AlarmManager的不精確調度有時需要耐心數量驚人的查看報警熄滅)

你可以看到,這兩個服務的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); 
    } 

}