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
Related
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;
}
}
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
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);
I heard that we can not use TelephonyManager for detecting Incoming/Missed/Dialed calls from Android version 2.3 and above. Is this true? Incase yes, Do we have any workaround for handling the calls? Please suggest. Im in urgent need of it.
Check this post. It will help.
You can query it.
Please find the code to use TelephonyManager. I tried in 2.3.3 and is working for me :
public class MainActivity extends Activity {
private TelephonyManager telephonyManager;
private PhoneStateListener listener;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Get the telephony manager
telephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
// Create a new PhoneStateListener
listener = new PhoneStateListener() {
#Override
public void onCallStateChanged(int state, String incomingNumber) {
String stateString = "N/A";
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";
break;
}
Log.i("DEMO",
String.format("\nonCallStateChanged: %s", stateString));
}
};
telephonyManager.listen(listener, PhoneStateListener.LISTEN_CALL_STATE);
}
In Manifest, you need to put the permission :
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
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