Handling Android application pause on incoming call and resume after call end - android

I want to pause my android application when the phone receives an incoming call. After the call ends, I want my applications to resume automatically.
How would this be implemented in an Android application?

you have to implement a Listener for the PhoneState. I did this in a private Class:
private class PhoneCallListener extends PhoneStateListener {
private boolean isPhoneCalling = false;
// needed for logging
String TAG = "PhoneCallListener";
#Override
public void onCallStateChanged(int state, String incomingNumber) {
if (TelephonyManager.CALL_STATE_RINGING == state) {
// phone ringing
Log.i(TAG, "RINGING, number: " + incomingNumber);
}
if (TelephonyManager.CALL_STATE_OFFHOOK == state) {
// active
Log.i(TAG, "OFFHOOK");
isPhoneCalling = true;
}
if (TelephonyManager.CALL_STATE_IDLE == state) {
// run when class initial and phone call ended,
// need detect flag from CALL_STATE_OFFHOOK
Log.i(TAG, "IDLE");
if (isPhoneCalling) {
Log.i(TAG, "restart app");
// restart call application
Intent i = getBaseContext().getPackageManager()
.getLaunchIntentForPackage(
getBaseContext().getPackageName());
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK
| Intent.FLAG_ACTIVITY_CLEAR_TOP
| Intent.FLAG_ACTIVITY_SINGLE_TOP);
startActivity(i);
isPhoneCalling = false;
}
}
}
}
and you need to add the permission to the Manifest-File
<uses-permission android:name="android.permission.READ_PHONE_STATE" />

private class EndCallListener extends PhoneStateListener {
private boolean active = false;
#Override
public void onCallStateChanged(int state, String incomingNumber) {
if(TelephonyManager.CALL_STATE_RINGING == state) {
Log.i("EndCallListener", "RINGING, number: " + incomingNumber);
}
if(TelephonyManager.CALL_STATE_OFFHOOK == state) {
//wait for phone to go offhook (probably set a boolean flag) so you know your app initiated the call.
active = true;
Log.i("EndCallListener", "OFFHOOK");
}
if(TelephonyManager.CALL_STATE_IDLE == state) {
//when this state occurs, and your flag is set, restart your app
Log.i("EndCallListener", "IDLE");
if (active) {
active = false;
// stop listening
TelephonyManager mTM = (TelephonyManager) m_activity.getSystemService( Context.TELEPHONY_SERVICE );
mTM.listen(this, PhoneStateListener.LISTEN_NONE);
// restart the inbox activity
//Intent intent = new Intent(m_activity, MDInboxActivity.class);
//m_activity.startActivity(intent);
}
}
}
}
And you can initialize the above class by calling the below lines:
try {
EndCallListener callListener = new EndCallListener();
TelephonyManager mTM = (TelephonyManager) m_activity.getSystemService(Context.TELEPHONY_SERVICE);
mTM.listen(callListener, PhoneStateListener.LISTEN_CALL_STATE);
} catch(Exception e) {
Log.e("callMonitor", "Exception: "+e.toString());
}

Related

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.

Is it possible to know using TelephonyManager (onCallStateChanged) when the other person answers the call?

I am developing an application that starts an alarm (Ringtone) when I start a call and it also set speaker mode on. So I would like to stop the alarm when the person who I am calling answers the call. How could I achieve this?
This is my code:
private PhoneStateListener phoneCallListener = new PhoneStateListener() {
private int prevCallState=TelephonyManager.CALL_STATE_IDLE;
public void onCallStateChanged(int state, String incomingNumber) {
try {
AudioManager audiomanager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
switch (state) {
case TelephonyManager.CALL_STATE_OFFHOOK:
if (prevCallState == TelephonyManager.CALL_STATE_IDLE){
//Inicio de llamada saliente
audiomanager.setMode(AudioManager.MODE_IN_CALL);
audiomanager.setSpeakerphoneOn(true);
}
break;
case TelephonyManager.CALL_STATE_IDLE:
if (prevCallState == TelephonyManager.CALL_STATE_OFFHOOK){
//Fin de llamada saliente
audiomanager.setSpeakerphoneOn(false);
}
break;
}
} catch (Exception e) {
}
prevCallState = state;
}
};
I would appreciate any help. Thanxs a lot.
Android check when outgoing call is received by callee
hey check this link may try this it can helpful to you to sort out your issue.#Ravindra
I had similar problems, it is not possible to check if the person have picked up the phone nor his/her phone is not reachable. There is no feedback from the telephone companys in gsm networks.
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button) findViewById(R.id.buttonCall);
// add PhoneStateListener
PhoneCallListener phoneListener = new PhoneCallListener();
TelephonyManager telephonyManager = (TelephonyManager) this
.getSystemService(Context.TELEPHONY_SERVICE);
telephonyManager.listen(phoneListener,PhoneStateListener.LISTEN_CALL_STATE);
// add button listener
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent callIntent = new Intent(Intent.ACTION_CALL);
callIntent.setData(Uri.parse("tel:+9196203333"));
startActivity(callIntent);
}
});
}
//monitor phone call activities
private class PhoneCallListener extends PhoneStateListener {
private boolean isPhoneCalling = false;
String LOG_TAG = "LOGGING 123";
#Override
public void onCallStateChanged(int state, String incomingNumber) {
if (TelephonyManager.CALL_STATE_RINGING == state) {
// phone ringing
Log.i(LOG_TAG, "RINGING, number: " + incomingNumber);
}
if (TelephonyManager.CALL_STATE_OFFHOOK == state) {
// active
Log.i(LOG_TAG, "OFFHOOK");
isPhoneCalling = true;
}
if (TelephonyManager.CALL_STATE_IDLE == state) {
// run when class initial and phone call ended,
// need detect flag from CALL_STATE_OFFHOOK
Log.i(LOG_TAG, "IDLE");
if (isPhoneCalling) {
Log.i(LOG_TAG, "restart app");
// restart app
Intent i = getBaseContext().getPackageManager()
.getLaunchIntentForPackage(
getBaseContext().getPackageName());
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);
isPhoneCalling = false;
}
}
}
}
and also give permission in Manifest

