當我的應用程序啟動時,我想檢查一下之前設置的警報器是否也啟動。如果沒有啟動,如何設置?
這樣設置intent後
Intent intent = new Intent();
intent.setAction("com.vit.upload");
PendingIntent pIntent = PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_NO_CREATE);
但是啟動程序時,警報器還是不能啟動?請問這個問題出在哪裡?
你可以通過創建每一個唯一的IP來區分alarms
Intent intent = new Intent(this, AlarmReceiverActivity.class);
PendingIntent pi = PendingIntent.getActivity(this,UNIQUE_ID_GOES_HERE, intent, 0);
AlarmManager am = (AlarmManager)getSystemService(Activity.ALARM_SERVICE);
am.set(AlarmManager.RTC_WAKEUP, triggerAtMillis ,pi);
當要訪問這個alarm,需要在相同的ID中創建相同的PendingIntent。例如下面這個例子是只訪問 PendingIntent id 是1234的alarm,然後他會取消之前的alarm重置。
Intent intent = new Intent(this, AlarmReceiverActivity.class);
PendingIntent pi = PendingIntent.getBroadcast(this, 1234, intent, 0);
AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
am.set(AlarmManager.RTC_WAKEUP, triggerAtMillis ,pi);