How to get outgoing call is answered in Android - android

I need a way to get the the status when a outgoing call is answered. However, in the OFFHOOK state I am also using to call for the outgoing call(ACTION_CALL). How can I add the awnsered state without overriding the outgoing call activity?
public class OutgoingBroadcastReceiver extends BroadcastReceiver {
private Intent mIntent;
private String phonenumber = null;
public static boolean wasRinging;
#Override
public void onReceive(Context context, Intent intent) {
mIntent = intent;
MyPhoneStateListener phoneListener = new MyPhoneStateListener(context);
TelephonyManager telephony = (TelephonyManager) context
.getSystemService(Context.TELEPHONY_SERVICE);
telephony.listen(phoneListener, PhoneStateListener.LISTEN_CALL_STATE);
phonenumber = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);
}
public class MyPhoneStateListener extends PhoneStateListener {
private final Context context;
public MyPhoneStateListener(Context context) {
this.context = context;
}
#Override
public void onCallStateChanged(int state, String incomingNumber) {
switch (state) {
case TelephonyManager.CALL_STATE_IDLE:
wasRinging = true;
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
Log.e("%%%%%%%%%%%%%%%%%%%%%%%%%%%t", "OFFHOOK");
if (UIUDialer.isOutgoingCall() == true) {
//Do my work when outgoing call is detected
}
else if (!wasRinging)
{
Log.e("%%%%%%%%%%%%%%%%%%%%%%%%%%%t", "WASRINGING");
//Do my work when outgoing call is awnsered
}
context.sendBroadcast(new Intent("finish_incoming"));
wasRinging = true;
break;
case TelephonyManager.CALL_STATE_RINGING:
wasRinging = true;
break;
}
}
}
}

There is no public API available for this.

