我有一个班级
public class Receiver extends BroadcastReceiver{
// with onReceive() method(overridden)
public void onReceive(Context context, Intent intent)
{...
}
}
在我的活动中,我正在使用Receiver类来收听广播。
//Block1
Intent intent = new Intent(Test.this, Receiver.class);
PendingIntent pIntent = PendingIntent.getBroadcast(Test.this, 0, intent, 0);
AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
am.set(AlarmManager.RTC_WAKEUP, 2000, pIntent);
//Block2
IntentFilter filter= new IntentFilter(Intent.ACTION_SCREEN_ON);
filter.addAction(Intent.ACTION_SCREEN_OFF);
mReceiver = new Receiver();
registerReceiver(mReceiver, filter);
现在,问题是我的活动中的块都使用相同的Receiver类进行广播。
我计划使用Intent.putExtra()
来检测调用onReceive()的意图,但它只适用于Intents而不适用于IntentFilter。
那么我如何区分onReceive()方法中对onReceive()的调用?