How to know when call is made and connected? - android

hello im using a broadcast reciever to try to find out when a call is "made" and after the call is "made" also when the made call is "connected" i searched around and came up with this code but this works when recievng a call and making a call, all i really need is when the call is made and connected, any help in guiding me in this is appreciated, thank you for your time
receiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
TelephonyManager telephonyManager = (TelephonyManager) context
.getSystemService(Context.TELEPHONY_SERVICE);
if(action.equals(android.telephony.TelephonyManager.ACTION_PHONE_STATE_CHANGED)){
//action for phone state changed
Toast.makeText(context, "Receiver call status", Toast.LENGTH_SHORT).show();
Log.d("reciever","entered keep going");
if(telephonyManager.getDataState() == TelephonyManager.DATA_CONNECTED){
Toast.makeText(context, "Receiver call connected", Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(context, "Receiver call not connected", Toast.LENGTH_SHORT).show();
}
}
}
};
IntentFilter filter = new IntentFilter();
filter.addAction(android.telephony.TelephonyManager.ACTION_PHONE_STATE_CHANGED);
registerReceiver(receiver,filter);

Here you can find all states of call, just make handle this states, like i did:
public class PhoneState extends PhoneStateListener {
#Override
public void onCallStateChanged(int state, String incomingNumber) {
switch (state) {
case TelephonyManager.CALL_STATE_IDLE:
//Some Action
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
break;
case TelephonyManager.CALL_STATE_RINGING:
//Some Action
break;
//case TelephonyManager.ANOTHER_STATE: ...
}
}
}

Related

Ending a phone call in Android

After a phone call ends, I must open another Activity.
However, it doesn't catch the "call end signal".
I've used mTelManager in BroadcastReceiver and Service, but it doesn't work.
When Activity is opened by a Service or BroadcastReceiver, it shows the MainActivity and not the targeted one instead.
Source code:
public class MainService extends Service{
TelephonyManager mTelManager;
PhoneStateRead pListener;
String TAG = "Call State catch";
boolean callOutState = false; //calling state
#Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
#Override
public void onCreate() {
Log.i(TAG, "ServiceReceiver->onReceive();");
pListener = new PhoneStateRead();
mTelManager = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
mTelManager.listen(pListener, PhoneStateListener.LISTEN_CALL_STATE);
if(callOutState){ //if call end
Intent showRecord = new Intent(getBaseContext(), Call_Out_Record_Activity.class);
showRecord.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(showRecord);//open Call_Out_Record_Activity
}
}
public class PhoneStateRead extends PhoneStateListener {
private final String TAG = "Phone State Read";
#Override
public void onCallStateChanged(int state, String incomingNumber) {
switch (state) {
case TelephonyManager.CALL_STATE_IDLE:
Log.i(TAG,"MyPhoneStateListener->onCallStateChanged() -> CALL_STATE_IDLE "+incomingNumber);
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
Log.i(TAG,"MyPhoneStateListener->onCallStateChanged() -> CALL_STATE_OFFHOOK "+incomingNumber);
break;
case TelephonyManager.CALL_STATE_RINGING:
Log.i(TAG,"MyPhoneStateListener->onCallStateChanged() -> CALL_STATE_RINGING "+incomingNumber);
break;
default:
Log.i(TAG,"MyPhoneStateListener->onCallStateChanged() -> default -> "+Integer.toString(state));
break;
}
CatchCallState(state);
}
}
private void CatchCallState(int state){
if(state == TelephonyManager.CALL_STATE_OFFHOOK || state == TelephonyManager.CALL_STATE_RINGING)
callOutState = false;
else if(state == TelephonyManager.CALL_STATE_IDLE)
callOutState = true;
}
}
So it looks like you have a couple problems here. First, you are trying to start your activity when you start your Service - you should do this code:
if(callOutState){ //if call end
Intent showRecord = new Intent(getBaseContext(), Call_Out_Record_Activity.class);
showRecord.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(showRecord);//open Call_Out_Record_Activity
}
in the CallStateListener. Or just put it in your CatchCallState method.
Next, notice that there is not a "call end signal" - the phone will go from "OFF_HOOK" to "IDLE" so you need flags to track that. Also, notice that "RINGING" happens on incoming calls, but not outgoing calls. So you may want to display different messages depending on incoming or outgoing calls.

Is it possible to know using TelephonyManager (onCallStateChanged) when the other person answers the call?