Why don't you just use the boolean wasRinging?
(you'll have to make it static and remove the initialization, though)

Related

Missed call detection in android

I want to develop a missed call detector .Which notify through broadcast reciver I found the code from the following link
public abstract class PhoneCallReceiver extends BroadcastReceiver {
static CallStartEndDetector listener;
#Override
public void onReceive(Context context, Intent intent) {
savedContext = context;
if(listener == null){
listener = new CallStartEndDetector();
}
TelephonyManager telephony = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
telephony.listen(listener, PhoneStateListener.LISTEN_CALL_STATE);
}
public class CallStartEndDetector extends PhoneStateListener {
int lastState = TelephonyManager.CALL_STATE_IDLE;
boolean isIncoming;
public PhonecallStartEndDetector() {}
//Incoming call- IDLE to RINGING when it rings, to OFFHOOK when it's answered, to IDLE when hung up
//Outgoing call- from IDLE to OFFHOOK when dialed out, to IDLE when hunged up
#Override
public void onCallStateChanged(int state, String incomingNumber) {
super.onCallStateChanged(state, incomingNumber);
if(lastState == state){
//No change
return;
}
switch (state) {
case TelephonyManager.CALL_STATE_RINGING:
isIncoming = true;
//incoming call started
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
//Transition of ringing->offhook are pickups of incoming calls. Nothing down on them
if(lastState != TelephonyManager.CALL_STATE_RINGING){
isIncoming = false;
//outgoing call started
}
break;
case TelephonyManager.CALL_STATE_IDLE:
//End of call(Idle). The type depends on the previous state(s)
if(lastState == TelephonyManager.CALL_STATE_RINGING){
//toast here for missed call
}
else if(isIncoming){
//incoming call ended
}
else{
//outgoing call ended
}
break;
}
lastState = state;
}
}
}
The above code woking fine .But when i got the missed call from same no it repeat the missed call detection . How i will got the toast only once please help
Hi everyone I have got the solution now its working fine with missed call.
Here my Class code MyCallListener extended by BroadcastReciever
private static boolean ring=false;
private static boolean callReceived=false;
private String callerPhoneNumber;
private Context saveContext;
#Override
public void onReceive(Context mContext, Intent intent)
{
saveContext=mContext;
// Get the current Phone State
String state = intent.getStringExtra(TelephonyManager.EXTRA_STATE);
if(state==null){
return;
}
Bundle bundle = intent.getExtras();
number= bundle.getString("incoming_number");
Calendar calendar=Calendar.getInstance();
currentTimeStamp=calendar.getTimeInMillis();
// If phone state "Rininging"
if(state.equals(TelephonyManager.EXTRA_STATE_RINGING))
{
ring =true;
// Get the Caller's Phone Number
}
// If incoming call is received
if(state.equals(TelephonyManager.EXTRA_STATE_OFFHOOK))
{
callReceived=true;
}
// If phone is Idle
if (state.equals(TelephonyManager.EXTRA_STATE_IDLE))
{
// If phone was ringing(ring=true) and not received(callReceived=false) , then it is a missed call
if(ring==true&&callReceived==false)
{
Toast.makeText(mContext, "missed call : "+number, Toast.LENGTH_LONG).show();
//workingWithFunctions();
ring=false;
}
callReceived=false;
}}
dont forgot to define receiver in manifest file
<receiver android:name="com.abc.broadcastrecivers.MyBroadcastReciver" />
in the application tag
public class callServiceReceiver extends BroadcastReceiver
{
TelephonyManager telephonyManager;
#Override
public void onReceive(Context context, Intent intent)
{
phoneStateListener phoneListener = new phoneStateListener(context);
telephonyManager = (TelephonyManager) context
.getSystemService(Context.TELEPHONY_SERVICE);
telephonyManager.listen(phoneListener, PhoneStateListener.LISTEN_CALL_STATE);
}`enter code here`
public void onDestroy() {
telephonyManager.listen(null, PhoneStateListener.LISTEN_NONE);
}
}
public class phoneStateListener extends PhoneStateListener
{
private Context mContext;
private static boolean ringing =false;
private static boolean call_received =false;
//Constructor
public phoneStateListener(Context context)
{
mContext = context;
}
#Override
public void onCallStateChanged(int state, String incomingNumber)
{
super.onCallStateChanged(state, incomingNumber);
try
{
switch (state)
{
case TelephonyManager.CALL_STATE_RINGING:
ringing = true;
// Get the Caller's Phone Number
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
call_received =true;
break;
case TelephonyManager.CALL_STATE_IDLE:
// If phone was ringing(ringing=true) and not received(call_received=false) , then it is a missed call
if(ringing ==true&& call_received ==false)
{
Toast.makeText(mContext, "missed call : "+incomingNumber, Toast.LENGTH_LONG).show();
ringing =false;
}
call_received =false;
break;
}
}catch (Exception e)
{
e.printStackTrace(System.err);
}
}
}
<receiver android:name=".callServiceReceiver">
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE" />
</intent-filter>
</receiver>

How to get the call end/ hanged event?

I have used TelephonyManager.CALL_STATE_OFFHOOK but unfortunately it gets called every time I touch the call button, i.e before the call is actually made.
my code:
public void onReceive(Context context, Intent intent) {
this.context = context;
listener = new CallStateListener();
if(intent.getAction().equals("android.intent.action.NEW_OUTGOING_CALL")){
Log.e("Aditya", "Broadcast listner");
//Currently no use
}
TelephonyManager tm = (TelephonyManager) context.getSystemService(context.TELEPHONY_SERVICE);
tm.listen(listener,PhoneStateListener.LISTEN_CALL_STATE);
}
public class CallStateListener extends PhoneStateListener {
int lastState = TelephonyManager.CALL_STATE_IDLE;
#Override
public void onCallStateChanged(int state, String incomingNumber) {
switch (state) {
case TelephonyManager.CALL_STATE_RINGING:
// called when someone is ringing to this phone
Toast.makeText(context,"Pioneer Contacts+ Updated",Toast.LENGTH_LONG).show();
Log.e("Aditya", "ringing");
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
Toast.makeText(context,"Pioneer Contacts+ Updated",Toast.LENGTH_LONG).show();
Log.e("Aditya", "offhook");
break;
}
}
}
The Log Log.e("Aditya", "offhook"); gets printed before the call gets connected. I want to appear it after the call is disconnected.

Detect Outgoing call has started and ended

I want to detect when the outgoing call has started and ended, and want to start an service when call has started and end the service when call has ended.
Here's the code i got from another question, but don't know where to put the start and stop activity to make it work.
public class CallListener {
private CallStateListener callStateListener = null;
private OutgoingReceiver outgoingReceiver = null;
public CallListener(Context context) {
this.context = context;
callStateListener = new CallStateListener();
outgoingReceiver = new OutgoingReceiver();
}
private class CallStateListener extends PhoneStateListener {
#Override
public void onCallStateChanged(int state, String incomingNumber) {
switch (state) {
case TelephonyManager.CALL_STATE_IDLE:
doSomething1();
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
doSomething2();
break;
case TelephonyManager.CALL_STATE_RINGING:
doSomething3();
break;
}
}
}
public class OutgoingReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
doSomething4();
}
}
public void start() {
telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
telephonyManager.listen(callStateListener, PhoneStateListener.LISTEN_CALL_STATE);
IntentFilter intentFilter = new IntentFilter(Intent.ACTION_NEW_OUTGOING_CALL);
context.registerReceiver(outgoingReceiver, intentFilter);
}
public void stop() {
telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
telephonyManager.listen(callStateListener, PhoneStateListener.LISTEN_NONE);
context.unregisterReceiver(outgoingReceiver);
}}
And here's to add in the service to make it work:
public class MyService extends Service {
private CallListener call = null;
public MyService() {
}
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
call = new CallListener(getApplicationContext());
call.start();
return(START_STICKY);
}
#Override
public void onDestroy() {
call.stop();
call.onDestroy();
}
#Override
public IBinder onBind(Intent intent) {
return null;
}
}
Thanks so much for any help, i may know that this is easy but i just don't know.
Thanks and have a really good day..!! :)
I had already searched how to detect call events like ringing / idle / etc... no success :(
Unfortunately, there is no solution in Android at this time...
This is well explained in this previous question/summary post:
Detecting outgoing call answered on Android

How can I find out when on android 2.3+ a phone call starts and stops?

I need my app to find out when a phone call is taking place.
It should work on when another phone is being called and also when a call is answered. I just need my app to get notified of exactly when the connection starts and when it stops (not the dialing, ringing, etc.).
Is this somehow possible?
You can use broadcast class to achieve this as below:
public class PhoneStateBroadcastReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
TelephonyManager telephonyManager = (TelephonyManager) context
.getSystemService(Context.TELEPHONY_SERVICE);
telephonyManager.listen(new CustomPhoneStateListener(context),
PhoneStateListener.LISTEN_CALL_STATE);
}
}
class CustomPhoneStateListener extends PhoneStateListener {
// private static final String TAG = "PhoneStateChanged";
Context context; // Context to make Toast if required
public CustomPhoneStateListener(Context context) {
super();
this.context = context;
}
#Override
public void onCallStateChanged(int state, String incomingNumber) {
super.onCallStateChanged(state, incomingNumber);
switch (state) {
case TelephonyManager.CALL_STATE_IDLE:
//WHEN PHONE WILL BE IDLE
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
// when Off hook i.e in call
break;
case TelephonyManager.CALL_STATE_RINGING:
// when Ringing
break;
default:
break;
}
}
}
Hope this will help you.

