简体   繁体   English

Android-具有待定意图的多个警报,如何确定正在调用哪个意图?

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

I'm working with 2 separate alarms. 我正在使用2个单独的警报。 I have a method in my class which creates 2 different pending intents to start 2 alarms. 我的课堂上有一个方法可以创建2个不同的挂起意图来启动2个警报。 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 创建intent时,使用intent.putExtra("id", "intent x") ,然后

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM