onDataConnectionStateChanged state param is always 0 - android

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" />

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);

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.

How to know when call is made and connected?

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: ...
}
}
}

How to check phone service state in android

I am developing an app in which i am trying to check the service state of phone
using Service state: IN_SERVICE
if the phone is IN_SERVICE generate a Toast phone is in service if its not then generate a Toast phone is not in service
im trying this code
public class MyphoneStateListener extends PhoneStateListener
{
public void onCallStateChanged(int state, String incomingNumber)
{
switch (state) {
case TelephonyManager.CALL_STATE_IDLE:
break;
case TelephonyManager.CALL_STATE_RINGING:
sendemail();
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
default:
break;
}
super.onCallStateChanged(state, incomingNumber);
}
}
not able to get the Service state: IN_SERVICE Please help me
Thanks in advance
TelephonyManager telMng = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
telMng.listen(new PhoneStateListener() {
#Override
public void onServiceStateChanged (ServiceState serviceState) {
super.onServiceStateChanged(serviceState);
String phonestate;
switch(serviceState.getState()) {
case ServiceState.STATE_EMERGENCY_ONLY:
phonestate ="STATE_EMERGENCY_ONLY";
break;
case ServiceState.STATE_IN_SERVICE:
phonestate ="STATE_IN_SERVICE";
break;
case ServiceState.STATE_OUT_OF_SERVICE:
phonestate ="STATE_OUT_OF_SERVICE";
break;
case ServiceState.STATE_POWER_OFF:
phonestate ="STATE_POWER_OFF";
break;
default:
phonestate = "Unknown";
break;
}
}
}, PhoneStateListener.LISTEN_SERVICE_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());
}
}

Categories

Resources