Garbit
Garbit

Reputation: 6036

Android - Multiple alarms w/ pending intents, how to determine which intent is being called?

I'm working with 2 separate alarms. I have a method in my class which creates 2 different pending intents to start 2 alarms. This method calls the broadcast method when complete however depending on which intent it receives i need to perform different actions

thanks in advance,

Andy

prompt class

public void setSleepPrompts(Context context){
    try{
        Intent intent = new Intent(context, SleepPromptReceiver.class ); 
        PendingIntent firstSender = PendingIntent.getBroadcast(context, 1, intent, 0);
        PendingIntent secondSender = PendingIntent.getBroadcast(context, 2, intent, 0);

        AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
        am.set(AlarmManager.RTC_WAKEUP, (this.getBedTimeEpoch() - this.firstPromptOffset), firstSender);
        am.set(AlarmManager.RTC_WAKEUP, (this.getBedTimeEpoch() - this.secondPromptOffset), secondSender);
    } catch (Exception e){
        Log.i(TAG, e.toString());
    }
}

Broadcast receiver class

@Override
public void onReceive(Context context, Intent intent) {
    try{
        if(intent.GET_THE_INTENT_ID?)
        Toast.makeText(context, "kapow chow", Toast.LENGTH_SHORT).show();
    } catch (Exception e){
        Toast.makeText(context, e.toString(), Toast.LENGTH_SHORT).show();
    }
}

Upvotes: 0

Views: 1302

Answers (1)

Marmoy
Marmoy

Reputation: 8079

Use intent.putExtra("id", "intent x") when you create the intent, and then

if(intent.getStringExtra("id").equals("intent x"))
        Toast.makeText(context, "kapow chow", Toast.LENGTH_SHORT).show();

Upvotes: 1

Related Questions