Trouble with reading phone state - android

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

Related

setRingerMode to normall during incoming call doesn't work on Samsung devices

I've read here. Didn't find the answer.
public class CallReceiver extends BroadcastReceiver {
private TelephonyManager mTelephony;
private IncomingPhoneStateListener mIncomingPhoneListener;
private String number;
private Context mContext;
#Override
public void onReceive(Context context, Intent intent) {
try {
mContext = context;
number = intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER);
mTelephony = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
mIncomingPhoneListener = new IncomingPhoneStateListener();
mTelephony.listen(mIncomingPhoneListener, PhoneStateListener.LISTEN_CALL_STATE);
} catch (Exception e) {
Log.e("CallReceiver", "onReceive error:" + e.getMessage());
}
}
public class IncomingPhoneStateListener extends PhoneStateListener {
#Override
public void onCallStateChanged(int state, String incomingNumber) {
AudioManager am = (AudioManager) mContext.getSystemService(Context.AUDIO_SERVICE);
switch (state) {
case TelephonyManager.CALL_STATE_RINGING:
// ******* This code should unmute and change volume to MAX
am.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
am.setStreamVolume(AudioManager.STREAM_RING, am.getStreamMaxVolume(AudioManager.STREAM_RING), AudioManager.FLAG_PLAY_SOUND);
// ******* end
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
break;
case TelephonyManager.CALL_STATE_IDLE:
break;
default:
break;
}
super.onCallStateChanged(state, incomingNumber);
mTelephony.listen(mIncomingPhoneListener, PhoneStateListener.LISTEN_NONE); // unregister
}
}
}
When the device is on silent mode or vibration mode, above code unmute and change the volume to the max.
This code works on Nexus devices (eg. Galaxy Nexus)
However, it doesn't work on Samsung devices (S3, S4, Note 2) [Android version 4.3].
Any suggestions?
Since I couldn't find the answer, I came up with the work-around by actually playing the ringer sound in my service.

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

Use TelephonyManager in fragment?

I want to start and stop service when device in action call, so i use TelephonyManager and my class extends flagment. i Tried this code
TelephonyManager tm = (TelephonyManager)getActivity().getSystemService(Context.TELEPHONY_SERVICE);
tm.listen(mPhoneListener, PhoneStateListener.LISTEN_CALL_STATE);
private PhoneStateListener mPhoneListener = new PhoneStateListener() {
public void onCallStateChanged(int state, String incomingNumber) {
switch (state) {
case TelephonyManager.CALL_STATE_RINGING:
getActivity().stopService(new Intent(getActivity(),LockScreenService.class));
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
getActivity().stopService(new Intent(getActivity(),LockScreenService.class));
break;
case TelephonyManager.CALL_STATE_IDLE:
getActivity().startService(new Intent(getActivity(),LockScreenService.class));
break;
}
}
}
;
but it does not work. Please give me some advises. Thanks so much
Try this code:
telephonyManager = ( TelephonyManager )getActivity.getSystemService( Context.TELEPHONY_SERVICE );
imeistring = telephonyManager.getDeviceId();
use Telephony manager inside your oncreate view

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

blocking Incoming call - Android

i have incoming call broadcast receiver and phone state listener class , how to block or reject incoming call and the ringing notification
BroadCastReceiver
public class PhoneCallReceiver extends BroadcastReceiver{
private static final String TAG = "Phone call";
#Override
public void onReceive(Context context, Intent intent) {
Log.v(TAG, "Receving....");
TelephonyManager telephony = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
SamplePhoneStateListener customPhoneListener = new SamplePhoneStateListener();
telephony.listen(customPhoneListener, PhoneStateListener.LISTEN_CALL_STATE);
}
}
Phone Listener
public class SamplePhoneStateListener extends PhoneStateListener {
private static final String TAG = "Phone Listner";
#Override
public void onCallStateChanged(int state, String incomingNumber) {
Log.v(TAG, "Current State is - "+state);
Log.v(TAG, incomingNumber);
switch(state){
case TelephonyManager.CALL_STATE_RINGING:
Log.d(TAG, "Phone Ringigg");
break;
}
super.onCallStateChanged(state, incomingNumber);
}
}
make sure you have the permission android.permission.READ_PHONE_STATE set up.
To block a call u have to use java reflections and access hidden file of android.telephony package. this file (ITelephony.java) has a function called endcall() which uses permissions. this is not advised by google team as recieving and ending calls in a programmatic way is invasion of user privacy.

Categories

Resources