How to check phone service state in android - 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);

Related

How to listen with PhoneStateListener on dual sim?

I am new to android development. trying to make a simple call recorder.
The following PhoneStateListner is working perfectly for sim2 incoming and outgoing calls. but call state CALL_STATE_IDLE is being called right after CALL_STATE_OFFHOOK when an incoming/outcoming call event occurs in SIM1.
AtomicBoolean isRecording = new AtomicBoolean();
public void onCallStateChanged(int state, String incomingNumber) {
super.onCallStateChanged(state, incomingNumber);
switch (state) {
case TelephonyManager.CALL_STATE_IDLE: // Idle... no call
if (isRecording.get()) {
RecordCallService.stopRecording(context);
phoneCall = null;
isRecording.set(false);
}
break;
case TelephonyManager.CALL_STATE_OFFHOOK: // Call answered
if(!isRecording.get())
RecordCallService.startRecording(context, phoneCall);
}
break;
case TelephonyManager.CALL_STATE_RINGING:
if (null == phoneCall)
phoneCall = new CallLog();
}
break;
}
}

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

How to identify the Rejected call and Missed call in android?

How to identify the Rejected calls and Missed calls by using PhoneStateListener? In my code i am sending sms only rejected calls not a missed calls. Please verify below code.
class PhoneStateChangeListener extends PhoneStateListener{
public static boolean wasRinging;
SmsManager smsManager = SmsManager.getDefault();
#Override
public void onCallStateChanged(int state, String incomingNumber) {
switch(state){
case TelephonyManager.CALL_STATE_RINGING:
Log.e("pppp", "RINGING");
// Ringing
wasRinging=false;
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
Log.e("Lifttttttt", "OFFHOOK");
//Received calls
wasRinging = true;
break;
case TelephonyManager.CALL_STATE_IDLE:
Log.e("Endddddddd", "IDLE");
if(!wasRinging ){
//Missed or Rejected calls
}
wasRinging = true;
break;
}
super.onCallStateChanged(state, incomingNumber);
}
}
Edit: Is it possible or not? please give to replay. Thanks.

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

Streaming issue in android

I have streamed audio from url,but when the call or sms comes i need to pause the song and when the call ends again i am gonna resume the song...
How to do this?...
This method works in my app, it's pretty simple. In your onCreate() put this code to listen for phone events and then act appropriately:
TelephonyManager telephonyManager;
PhoneStateListener ringListener;
// Get the telephony manager
telephonyManager = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
// Create a new PhoneStateListener
ringListener = new PhoneStateListener() {
#Override
public void onCallStateChanged(int state, String incomingNumber) {
String stateString = "none";
switch (state) {
case TelephonyManager.CALL_STATE_IDLE:
stateString = "Idle";
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
stateString = "Off Hook";
break;
case TelephonyManager.CALL_STATE_RINGING:
stateString = "Ringing";
Log.i(TAG, "TELEPHONE RINGING!!");
//Do stuff here when phone ringing detected
break;
}
}
};
// Register the listener with the telephony manager
telephonyManager.listen(ringListener, PhoneStateListener.LISTEN_CALL_STATE);
//End create a new PhoneStateListener

Categories

Resources