I managed to prepare an activity when the phone is ringing. Now I need to know how to cancel this activity when I anwser the phone or I reject the call.Do I call EXTRA_STATE_IDLE?EXTRA_STATE_OFFHOOK?
Any ideas?
<receiver android:name=".IncomingBroadcastReceiver">
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE"/>
</intent-filter>
</receiver>
public class IncomingBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
String state = intent.getStringExtra(TelephonyManager.EXTRA_STATE);
// If an incoming call arrives
if (state.equals(TelephonyManager.EXTRA_STATE_RINGING))
{ //Did my work }
below is a code of detecting outgoing call by accessibility events -
Add a class which extends
AccessibilityService
in your projects -But to get the function
event.getSource()
working you have to specify some of your service configuration through xml, so create a xml folder in your project and add a xml file called serviceconfig.xml (you can give any name you want.The content of serviceconfig is below -
You can find more about serviceconfig in Here
Now add your service in you Manifest file like this -
And youre done, just run the app and go to Accessibility settings in your phone, you will find an option named as detection (or whatever name you have given as your service description), switch that on to give accesibility permissions for you app.
Now you will see a toast when call is answered.
you can Code any code you want in there, also you can call a callback function in your activity
Most important - Dont call your call window(android dialer window) untill the call is answered, otherwise this will not work.
Note - As android doesn't provide any solution to detect if the call is answered or not, this is the best alternative i have made, hope it works for you.
in your onReceive:
separate class:
All you need to do is write some code to check if the previous state was 'ringing'. If the current state is idle and the previous state was ringing, they cancelled the call. If the current state is offhook and the previous state was ringing, they answered the call.
Following are the states which it goes through in different scenarios:
1) Answering Received call
2) Rejecting / Not Answering (Missed) Received call
3) Dialing call
Code
In your receiver
The above answer is completely wrong in case of outgoing calls. In Android there is no way by which one detect whether the call was actually answered (in case of outgoing calls). The moment you dial a number, the
off_hook
state is fired. This is one of the drawbacks of Android programming.