2015-05-14 113 views
2

Iv'e使用Intent和BroadcastReciever創建了多個警報,並將它們放置在Intent數組中。 對於Intent.PutExtra(「info」,string);稍後將顯示爲Toast,當警報激活時, 並給每個不同的requestCode。 但是,當添加多個警報時,Toast也會顯示每個其他警報的信息。 MainActivity:獲取Android警報信息

Intent newAlarmIntent = new Intent(this,AlarmReceiver.class); 
     newAlarmIntent.putExtra("info",editText.getText().toString()); 
     alarmsArray[alarmCounter]=newAlarmIntent; 
     alarm.AlarmListSortAndSetNext(gameArrayList, alarmArray, this,alarmCounter,alarmsArray[alarmCounter]); 

報警類別:

public void CreateNew (Context context, Long alarmTimeAsLong, int counter, Intent intent) 
{ 
PendingIntent pendingIntent; 
pendingIntent = PendingIntent.getBroadcast(context, counter, intent, PendingIntent.FLAG_UPDATE_CURRENT); 
AlarmManager manager; 
manager = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE); 
manager.set(AlarmManager.RTC_WAKEUP, (alarmTimeAsLong),pendingIntent); 
Toast.makeText(context, "Alarm Set", Toast.LENGTH_SHORT).show(); 
} 

public void AlarmListSortAndSetNext (ArrayList<Game> gameArrayList,Long[] alarmArray,Context context,int alarmCounter, Intent intent) 
{ 
    Long SystemTimeAsLong = System.currentTimeMillis(); 
    //Sorting Long Array for NEXT ALARM 
    for (int i=0;i<10;i++) 
    {if(i<gameArrayList.size()){ 
      alarmArray[i] = gameArrayList.get(i).getDateAndTimeAsLong(); 
     } 
     else alarmArray[i]= 0L; 
     } 
    Arrays.sort(alarmArray); 
    //Setting next ALARM by Long Size 
    for(int i=0;i<10;i++) 
    { 
     if (alarmArray[i]>SystemTimeAsLong){ 
      CreateNew(context,alarmArray[i],alarmCounter,intent); 
      alarmCounter++;} 
    }} 

AlarmReceiver:

public class AlarmReceiver extends BroadcastReceiver{ 
@Override 
public void onReceive(Context context, Intent intent) 
{ 
    PowerManager pm = (PowerManager) context.getApplicationContext().getSystemService(Context.POWER_SERVICE); 
    PowerManager.WakeLock wakeLock = pm.newWakeLock((PowerManager.SCREEN_BRIGHT_WAKE_LOCK | PowerManager.FULL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP), "TAG"); 
    wakeLock.acquire(); 
    String gameInfo= intent.getStringExtra("info"); 
    Toast.makeText(context, gameInfo, Toast.LENGTH_SHORT).show(); 
    MediaPlayer mp = MediaPlayer.create(context, R.raw.bipbip); 
    mp.start(); 
    wakeLock.release();}} 

問題:怎樣才能讓每一個意向有它的 「信息」/ PutExtra,或其他任何告訴哪一個被激活的方法?

回答

0

*發現問題* 我已經離開最初本來是要管理一個報警的AlarmListSortAndSetNext方法。由於我創建了多個Intents,因此我不再需要使用此方法。循環中的東西可能創建了多個putExtra(); 謝謝。