Reputation: 13511
I need to create a BroadcastReceiver
that listens for when the the phone receives or makes a call, so I can take note of when it started and when it ended. I realized that the two receivers will look almost the same so instead of creating two separate BroadcastReceiver
s for incoming and for outgoing calls, I can just create one for both and make my actions depend on what event was fired.
I registered intent-filter
s for android.intent.action.PHONE_STATE
and android.intent.action.NEW_OUTGOING_CALL
in the manifest, but how do I find out from onReceive()
what kind of Intent
was fired?
@Override
public void onReceive(Context context, Intent intent) {
Bundle extras = intent.getExtras();
// get the intent fired--incoming or outgoing call?
// then, save it in a variable and perform corresponding actions
}
Upvotes: 1
Views: 3856
Reputation: 1577
You can also do one thing is to put one string parameter to bundle at the time of setting pending intent, that makes possible to know from which intent OnReceive calls.Also can use " intent.getAction() " Hope it helps :)
Upvotes: 0
Reputation: 25858
just use intent.getAction();
String action=intent.getAction();
if(action.equalsIgnoreCase(Intent.ACTION_NEW_OUTGOING_CALL)){
//dosomething here
}
else if(action.equalsIgnoreCase(second action)){
//do something here
}
Upvotes: 7