I am developing an application that starts an alarm (Ringtone) when I start a call and it also set speaker mode on. So I would like to stop the alarm when the person who I am calling answers the call. How could I achieve this?
This is my code:
private PhoneStateListener phoneCallListener = new PhoneStateListener() {
private int prevCallState=TelephonyManager.CALL_STATE_IDLE;
public void onCallStateChanged(int state, String incomingNumber) {
try {
AudioManager audiomanager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
switch (state) {
case TelephonyManager.CALL_STATE_OFFHOOK:
if (prevCallState == TelephonyManager.CALL_STATE_IDLE){
//Inicio de llamada saliente
audiomanager.setMode(AudioManager.MODE_IN_CALL);
audiomanager.setSpeakerphoneOn(true);
}
break;
case TelephonyManager.CALL_STATE_IDLE:
if (prevCallState == TelephonyManager.CALL_STATE_OFFHOOK){
//Fin de llamada saliente
audiomanager.setSpeakerphoneOn(false);
}
break;
}
} catch (Exception e) {
}
prevCallState = state;
}
};
I would appreciate any help. Thanxs a lot.
Android check when outgoing call is received by callee
hey check this link may try this it can helpful to you to sort out your issue.#Ravindra
I had similar problems, it is not possible to check if the person have picked up the phone nor his/her phone is not reachable. There is no feedback from the telephone companys in gsm networks.
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button) findViewById(R.id.buttonCall);
// add PhoneStateListener
PhoneCallListener phoneListener = new PhoneCallListener();
TelephonyManager telephonyManager = (TelephonyManager) this
.getSystemService(Context.TELEPHONY_SERVICE);
telephonyManager.listen(phoneListener,PhoneStateListener.LISTEN_CALL_STATE);
// add button listener
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent callIntent = new Intent(Intent.ACTION_CALL);
callIntent.setData(Uri.parse("tel:+9196203333"));
startActivity(callIntent);
}
});
}
//monitor phone call activities
private class PhoneCallListener extends PhoneStateListener {
private boolean isPhoneCalling = false;
String LOG_TAG = "LOGGING 123";
#Override
public void onCallStateChanged(int state, String incomingNumber) {
if (TelephonyManager.CALL_STATE_RINGING == state) {
// phone ringing
Log.i(LOG_TAG, "RINGING, number: " + incomingNumber);
}
if (TelephonyManager.CALL_STATE_OFFHOOK == state) {
// active
Log.i(LOG_TAG, "OFFHOOK");
isPhoneCalling = true;
}
if (TelephonyManager.CALL_STATE_IDLE == state) {
// run when class initial and phone call ended,
// need detect flag from CALL_STATE_OFFHOOK
Log.i(LOG_TAG, "IDLE");
if (isPhoneCalling) {
Log.i(LOG_TAG, "restart app");
// restart app
Intent i = getBaseContext().getPackageManager()
.getLaunchIntentForPackage(
getBaseContext().getPackageName());
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);
isPhoneCalling = false;
}
}
}
}
and also give permission in Manifest

onDataConnectionStateChanged state param is always 0

I am trying to use PhoneStateListener to handle connection state changes. I've registered the listener inside onCreate method of a service. The overriden method onDataConnectionStateChanged(int state) of the listener is being called when I enable or disable wifi connection from the phone' settings, but in both cases the 'state' param is 0 and it always enters the first case of the switch.
Here's my implementation of the method:
PhoneStateListener listener = new PhoneStateListener() {
#Override
public void onDataConnectionStateChanged(int state) {
super.onDataConnectionStateChanged(state);
switch (state) {
case TelephonyManager.DATA_DISCONNECTED:
Toast.makeText(LocationService.this, "Data connection lost!", Toast.LENGTH_LONG).show();
break;
case TelephonyManager.DATA_CONNECTED:
Toast.makeText(LocationService.this, "Data connection available!", Toast.LENGTH_LONG).show();
break;
}
}
};
The result is always a toast message saying: "Data connection lost!". I'm sure Im missing something simple here but I can't find out what the problem is.
Old question, but the code you show is for "data", not for Wifi.
I have the same code, and it's executed only when I connect with "LTE" (data)
private void registerDataListener(Context context) {
listener = new PhoneStateListener() {
#Override
public void onDataConnectionStateChanged(int state) {
switch (state) {
case TelephonyManager.DATA_DISCONNECTED:
Log.d(TAG, "Disconnected");
break;
case TelephonyManager.DATA_CONNECTED:
Log.d(TAG, "Connected");
break;
case TelephonyManager.DATA_CONNECTING:
Log.d(TAG, "Connecting");
break;
case TelephonyManager.DATA_SUSPENDED:
Log.d(TAG, "Disconnecting");
break;
}
}
};
telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
telephonyManager.listen(listener,PhoneStateListener.LISTEN_DATA_CONNECTION_STATE);
Log.d(TAG, "Registering PhoneStateListener");
}
And don't forget to add permission in your manifest
<uses-permission android:name="android.permission.READ_PHONE_STATE" />

