CALL_STATE_OFFHOOK called two times - android

I am working on an app which determine a call is incoming or outgoing call. Not understanding why CALL_STATE_OFFHOOK is calling two times during a call.Any help please
switch (state) {
case TelephonyManager.CALL_STATE_IDLE:
startedCall=false;
log.info("state_idle");
// log.info("onCallStateChanged CALL_STATE_IDLE");
/* Intent idle = new Intent(context, BackgroundServices.class);
context.stopService(idle);*/
// Toast.makeText(context, "Phone state Idle", Toast.LENGTH_LONG).show();
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
log.info("onCallStateChanged CALL_STATE_OFFHOOK");
if(!startedCall){
log.info("it is a outgoing call");
}
if (startedCall){
log.info("it is a incoming call");
}
/* Intent offHook = new Intent(context, BackgroundServices.class);
context.startService(offHook);*/
//Toast.makeText(context, "Phone state Off hook", Toast.LENGTH_LONG).show();
break;
case TelephonyManager.CALL_STATE_RINGING:
startedCall = true;
log.info("call state ringing");
//Toast.makeText(context, "Phone state Ringing", Toast.LENGTH_LONG).show();
break;
}
}`

Related

How to get disconnecting event of ble in Oreo+?

I have tried to connect using broadcast receiver and use Bluetooth adapter ack connect event but I can not get a disconnecting event. I can get connected, disconnected event but cannot get disconnecting event.
This is my code please help me with that.
#Override
public void onReceive(Context context, Intent intent) {
final String action = intent.getAction();
if (action != null) {
if (action.equals(BluetoothAdapter.ACTION_STATE_CHANGED)) {
final int bluetoothState = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE,
BluetoothAdapter.ERROR);
switch (bluetoothState) {
case BluetoothAdapter.STATE_ON:
//Bluethooth is on, now you can perform your tasks
Log.e("STAGE ", "State On");
break;
case BluetoothAdapter.STATE_OFF:
Log.e("STAGE ", "State Off");
break;
case BluetoothAdapter.STATE_CONNECTED:
Log.e("STAGE ", "STATE_CONNECTED");
break;
case BluetoothAdapter.STATE_CONNECTING:
Log.e("STAGE ", "STATE_CONNECTING");
break;
case BluetoothAdapter.STATE_DISCONNECTED:
Log.e("STAGE ", "SSTATE_DISCONNECTED");
break;
case BluetoothAdapter.STATE_DISCONNECTING:
Log.e("STAGE ", "STATE_DISCONNECTING");
break;
}
}
}
}
in my code
case BluetoothAdapter.STATE_CONNECTING:
Log.e("STAGE ", "STATE_CONNECTING");
break;
case BluetoothAdapter.STATE_DISCONNECTING:
Log.e("STAGE ", "STATE_DISCONNECTING");
break;
not working, not getting any log.
I use this code to register the reciever
BluetoothReciever bluetoothReciever = new BluetoothReciever();
IntentFilter intentFilter = new IntentFilter("android.bluetooth.adapter.action.STATE_CHANGED");
intentFilter.setPriority(IntentFilter.SYSTEM_HIGH_PRIORITY);
registerReceiver(bluetoothReciever, intentFilter);

How to get the Disconnected Phone Number in android PhoneStateListener

I am using PhoneStateListener to listen the call State when we have incoming call, outgoing and disconnect the call. All are working fine. But my question is i want to get the disconnected the phonenumber in CALL_STATE_IDLE, CALL_STATE_OFFHOOK.
When we have incoming call i can get the incoming phone number. But when the phone call end i cant able to get the disconnected phone number. I am using below code.
Anyone give me some ideas about how to get the phone number is CALL_STATE_IDLE, CALL_STATE_OFFHOOK.
// Call State Listener
private final class CallStateListener extends PhoneStateListener {
#Override
public void onCallStateChanged(int state, String inComingNumber) {
switch (mCallState) {
case TelephonyManager.CALL_STATE_IDLE:
if (state == TelephonyManager.CALL_STATE_OFFHOOK) {
// New Outgoing Call
} else if (state == TelephonyManager.CALL_STATE_RINGING) {
System.out.println("New Incoming Call");
System.out.println("CallStateListener Incoming Number: " + inComingNumber);
// New Incoming Call
}
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
if (state == TelephonyManager.CALL_STATE_IDLE) {
// Call Disconnected
System.out.println("CallStateListener Disconnected Number: " + inComingNumber);
System.out.println("Call Disconnected");
} else if (state == TelephonyManager.CALL_STATE_RINGING) {
// Another Call Waiting
}
break;
case TelephonyManager.CALL_STATE_RINGING:
if (state == TelephonyManager.CALL_STATE_OFFHOOK) {
System.out.println("Call Received");
System.out.println("CallStateListener Received Number: " + inComingNumber);
// Call Received
} else if (state == TelephonyManager.CALL_STATE_IDLE) {
System.out.println("CallStateListener Missed Number: " + inComingNumber);
// Call Missed
}else if(state == TelephonyManager.CALL_STATE_RINGING) {
}
break;
}
mCallState = state;
}
}
When i am using above PhoneStateListener i am getting the log as
CallStateListener Incoming Number: 4121312323
CallStateListener Missed Number:
CallStateListener Received Number:
CallStateListener Disconnected Number:
I can get only Incoming Call Number in RINGINE State, But I can't get Call received state Phone number, Call Missed State Phone Number and Disconnected Phone Number. Please any help guide me. Thanks
Try to open activity like this:
switch (state) {
case TelephonyManager.CALL_STATE_IDLE:
Log.v("idle state", "CALL_STATE_IDLE");
// CALL_STATE_IDLE ( OpenMissedCallScreen )
if (ring == true && callReceived == false && CheckMissCall.isRunning== false) {
flag = true;
if (prefs.getBoolean("main_state", true))
{
Intent inter = new Intent(c, MissCall.class);
}
}
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
// CALL_STATE_OFFHOOK ( OpenReceivedCallScreen )
callReceived = true;
break;
case TelephonyManager.CALL_STATE_RINGING:
ring = true;
// CALL_STATE_RINGING ( openIncomingCallScreen )
break;

Stop tts if the call is in offHook

I am doing an application which speaks the caller name after 4 sec of the ring tone.The problem is it speaks the caller Name after two or three rings. The problem is it keeps speaking the caller name even if the call is in offhook or idle I don't want so.
the code in onreceive of broadcast receiver is
state = bundle.getString(TelephonyManager.EXTRA_STATE);
if(state.equals( TelephonyManager.CALL_STATE_OFFHOOK))
{
System.out.println("fjkerj");
}
else if(state.equals( TelephonyManager.CALL_STATE_IDLE))
{
}
else if(state.equalsIgnoreCase(TelephonyManager.EXTRA_STATE_RINGING))
{
System.out.println("Entered Receiver");
final String phonenumber = bundle.getString(TelephonyManager.EXTRA_INCOMING_NUMBER);
// String info = "Detect Calls sample application\nIncoming number: " + phonenumber;
AudioManager amanager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
switch (amanager.getRingerMode()) {
case AudioManager.RINGER_MODE_SILENT:
System.out.println("Silent");
break;
case AudioManager.RINGER_MODE_VIBRATE:
System.out.println("Vibrate");
break;
case AudioManager.RINGER_MODE_NORMAL:
amanager.setStreamVolume(AudioManager.STREAM_RING,1, 0);
Handler handler=new Handler();
Runnable r=new Runnable()
{
public void run()
{
if(state.equalsIgnoreCase(TelephonyManager.EXTRA_STATE_RINGING))
{
Intent IncomingNumberIntent = new Intent(context1,SpeakOut.class);
IncomingNumberIntent.putExtra("PhoneNumber", phonenumber);
context1.startService(IncomingNumberIntent);
}
}
};
handler.postDelayed(r, 3000);
If the call is in ringing and profile is normal, it will go to a service and talks the name but
the trouble that app speaks the name even if the call is offhook.
Change your code to
int state = bundle.getInt(TelephonyManager.EXTRA_STATE);
switch (state)
{
case TelephonyManager.CALL_STATE_OFFHOOK:
System.out.println("fjkerj");
break;
case TelephonyManager.CALL_STATE_IDLE:
break;
case TelephonyManager.CALL_STATE_RINGING:
// Your code goes here
}
Remove from the run() method the if statement
if(state.equalsIgnoreCase(TelephonyManager.EXTRA_STATE_RINGING))

Android - In the event of outgoing calls, are being throws automatically the state of OFF_HOOK

In the event of outgoing calls, are being throws automatically the state of OFF HOOK
Hi guys, firstly my apologies for my english.
Well, in my application I'm monitoring every call, before and after. But just after of the call start, so I don't matter with "ringing" state, however my application is triggering a fake "off hook" state. When I make a call (outgoing call), my app is setting the state off hook immediately following the ringing. With this, I'm monitoring a call that I don't should, because that call can do not be answered.
Did someone already had this problem?
AndroidManifest.xml
<receiver
android:name=".CallStateBroadcastReceiver">
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE" />
</intent-filter>
</receiver>
<receiver
android:name=".OutgoingCallBroadcastReceiver">
<intent-filter>
<action android:name="android.intent.action.NEW_OUTGOING_CALL" />
</intent-filter>
</receiver>
CallStateBroadcastReceiver:
CallStatePhoneStateListener phoneListener = new CallStatePhoneStateListener(context, intent);
TelephonyManager telephony = (TelephonyManager) context.getSystemService(
Context.TELEPHONY_SERVICE);
telephony.listen(phoneListener, PhoneStateListener.LISTEN_CALL_STATE);
CallStatePhoneStateListener:
#Override
public void onCallStateChanged(int state, String incomingNumber) {
switch (state) {
case TelephonyManager.CALL_STATE_IDLE:
// do something
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
// do something
break;
case TelephonyManager.CALL_STATE_RINGING:
// do something
break;
}
OutgoingCallBroadcastReceiver:
#Override
public void onReceive(Context context, Intent intent) {
Bundle bundle = intent.getExtras();
if (bundle == null)
return;
String number = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);
// just save the number
}
Take a Static String eg:- prev_state and store the state of phone in that String at each case
example if u want to know how the outgoing call is disconnected then should do something like this,
if(state==TelephonyManager.CALL_STATE_IDLE)
{ Log.d(TAG, "CALL_STATE_IDLE==>"+incoming_nr);
{
if((prev_state==TelephonyManager.CALL_STATE_OFFHOOK))
{
prev_state=state;
// do something when the Call is ended
}
}
}
hope it helps :)
i hope this is work for u.
public void onCallStateChanged(int state, String incomingNumber) {
super.onCallStateChanged(state, incomingNumber);
Log.d("main", "incoming call receiver.");
PowerManager pm = (PowerManager) c
.getSystemService(Context.POWER_SERVICE);
boolean isScreenOn = pm.isScreenOn();
switch (state) {
case TelephonyManager.CALL_STATE_IDLE:
Log.v("idle state", "CALL_STATE_IDLE");
// CALL_STATE_IDLE;
if (ring == true && callReceived == false && CheckMissCall.isReject==true) {
Log.v("missed call", "Missed call from : " + incomingNumber);
if(CheckMissCall.isShown)
{
c.stopService(new Intent(c, Unlock_hud.class));
}
flag = true;
if (prefs.getBoolean("main_state", true) )
{
Intent inter = new Intent(c, MissCall.class);
inter.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
inter.putExtra("CellNumber", incomingNumber);
c.startActivity(inter);
}
}
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
// CALL_STATE_OFFHOOK;
callReceived = true;
Log.v("call received", "CALL_STATE_OFFHOOK " + incomingNumber);
break;
case TelephonyManager.CALL_STATE_RINGING:
ring = true;
// CALL_STATE_RINGING
Log.v("call ringing", "CALL_STATE_RINGING " + incomingNumber);
Log.d("flags", "flags: " + flag);
if (flag) {
//cut = true;
//flag = false;
CheckMissCall call = new CheckMissCall(c);
call.setName(incomingNumber);
call.setNumber4Sms(incomingNumber);
call.setContactPhoto();
Log.d("main", "state ringing");
prefs = PreferenceManager.getDefaultSharedPreferences(c);
if (!prefs.getBoolean("main_state", false)) {
return;
}
if (CheckMissCall.isRunning) {
return;
}
else {
Log.d("main", "EEEEEEEEEEEEEEEE: Unlock hud");
Intent in = new Intent(c, Unlock_hud.class);
in.setFlags(Intent.FLAG_ACTIVITY_PREVIOUS_IS_TOP);
c.startService(in);
// c.stopService(new Intent(c, Unlock_hud.class));
}
}
break;

outgoing call detect in PhoneStateListener

I want to detect outgoing call in android application . Actually I can detect outgoing calls but it is also working for incoming too and I don't want that. I need it to work only for outgoing. here is my code....
boolean ringing = false;
boolean offhook = false;
public void onCallStateChanged(int state, String incomingNumber) {
switch (state) {
case TelephonyManager.CALL_STATE_IDLE:
if((!ringing)&& offhook){
///incomming call
}
break;
case TelephonyManager.CALL_STATE_RINGING:
callerPhoneNumber = incomingNumber;
Log.d(TAG, "RINGING");
ringing = true;
offhook = false;
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
Log.d(TAG, "OFFHOOK");
offhook = true;
ringing = false;
break;
}
You can write a broadcast receiver for android.intent.action.NEW_OUTGOING_CALL.
See here Sample code is available

Categories

Resources