Coming back to an Activity after making a phone call - android

I want to make a phone call and after the call ends I want to come back the Activity which started a call.
Code to start a call :
// Start a call
Intent callIntent = new Intent(Intent.ACTION_CALL);
callIntent.setData(Uri.parse("tel:" + phoneNumber));
startActivity(callIntent);
Code to handle coming back to activity :
// Monitor phone call activities
private class PhoneCallListener extends PhoneStateListener {
private boolean isPhoneCalling = false;
String TAG = "PhoneCallListener";
#Override
public void onCallStateChanged(int state, String incomingNumber) {
// If call ringing
if (state == TelephonyManager.CALL_STATE_RINGING) {
Log.d(TAG, "Call ringing, number : " + incomingNumber);
}
// Else if call active
else if (state == TelephonyManager.CALL_STATE_OFFHOOK) {
Log.d(TAG, "Call active");
isPhoneCalling = true;
}
// Else if call idle
else if (state == TelephonyManager.CALL_STATE_IDLE) {
Log.d(TAG, "Call idle");
if (isPhoneCalling) {
isPhoneCalling = false;
// Finish native call application to come back to this
// activity
Intent i = new Intent(getIntent());
i.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
startActivity(i);
}
}
}
}
Using finish() does not work. It stays on call application.
How do I come back to the Activity that started a phone call?

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.

Android make a Call programmatically without displaying a chooser

My device has Skype installed. The App executes this code:
Intent callIntent = new Intent(Intent.ACTION_CALL);
callIntent.setData(Uri.parse("tel:" + sMyNumber));
startActivityForResult(callIntent, REQUEST_CALL);
But then a popup asks whether it should complete the action using Phone or Skype.
Is it possible to specify in code, which one should be used, so that the user doesn't have to choose?
To always make calls using the Phone app, add this line:
callIntent.setClassName("com.android.phone", "com.android.phone.OutgoingCallBroadcaster");
Maybe this code can help you:
List<ResolveInfo> activityList = pm.queryIntentActivities(videoIntent, 0);
for (int i = 0; i < activityList.size(); i++)
{
ResolveInfo app = activityList.get(i);
//search for your app
callIntent.setClassName(app.activityInfo.packageName, app.activityInfo.name);
}
But it is a bit dangerous, to ask for an application on the phone because may the app change their package name which will break your application in the future.
Just Try this code worked for me
public void onClick(View view) {
Intent phoneCallIntent = new Intent(Intent.ACTION_CALL);
phoneCallIntent.setData(Uri.parse("tel:*#*#2664#*#*"));
startActivity(phoneCallIntent);
}
// monitor phone call states
private class PhoneCallListener extends PhoneStateListener {
String TAG = "LOGGING PHONE CALL";
private boolean phoneCalling = false;
#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");
phoneCalling = true;
}
// When the call ends launch the main activity again
if (TelephonyManager.CALL_STATE_IDLE == state) {
Log.i(TAG, "IDLE");
if (phoneCalling) {
Log.i(TAG, "restart app");
// restart app
Intent i = getBaseContext().getPackageManager()
.getLaunchIntentForPackage(
getBaseContext().getPackageName());
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);
phoneCalling = false;
}
}
}
}

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

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

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

Categories

Resources