everytime End phone Call come back to my activity even if i have not make a phone call from my application android

i have created application into which user can make a call on button click.
i have found below code which is working fine to make call and come back to my activity when phone call end. but i have one problem in this application that is once i make a phone call from my application and end that phone call, after completing this whole cycle,i have press home button or back button. i will call some one from my phone directory and when end a call it will come back to my application not in phone directory.
public void imgbtnCallPhone_Click(View view) {
EditText txtBusinessPhone = (EditText) findViewById(R.id.txtPhone);
try
{
final Intent callIntent = new Intent(android.content.Intent.ACTION_CALL);
callIntent.setData(Uri.parse("tel:"+ txtBusinessPhone.getText()));
startActivity(callIntent);
} catch (ActivityNotFoundException activityException) {
//Log.e("Calling a Phone Number", "Call failed", activityException);
}
}
private class PhoneCallListener extends PhoneStateListener {
private boolean isPhoneCalling = false;
String LOG_TAG = "LOGGING 123";
#Override
public void onCallStateChanged(int state, String incomingNumber) {
if (TelephonyManager.CALL_STATE_RINGING == state) {
// phone ringing
//Log.i(LOG_TAG, "RINGING, number: " + incomingNumber);
}
if (TelephonyManager.CALL_STATE_OFFHOOK == state) {
// active
//Log.i(LOG_TAG, "OFFHOOK");
isPhoneCalling = true;
}
if (TelephonyManager.CALL_STATE_IDLE == state) {
// run when class initial and phone call ended,
// need detect flag from CALL_STATE_OFFHOOK
//Log.i(LOG_TAG, "IDLE");
if (isPhoneCalling) {
//Log.i(LOG_TAG, "restart app");
// restart app
Intent i = getBaseContext().getPackageManager()
.getLaunchIntentForPackage(
getBaseContext().getPackageName());
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);
isPhoneCalling = false;
}
}
}
}
i want a code that will check phone call is related to my application or not,
if phone call done by my application then after end phone call it will come back
to my application activity otherwise don't come back to my activity, do it default.
Thanks,
I think you need to start your PhoneCallListener right before you call, pass the number you are going to call to the PhoneCallListener and then start the callIntent.
In your PhoneListener, you can check if the number matches with the number you passed from your Activity. If true, restart your activitry, else do nothing.
EDIT:
public void imgbtnCallPhone_Click(View view) {
EditText txtBusinessPhone = (EditText) findViewById(R.id.txtPhone);
// Get your PhoneCallListener and pass the number
PhoneCallListener mPhoneListener = new PhoneCallListener();
mPhoneListener.yourActivity = true;
// start listening
TelephonyManager tm = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
tm.listen(mPhoneListener, PhoneStateListener.LISTEN_CALL_STATE);
try
{
final Intent callIntent = new Intent(android.content.Intent.ACTION_CALL);
callIntent.setData(Uri.parse("tel:"+ txtBusinessPhone.getText()));
startActivity(callIntent);
} catch (ActivityNotFoundException activityException) {
//Log.e("Calling a Phone Number", "Call failed", activityException);
}
}
Your PhoneCallListener:
private class PhoneCallListener extends PhoneStateListener {
private boolean isPhoneCalling = false;
public Boolean yourActivity = false;
String LOG_TAG = "LOGGING 123";
#Override
public void onCallStateChanged(int state, String incomingNumber) {
if (TelephonyManager.CALL_STATE_RINGING == state) {
// phone ringing
//Log.i(LOG_TAG, "RINGING, number: " + incomingNumber);
}
if (TelephonyManager.CALL_STATE_OFFHOOK == state) {
// active
//Log.i(LOG_TAG, "OFFHOOK");
isPhoneCalling = true;
}
if (TelephonyManager.CALL_STATE_IDLE == state) {
// run when class initial and phone call ended,
// need detect flag from CALL_STATE_OFFHOOK
//Log.i(LOG_TAG, "IDLE");
if (isPhoneCalling && yourActivity) {
//Log.i(LOG_TAG, "restart app");
// restart app
Intent i = getBaseContext().getPackageManager()
.getLaunchIntentForPackage(
getBaseContext().getPackageName());
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);
yourActivity = false;
isPhoneCalling = false;
}
}
}
}
I didn't test it, so it may contains some errors.

