public class MyReceiver extends PhoneStateIntentReceiver {
#Override
public void onReceiveIntent(Context context, Intent intent) {
if (intent.action == Intent.CALL_ACTION) {
}
}
}
Assume that notifyPhoneCallState has been called to enable MyReceiver to receive notifications about phone call states, in which case the code will get executed?
when device receives an incoming call
when outgoing call is initiated on the device
when the user presses the call button
incoming phone call is terminated
or will the code not be executed at all?
Did you mean public static final String ACTION_CALL instead of CALL_ACTION?
Activity Action: Perform a call to someone specified by the data.
Input: If nothing, an empty dialer is started; else getData() is URI of a phone number to be dialed or a tel: URI of an explicit phone number.
Output: nothing.
Note: there will be restrictions on which applications can initiate a call; most applications should use the ACTION_DIAL.
Note: this Intent cannot be used to call emergency numbers. Applications can dial emergency numbers using ACTION_DIAL, however.
Related
I have an app that checks the phone number of an incoming call against a blacklist.
I have used the below code for several versions of Android to get the phone number of an incoming call, but when I test it against Android P, it behaves unexpectedly.
For readability, I have removed all null checks from the code below.
public class IncomingCallHandler extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
String state = intent.getExtras().getString(TelephonyManager.EXTRA_STATE);
if (state.equalsIgnoreCase(TelephonyManager.EXTRA_STATE_RINGING)) {
String phoneNumber = bundle
.getString(TelephonyManager.EXTRA_INCOMING_NUMBER);
}
}
}
On Android versions less than P, onReceive with state EXTRA_STATE_RINGING may be called several times during an incoming call, but phoneNumber always has the same value (the actual incoming phone number).
On Android P, onReceive is called twice during an incoming call. The first time phoneNumber=null, the second time it is the actual phone number.
Is this a bug? Is it supposed to be like this? Do you get the same in your apps?
I want a service to monitor the calls I dial, and to perform a specific task parallel to a specific call.
Example. I call 123 from the dialer and a specific task gets triggered at that time.
But if I dial some other number then nothing should happen.
How can I implement such a thing?
You can use BroadcastReceiver for ACTION_NEW_OUTGOING_CALL
public class OutgoingBroadcastReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
if (intent. getAction (). equals (Intent. ACTION_NEW_OUTGOING_CALL)) {
String number=intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);
Log.e("Number=", number);
if (number.equals("123")) {
// do your magic here
}
}
}
And don't forget and using permission:
<uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS" />
I need to pause the outgoing call till I enter correct PIN. Only authorized user can make call . I have used BroadcastReceiver for detecting the outgoing call.
This is my code,
public void onReceive(Context context, Intent intent) {
String number = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER).toString();
//if u want to block particular call then check number or else block all call in thid code
if (worngPIN){
setResultData(null);//Canceling call operation
Toast.makeText(context, "This call is not Allowed", Toast.LENGTH_LONG).show();
}
}
I'm able to see the Toast message but call is connecting.
I tested in version 5.1.
I read this post: Broadcast receiver onReceive() getting called multiple times
But I didn't find the needed answer.
I've created a small utility function that overrides the onReceive() of BroadcastReceiver and rejects an incoming call based on certain conditions (which works fine).
Now once all the conditions are matched and I reject the call, I would like to store that number in the database (which again is an easy task). I would like to save the number in the database once I reject the call. But in the logs I observe that once there is an incoming call, the onReceive function gets called multiple times. If this is the case, I do not want multiple entries in my DB.
Is there any way by which the onReceive() would be called only once ? Or any workaround ?
Thanks for any help
Your receiver will get called for three different state.on ringing, on hook and on idle state.
Check the phone state in onReceive.You may want to cut the call and store in db if its state is ringing.
public void onReceive(final Context context, Intent intent) {
Bundle bundle = intent.getExtras();
if (null == bundle) {
return;
}
String state = bundle.getString(TelephonyManager.EXTRA_STATE);
if (TelephonyManager.EXTRA_STATE_RINGING.equalsIgnoreCase(state)) {
//cut the call and store in db
return;
}
}
if(TelephonyManager.EXTRA_STATE_IDLE.equalsIgnoreCase(state)) {
return;
}
if(TelephonyManager.EXTRA_STATE_OFFHOOK.equalsIgnoreCase(state)){
return;
}
}
I want to track the phone call information after dialing the call button and before ringing.Means before going call to the person whom I want to call, I want to a notification in my application.
public class CallReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
// Try to read the phone number from previous receivers.
String phoneNumber = getResultData();
if (phoneNumber == null) {
// We could not find any previous data. Use the original phone number in this case.
phoneNumber = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);
}
}
}
You can retrieve this from a Broadcast in response to ACTION_NEW_OUTGOING_CALL:
Register a BroadcastReceiver with an ACTION_NEW_OUTGOING_CALL intent filter. You can also read this Android Developers blog post regarding some details on the subject.