开发者

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

开发者 https://www.devze.com 2023-03-25 14:17 出处:网络
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

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


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();
0

精彩评论

暂无评论...
验证码 换一张
取 消