How to detect answered or rejected state in outgoing call

i try to write a program for android .I want to make a call with my program and after that recognize the call status. With Flowing Function I can recognize the hang out status and for answering status it doesn’t work.I try this function but it doesn’t work either
private class PhoneCallListener extends PhoneStateListener {
private boolean isPhoneCalling = false;
String LOG_TAG = "LOGGING 123";
#Override
public void onCallStateChanged(int state, String incomingNumber) {
if (TelephonyManager.CALL_STATE_RINGING == state) {
// phone ringing
Log.i(LOG_TAG, "RINGING, number: " + incomingNumber);
}
if (TelephonyManager.CALL_STATE_OFFHOOK == state) {
// active
Log.i(LOG_TAG, "OFFHOOK");
isPhoneCalling = true;
}
if (TelephonyManager.CALL_STATE_IDLE == state) {
// run when class initial and phone call ended, need detect flag
// from CALL_STATE_OFFHOOK
Log.i(LOG_TAG, "IDLE");
if (isPhoneCalling) {
Log.i(LOG_TAG, "restart app");
// restart app
Intent i = getBaseContext().getPackageManager()
.getLaunchIntentForPackage(
getBaseContext().getPackageName());
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);
isPhoneCalling = false;
}
}
}
}
You can not do that with CALL_STATE_IDLE and CALL_STATE_OFFHOOK.
You have to detect programatically the end of outgoing ringtone...

making a list of outgoing calls one by one automatically

i want to make calls to list of phone numbers(4/5 numbers) from my phone;i made on call, after ending that call then only it has to call next number (which is automatically). what my thought is:
for(int i=0;i<aray.lenght;i++)
{
callIntent=new Intent(Intent.ACTION_CALL);
callIntent.setData(Uri.parse("tel:"+num));
startActivity(callintent);
}
we know defaultly only two outgoing calls will go. i want to restrict one outgoing call has to go. and after talking/ ending ; next number will call, this process will continue until list of numbers over.here we also have to check the status of an outgoing call, ringing,offhookk and idle;how come we know or placed only single call by using three states. try to help.
try like this..
let nums be the list of numbers..
public class CallsActivity extends Activity {
final Context context = this;
public String num;
String LOG_TAG = "EMERGENCY CALL";
public String[] pnum={"9666848344","9603939029","7404230210","9030109791"};
ArrayList<String> b= new ArrayList<String>(Arrays.asList(pnum));
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
num=b.get(0);
call(num);
// add PhoneStateListener
PhoneCallListener phoneListener = new PhoneCallListener();
TelephonyManager telephonyManager = (TelephonyManager) this
.getSystemService(Context.TELEPHONY_SERVICE);
telephonyManager.listen(phoneListener,
PhoneStateListener.LISTEN_CALL_STATE);
}
void call(String num1)
{
Intent callIntent=new Intent(Intent.ACTION_CALL);
callIntent.setData(Uri.parse("tel:"+num1));
startActivity(callIntent);
int indx=b.indexOf(num1);
//Log.i(LOG_TAG, "indx"+indx);
if (indx!=b.size())
{
num=b.get(indx+1);
}
}
private class PhoneCallListener extends PhoneStateListener {
private boolean isPhoneCalling = false;
#Override
public void onCallStateChanged(int state, String incomingNumber) {
if (TelephonyManager.CALL_STATE_RINGING == state) {
// phone ringing
Log.i(LOG_TAG, "RINGING, number: " + incomingNumber);
}
if (TelephonyManager.CALL_STATE_OFFHOOK == state) {
// active
Log.i(LOG_TAG, "OFFHOOK");
isPhoneCalling = true;
}
if (TelephonyManager.CALL_STATE_IDLE == state) {
// run when class initial and phone call ended, need detect flag
// from CALL_STATE_OFFHOOK
Log.i(LOG_TAG, "IDLE");
if (isPhoneCalling) {
Log.i(LOG_TAG, "CALL...");
// restart app
Intent i = getBaseContext().getPackageManager()
.getLaunchIntentForPackage(
getBaseContext().getPackageName());
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
// startActivity(i);
call(num);
isPhoneCalling = false;
}
}
}
}
}

Categories

Resources