Trouble with reading phone state

I want to perform some operation (Pause game) in my application when a call came. But reading the phone state is not working. I have given permission(READ_PHONE_STATE) in the manifest. Nothing is happen when a call came.
Thanks.
TelephonyManager telephonyManager;
PhoneStateListener listener;
telephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
*
*
*
listener = new PhoneStateListener() {
#Override
public void onCallStateChanged(int state, String incomingNumber) {
switch (state) {
case TelephonyManager.CALL_STATE_IDLE:
Toast.makeText(SudokuGameActivity.this, "IDLE", Toast.LENGTH_SHORT).show();
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
Toast.makeText(SudokuGameActivity.this, "OFF Hook", Toast.LENGTH_SHORT).show();
break;
case TelephonyManager.CALL_STATE_RINGING:
Toast.makeText(SudokuGameActivity.this, "Ringing", Toast.LENGTH_SHORT).show();
mpauseButton.performClick();
break;
}
}
};
Have you written the following line :
telephonyManager.listen(listener,PhoneStateListener.LISTEN_CALL_STATE);
when your listener has be created, you need invoke `public void listen (PhoneStateListener listener, int events)' to listen.
also, you can try this:
create a broadcatst receiver handle the action android.intent.action.PHONE_STATE,
code example:
public class PhoneStateReceiver extends BroadcastReceiver {
private TelephonyManager manager;
#Override
public void onReceive(Context context, Intent intent) {
if (manager == null) {
manager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
}
String action = intent.getAction();
System.out.println(action);
System.out.println("current phone state:" + manager.getCallState());
}
}

Broadcast receiver and phonestatelistener

I am trying to use BroadcastReceiver to receive incoming calls. I want to register call when the call is ended. Below code gives each state (idle,ringing n offhook) more than once with the same number (its not an issue of past call). Is it because phonestatelistner is called within the service.
public class IncomingCallReceiver extends BroadcastReceiver {
int stateString =0;
int prevStat = 0;
#Override
public void onReceive(Context context, Intent intent) {
Bundle bundle = intent.getExtras();
if(null == bundle)
return;
String state = bundle.getString(TelephonyManager.EXTRA_STATE);
if(state.equalsIgnoreCase(TelephonyManager.EXTRA_STATE_RINGING))
{
String phonenumber = bundle.getString(TelephonyManager.EXTRA_INCOMING_NUMBER);
String info = "Detect Calls sample application\nIncoming number: " + phonenumber;
Toast.makeText(context, info, Toast.LENGTH_LONG).show();
}
TelephonyManager telephony = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
//CustomPhoneStateListener customPhoneListener = new CustomPhoneStateListener();
PhoneStateListener listener = new PhoneStateListener() {
#Override
public void onCallStateChanged(int state, String incomingNumber) {
String p = incomingNumber;
switch (state) {
case TelephonyManager.CALL_STATE_IDLE:
prevStat = stateString;
stateString = 1;
Log.i("ON CALL STATE CHANGED","IDLE");
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
Log.i("ON CALL STATE CHANGED","OFFFHOOK");
prevStat = stateString;
stateString = 2;
break;
case TelephonyManager.CALL_STATE_RINGING:
prevStat = stateString;
stateString = 3;
Log.i("ON CALL STATE CHANGED","RINGING");
break;
}
if(prevStat == 2 && stateString == 1)
{
Log.i("REGISTER A CALLL","==" +p);
}
}
};
telephony.listen(listener, PhoneStateListener.LISTEN_CALL_STATE);
}
}
I also tried the same thing with phonestatelistener in a separate class, but it was not being called, i.e my code didn't reach there. In my manifest file I have registered receiver, but there is no declaration of phonestatelistener. Is that to be registered?
I am sorry if the solution is quite obvious but am new to android things.
Any help is appreciated :)
Yes, choose listener or received , not both of them

Categories

Resources