Block incoming call instantly in Android - android

In my application I want to put a feature for blocking incoming calls.
For this I refereed this link
And its work perfectly but it takes nearly 2 seconds to end the call not instantly.
And at caller side a tune of The person your calling busy at the movement gets listen.
Is it possible to end the call without ringing?

Try this in your code.
#Override
public void onReceive(Context context, Intent intent)
{
String number = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);
setResultData(null); //It will terminate the call
Toast.makeText(context, "Call Terminated" + number, 5000).show();
}

Related

Does Android P broadcast incoming calls twice, or is it just me?

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?

How to pause outgoing call till verification in android

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.

How to get alert dialog only after disconnect call?

In my app i am doing one thing that,when the call is disconnected by caller or receiver,one alert dialog should appear with cellphone number.it all works fine but issue is alert dialog is also appearing when i am getting call,but i only want only after disconnection,i don't know what is mistake i am making following is my code..can any one help?
public class MyCallReceiver extends BroadcastReceiver {
private String incomingNumber;
#Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
if (intent.getStringExtra(TelephonyManager.EXTRA_STATE).equals(TelephonyManager.EXTRA_STATE_IDLE)) {
// This code will execute when the phone has an incoming call
// get the phone number
incomingNumber = intent.getStringExtra(TelephonyManager.EXTRA_STATE);
Intent i = new Intent(context, Disp_Alert_dialog.class);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
i.putExtra("Number", incomingNumber);
context.startActivity(i);
Toast.makeText(context, "Call from:" +incomingNumber, Toast.LENGTH_LONG).show();
/* String incomingNumber = intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER);
Toast.makeText(context, "Call from:" +incomingNumber, Toast.LENGTH_LONG).show();*/
} else if (intent.getStringExtra(TelephonyManager.EXTRA_STATE).equals(
TelephonyManager.EXTRA_STATE_IDLE)
|| intent.getStringExtra(TelephonyManager.EXTRA_STATE).equals(
TelephonyManager.EXTRA_STATE_OFFHOOK)) {
// This code will execute when the call is disconnected
}
}
You have written your code in wrong place. This code
Intent i = new Intent(context, Disp_Alert_dialog.class);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
i.putExtra("Number", incomingNumber);
context.startActivity(i);
will be in else..if condition instead of if condition.
You condition is wrong. You are using else block of
if (intent.getStringExtra(TelephonyManager.EXTRA_STATE).equals(TelephonyManager.EXTRA_STATE_RINGING)
It will run whenever call state changes to 'Ideal' or 'off hook' (in outgoing call - when dialed and call is disconnected, in incoming call - when call is picked up and call is disconnected).
If you want to show dialogue only while disconnecting call, use
if (intent.getStringExtra(TelephonyManager.EXTRA_STATE).equals(TelephonyManager.EXTRA_STATE_IDEAL)
and write code in if block
As for number, you cannot get number that way as it is only valid for ringing state.
A work around would be using a ContentObserver on CallLog. Its onChange() will be called whenever there is a change in call log. You can then take number from call log. But you will need to have a part of your application active for this. Use in a Service may be.
Note- In some devices message also comes in call log data. So check if last entry in call log in of call type or not before showing your dialogue
This may help you in using content observer

How to get exact outgoing call receiving time?

I am new to android.
I am implementing one application related to incoming and outgoing call details.
I am getting outgoing call and incoming call details by using brodcast receivers.
The problem is when the incoming call will come the brodcast receiver will rise,
And i make an outgoing call the brodcast receiver raised.
That is fine,but when i click the green button the outgoing call will start,but
i want exact time of answering the call from otherside.
How i get this?
If any one know please help me.
Thanks in advance.
Try using a TelephonyManager
if ((intent.getAction().equals(Intent.ACTION_NEW_OUTGOING_CALL) || (intent
.getAction()
.equals(TelephonyManager.ACTION_PHONE_STATE_CHANGED)))) {
String phoneState = intent.getExtras().getString(
TelephonyManager.EXTRA_STATE);
if (intent.getAction().equals(Intent.ACTION_NEW_OUTGOING_CALL)) {
Log.d(TAG, "Outgoing call");
if (TelephonyManager.EXTRA_STATE_OFFHOOK.equals(phoneState)) {
Log.d(TAG, "Incoming call/Outgng call Started");
}
if (TelephonyManager.EXTRA_STATE_IDLE.equals(phoneState)) {
Log.d(TAG,"Call ended");
}
if (TelephonyManager.EXTRA_STATE_RINGING.equals(phoneState)) {
Log.d(TAG,"Call ringing");
}
}
Continuously check when device state is changed from CALL_STATE_IDLE, to CALL_STATEOFFHOOK... It gives the exact time of your call being answered

Small confusion about PhoneStateIntentReceiver

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.

Categories

Resources