Broadcastreceiver creating multiple instances of TelephonyManager

This is my BroadcastReceiver
public class PlayAudio extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
tm.listen(new CustomPhoneStateListener(context), PhoneStateListener.LISTEN_CALL_STATE);
}
}
This is my Custom PhoneStateListener Class
public class CustomPhoneStateListener extends PhoneStateListener {
Context context;
public CustomPhoneStateListener(Context context) {
super();
this.context = context;
}
#Override
public void onCallStateChanged(int state, String incomingNumber) {
super.onCallStateChanged(state, incomingNumber);
switch (state) {
case TelephonyManager.CALL_STATE_IDLE:
Log.d("PHONEA", "IDLE");
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
Log.d("PHONEA", "OFFHOOK");
break;
case TelephonyManager.CALL_STATE_RINGING:
Log.d("PHONEA", "RINGING");
Intent intent = new Intent(this.context, AudioService.class);
context.startService(intent);
break;
default:
break;
}
}
}
and this is my service
public class AudioService extends Service{
private static final String TAG = "PHONEA";
MediaPlayer player;
#Override
public IBinder onBind(Intent intent) {
return null;
}
#Override
public void onCreate() {
Log.d(TAG, "onCreate -> TODO");
}
}
My question is that every time I receive data in Broadcast receiver I create a new instance of the TelephonyManager. So when I view the logcat the first time I get "RINGING", the second time "RINGING" "RINGING" and so on. When should I create my telephonylistener in order to have only one instance?
Regards,
Nicos
You are getting call on your receiver(Asssuming you are listening for PhoneState) every time the phone state change.
You should put some check in your receiver and instatiate the TelephonyManager for the first time only.

Categories

Resources