2016-02-26 23 views
0

大家好,我正在嘗試學習如何在Android中使用AlarmManagerBroadcastReceiver。 我在AlarmManager: 有一些問題我在1分鐘的距離設置了兩個鬧鐘,但只有一個火災,並且它遲了幾分鐘(我猜不可預測)。AlarmManager火遲了或根本不開火

這是我的主要活動(我通過點擊一個按鈕設置報警)

public class MainActivity extends AppCompatActivity { 
    AlarmManager alarmManager; 
    Intent intent; 
    PendingIntent pendingIntent; 
    AtomicInteger atomicInteger; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE); 
     intent = new Intent(Constants.EXTENDED_DATA_STATUS); 
     atomicInteger = new AtomicInteger(); 
     setContentView(R.layout.activity_main); 

     Button startButton = (Button) findViewById(R.id.start_button); 

     startButton.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       int id = atomicInteger.incrementAndGet(); 
       pendingIntent = PendingIntent.getBroadcast(getApplicationContext(),id,intent,0); 
       Calendar firstLullo = Calendar.getInstance(); 
       Calendar secondLullo = Calendar.getInstance(); 
       firstLullo.set(Calendar.HOUR_OF_DAY,10); 
       firstLullo.set(Calendar.MINUTE,55); 
       secondLullo.set(Calendar.HOUR_OF_DAY,10); 
       secondLullo.set(Calendar.MINUTE,56); 
       if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { 
        alarmManager.setExact(AlarmManager.RTC_WAKEUP,firstLullo.getTimeInMillis(),pendingIntent); 
        alarmManager.setExact(AlarmManager.RTC_WAKEUP,secondLullo.getTimeInMillis(),pendingIntent); 
       } 
       Log.d("ALARM","settato con id " + String.valueOf(id)); 
      } 
     }); 
    } 
} 

我的收款人只顯示一個麪包和讓手機震動。 Constants.EXTENDED_DATA_STATUS是一個Constants類的字符串,它被添加到Android Manifest中我的Reveiver的intent-filter

我在想什麼?我用Google搜索了很多,只發現我必須使用setExact(),但沒有運氣..

在此先感謝

+0

能否請您發表您的'的onReceive()'你報警的一部分? – Strider

回答

1

是的,當然上面的代碼只會觸發一個鬧鐘,因爲您爲兩個鬧鐘設置了相同的ID。

在pendingintent中通過它們的id來識別和區分報警。

使用不同的ID爲不同的警報

更新代碼:

pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 1234 ,intent,0); 
alarmManager.setExact(AlarmManager.RTC_WAKEUP,firstLullo.getTimeInMillis(),pendingIntent); 

pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 5678, intent,0); 
alarmManager.setExact(AlarmManager.RTC_WAKEUP,secondLullo.getTimeInMillis(),pendingIntent); 
+0

仍然一樣** pendingintent ** – Strider

+0

@Strider檢查id,pendingintent值是否變化, –

+0

如何在不知情的情況下downvote,請upvote –

4

之所以只有一個火災是因爲你的取消您的第一個總是

來自文檔:如果已經爲同一個IntentSender安排了報警,則先前的報警將首先被取消。

要解決這個問題,請對兩個警報使用不同的PendingIntents

-爲了讓你的接收機火多個時間,重新安排它在你的onReceive()

例子: 只有ü要分開兩個接收器從海誓山盟!在

//Declare AlarmManager 
AlarmManager am = (AlarmManager) LayoutActivity.this.getSystemService(ALARM_SERVICE); 

//create new calendar instance for your first alarm 
Calendar startTime= Calendar.getInstance(); 

//set the time of your first alarm 
firstLullo.set(Calendar.HOUR_OF_DAY,10); 
firstLullo.set(Calendar.MINUTE, 55); 
firstLullo.set(Calendar.SECOND, 0); 

//create a pending intent 
PendingIntent firstPI = PendingIntent.getBroadcast(yourActivity.this, 0, new Intent("yourFirstAlarmReceiver"), PendingIntent.FLAG_UPDATE_CURRENT); 

//schedule time for pending intent, and set the interval to day so that this event will repeat at the selected time every day 
am.setRepeating(AlarmManager.RTC_WAKEUP, firstAlarm.getTimeInMillis(), firstPI); 

//---------------------------------------------------- 

//create new calendar instance for your second alarm 
Calendar endCalender = Calendar.getInstance(); 

//Set the time alarm of your second alarm 
secondLullo.set(Calendar.HOUR_OF_DAY,10); 
secondLullo.set(Calendar.MINUTE,56); 
secondLullo.set(Calendar.SECOND, 0); 

//create a pending intent to be 
PendingIntent secondPI= PendingIntent.getBroadcast(yourActivity.this, 0, new Intent("yourSecondAlarmReceiver"), PendingIntent.FLAG_UPDATE_CURRENT); 

//schedule time for pending intent, and set the interval to day so that this event will repeat at the selected time every day 
am.setRepeating(AlarmManager.RTC_WAKEUP, secondLullo.getTimeInMillis(), secondPI); 

註冊報警的Manifest.xml

<receiver android:name="firstAlarm" > 
    <intent-filter> 
     <action android:name="yourFirstAlarmReceiver" > 
     </action> 
    </intent-filter> 
</receiver> 

<receiver android:name="secondAlarm" > 
    <intent-filter> 
      <action android:name="yourSecondAlarmReceiver" > 
      </action> 
    </intent-filter> 
</receiver> 

現在ü可以叫你報警,包括:

首先報警:

public class yourFirstAlarmReceiver extends BroadcastReceiver { 

    @Override 
    public void onReceive(Context context, Intent intent) { 

    //Do something when first alarm goes off 

    } 
} 

第二個報警:

public class yourSecondAlarmReceiverextends BroadcastReceiver { 

    @Override 
    public void onReceive(Context context, Intent intent) { 

    //Do something when second alarm goes off 

    } 
} 

應該幫助你出來。

+0

不同的PendingIntents和pendingintent具有不同的值有什麼區別。當價值改變時,它就變得不同了。 –

+0

爲什麼我應該創建不同的接收器?我的意思是,如果待處理的意圖具有不同的ID,他們爲什麼被認爲是相同的? 如果我想發出n次報警,應該創建n個接收器嗎?這對我來說看起來不太好:/ – magicleon

+0

如果你想一次使用多個警報接收器,那麼這是一個**示例**,如果你希望**一個**接收器多次觸發,則不需要使用它,比你應該在你的'onReceive()'中重新安排你的鬧鐘。我只寫了這個原因,你說:'我設置了兩個鬧鐘' – Strider