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.
Related
I am writing an application that gives a dialog(dialog which is an activity) for every international outgoing call. the application interrupts the outgoing call and gives an alert for all the international calls. On the users confirmation, a new call with the same number is placed.
My app is very similar to this one
Outgoing call don't start
However my broadcast receiver receives even the outgoing call i place from my application, this leads to an infinite loop. I am using the below code to disable the broadcast receiver after the call is placed from my app.
private void makeCall1(String number) {
PackageManager pm = mContext.getPackageManager();
ComponentName componentName = new ComponentName(mContext,OutgoingCallReceiver.class);
pm.setComponentEnabledSetting(componentName, PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP);
Intent callIntent = new Intent(Intent.ACTION_CALL, Uri.parse(number));
startActivity(callIntent);
// Now wait for the call to end somehow and afterwards ->
// pm.setComponentEnabledSetting(componentName, PackageManager.COMPONENT_ENABLED_STATE_ENABLED, PackageManager.DONT_KILL_APP);
}
How can i retrieve the call ended notification of the call that was placed by me so that i can write some code to enable the broadcast receiver for the future calls.
You need add a variable to control it...when the BroadCastReceiver is called first( i.e this would be when the user places call and not from your app)then set the variable to true...Now if true only then show the dialog and later set the variable to false when the call is disconnected.
Now how to know the call has ended?
You can know the call has disconnected using states of call.
These are the states when the call is placed-
CALL_STATE_OFFHOOK->CALL_STATE_IDLE
CALL_STATE_OFFHOOK--> Called when the call is placed
CALL_STATE_IDLE-----> Called when the call is disconnected
Now you want to know when the call has disconnected, you can set a variable to control it in your BroadCastReceiver
private static boolean isCalled=false;
................
................
#Override
public void onReceive(Context context, Intent intent) {
Bundle bundle = intent.getExtras();
String state = bundle.getString(TelephonyManager.EXTRA_STATE);
if (state.equals(TelephonyManager.EXTRA_STATE_OFFHOOK)) {
isCalled= true;
Log.v("ranjapp", "Within DIALED NUMBER");
} else if (state.equals(TelephonyManager.EXTRA_STATE_IDLE) && isCalled){
Log.v("ranjapp", "Within IDLE");
//ADD YOUR CODE FOR WHAT NEEDS TO BE DONE AT CALL DISCONNECT
isCalled=false;
}
.........................
.........................
Also dont forget to add the permission in manifest as below:
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
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
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();
}
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
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.