Automatiquelly resume application after a phone call ends [duplicate] - android

This question already has answers here:
How to make a phone call in android and come back to my activity when the call is done?
(21 answers)
Closed 9 years ago.
I try here to make a phone call and it works but the problem is when i finish the call,i don't get my view back.it stay stack in the calls list...and when i click on the button return in my phone it goes back to my activity that i want to show...Thanks :)
#Override
public void onClick(View arg0) {
if (arg0 == btn_ajout) {
Intent i=new Intent(Docteur_gestion.this, AjoutDocActivity.class);
startActivity(i);
}
if(arg0 == btn_appel) {
Intent callIntent = new Intent(Intent.ACTION_CALL);
callIntent.setData(Uri.parse("tel:"+global_txt_tel_doc));
startActivity(callIntent);
}
}

You need to implement a phoneStateListener as follows:
public class MainActivity extends Activity {
final Context context = this;
private Button button;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.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:0377778888"));
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;
}
}
}
}
}
Two permissions required in the manifest
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
and
<uses-permission android:name="android.permission.CALL_PHONE" />
I found this solution on this link

Related

My Broadcastreceiver seem to detect that a call ended, still doesn't do that it should do. Does anybody see the flaws in my code?

I would like to make two calls in a row in an android app. Upon clicking button the app calls the first number. I created the broadcastreceiver below, that detects when the first call ends. It should write out that "First call ended" and then call the second number. It does not seem working. Can anybody spot the mistake in my code?
public class MainActivity extends AppCompatActivity {
public void calling(String phone) {
Intent callIntent = new Intent(Intent.ACTION_CALL)
.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
callIntent.setData(Uri.parse("tel:" + phone));
callIntent.putExtra("com.android.phone.extra.slot", 1);
startActivity(callIntent);
Intent intent = new Intent(this, CallReciever.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this.getApplicationContext(), 234324243 , intent, 0);
startActivity(callIntent);
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button b = (Button) this.findViewById(R.id.CallButton);
b.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
calling("+36309577686");
}
});
}
public class CallReciever extends BroadcastReceiver {
private Context mContext;
private CustomPhoneStateListener mPhoneListener;
private String incoming_nr;
private int prev_state;
#Override
public void onReceive(Context context, Intent intent) {
mContext = context;
if (mPhoneListener == null) {
mPhoneListener = new CustomPhoneStateListener();
// TelephonyManager object
TelephonyManager telephony = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
// Register our listener with TelephonyManager
telephony.listen(mPhoneListener, PhoneStateListener.LISTEN_CALL_STATE);
}
}
/* Custom PhoneStateListener */
class CustomPhoneStateListener extends PhoneStateListener {
#Override
public void onCallStateChanged(int state, String incomingNumber) {
if (!TextUtils.isEmpty(incomingNumber)) {
incoming_nr = incomingNumber;
}
switch (state) {
case TelephonyManager.CALL_STATE_RINGING:
prev_state = state;
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
prev_state = state;
break;
case TelephonyManager.CALL_STATE_IDLE:
if ((prev_state == TelephonyManager.CALL_STATE_OFFHOOK)) {
// A call has now ended
//it writes out the call end, but does not call. why?
Toast.makeText(mContext, "Call End", Toast.LENGTH_SHORT).show();
calling("+36303853440");
prev_state = state;
}
else if ((prev_state == TelephonyManager.CALL_STATE_RINGING)) {
// Rejected or Missed call
Toast.makeText(mContext, "Rejected Call", Toast.LENGTH_SHORT).show();
prev_state = state;
}
break;
}
}
}
}
}
You are not registering the CallReciever anywhere. Try this
Button b = (Button) this.findViewById(R.id.CallButton);
b.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
calling("+36309577686");
registerReceiver(new CallReceiver());
}
});
Then unregister the receiver on broadcast is received

Broadcast Receiver Call forwarding for selected numbers/callers not working

I have created an app that forwards selected call to a number. However, right now, all call are forwarded to the number. What is missing in my code which does not allow specific callers to be forwarded. Thanks
java class
public class CallForwarding extends Activity {
private SharedPreferences prefs;
private EditText editText;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.call_forwarding);
final Switch switch1 = (Switch) findViewById(R.id.switch1);
editText = (EditText) findViewById(R.id.editText1);
SharedPreferences prefs = getPreferences(MODE_PRIVATE);
editText.setText(prefs.getString("edittext_content", ""));
switch1.setChecked(false);
switch1.setOnCheckedChangeListener(new OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
if(editText.getText().toString().trim().length() <= 7){
switch1.setChecked(false);
Toast.makeText(getApplicationContext(), "Please enter a number for receiving forwarded calls",Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(getApplicationContext(), "Call Forwarding is activated",Toast.LENGTH_SHORT).show();
callforward("*21*" + editText.getText().toString() + "#" ); // 0123456789 is the number you want to forward the calls.;
}
}else{
Toast.makeText(getApplicationContext(), "Call Forwarding is deactivated",Toast.LENGTH_SHORT).show();
callforward("#21#");
}
}
});}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public void onPause(){
super.onPause();
prefs = getPreferences(MODE_PRIVATE);
SharedPreferences.Editor editor = prefs.edit();
editText = (EditText)findViewById(R.id.editText1);
editor.putString("edittext_content", editText.getText().toString());
editor.commit();
}
public void manage_numbers_onClick(View view) {
Intent myIntent = new Intent(this, Set_numbers_to_forward.class);
startActivity(myIntent);
}
private void callforward(String ArrayString)
{
PhoneCallListener phoneListener = new PhoneCallListener();
TelephonyManager telephonyManager = (TelephonyManager)
this.getSystemService(Context.TELEPHONY_SERVICE);
telephonyManager.listen(phoneListener, PhoneStateListener.LISTEN_CALL_STATE);
Intent intentCallForward = new Intent(Intent.ACTION_CALL);
Uri mmiCode = Uri.fromParts("tel", ArrayString, "#");
intentCallForward.setData(mmiCode);
startActivity(intentCallForward);
}
public void onReceive(Context context, Intent intent) {
String s = intent.getStringExtra(TelephonyManager.EXTRA_STATE);
if(s.equals(TelephonyManager.EXTRA_STATE_RINGING)){
String incomingnumber = intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER);
incomingnumber.contentEquals(incomingnumber);
}
}
private class PhoneCallListener extends PhoneStateListener
{ private Context mContext;
private boolean isPhoneCalling = false;
DBAdapter db = new DBAdapter(mContext);
#Override
public void onCallStateChanged(int state, String incomingnumber)
{
//phone ringing
if (TelephonyManager.CALL_STATE_RINGING == state)
{
String contacts = db.getAllContacts().toString();
if(incomingnumber == contacts) {
callforward("*21*" + editText.getText().toString() + "#" );
} else if(incomingnumber != contacts){
//do nothing
callforward("#21#");
}
}
if (TelephonyManager.CALL_STATE_OFFHOOK == state)
{
// active
isPhoneCalling = true;
}
if (TelephonyManager.CALL_STATE_IDLE == state)
{
// run when class initial and phone call ended, need detect flag
// from CALL_STATE_OFFHOOK
if (isPhoneCalling)
{
Intent i = getBaseContext().getPackageManager()
.getLaunchIntentForPackage(getBaseContext().getPackageName());
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);
isPhoneCalling = false;
}
}
}
}
}

How to keep Base Activity running?

I have an activity named MainActivity.
In which I have started another activity (ContentDetails)
public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
Uri contactUri = ContentUris.withAppendedId(People.CONTENT_URI, id);
Intent intent = new Intent(this, ContactDetails.class);
intent.setData(contactUri);
startActivity(intent);
}
And in ContactDetails.java I have written code to make a phone call.
but when I end call. It finishes all application instead of redirecting to base activity
following is the code of ContactDetails.java
public class ContactDetails extends Activity{
TextView nameField = null;
TextView phoneField = null;
TextView idField = null;
final Context context = this;
private Button button;
/**
* Called when the activity is first created.
*/
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.details);
idField = (TextView) findViewById(R.id.contact_id);
nameField = (TextView) findViewById(R.id.contact_name);
phoneField = (TextView) findViewById(R.id.contact_phone);
button = (Button) findViewById(R.id.call_button);
// add PhoneStateListener
PhoneCallListener phoneListener = new PhoneCallListener();
TelephonyManager 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:03577899456"));
startActivity(callIntent);
}
});
}
#Override
protected void onStart() {
super.onStart();
Cursor cursor = managedQuery(getIntent().getData(), null, null, null, null);
cursor.moveToFirst();
//idField.setText(cursor.getString(cursor.getColumnIndex(People._ID)));
nameField.setText(cursor.getString(cursor.getColumnIndex(People.DISPLAY_NAME)));
phoneField.setText(cursor.getString(cursor.getColumnIndex(People.NAME)));
}
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
try{
// Intent intentAndroid = new Intent().setClass(this, ContactList.class);
Intent i = getBaseContext().getPackageManager()
.getLaunchIntentForPackage(getBaseContext().getPackageName());
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);
}catch (Exception e) {
// TODO: handle exception
}
isPhoneCalling = false;
}
}
}
}
}
Actually, I think you have to use
startActivityForResult(callIntent, <RESULT_OK>);
instead of
startActivity(callIntent);
And override onActivityResult() in your ContactDetails Activity.
Try this and let me know what happen..
Also from your code I suggest you to don't use of Flag i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); As its clear the current Activities state Task.

restart the app after phone call

I'm having a problem to return to my app after a call as after the call is ended it sends me to the main screen instead of my app.
public class ButtonView extends FrameLayout {
private static final String TAG = ButtonView.class.getSimpleName();
private final View mButtonView;
private Server mServer;
final Context context = getContext();
public ButtonView(Context context, AttributeSet attrs) {
super(context, attrs);
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
mButtonView = inflater.inflate(R.layout.input, this);
}
.
.
.
.
.
.
public void call() {
PhoneCallListener phoneListener = new PhoneCallListener();
TelephonyManager telephonyManager = (TelephonyManager) getContext()
.getSystemService(Context.TELEPHONY_SERVICE);
telephonyManager.listen(phoneListener,
PhoneStateListener.LISTEN_CALL_STATE);
Intent callIntent = new Intent(Intent.ACTION_CALL);
callIntent.setData(Uri.parse("tel:048598077"));
getContext().startActivity(callIntent);
}
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
Context appContext = context.getApplicationContext();
Intent i = appContext.getPackageManager().getLaunchIntentForPackage(appContext.getPackageName());
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
context.startActivity(i);
isPhoneCalling = false;
}
}
}
}
does someone know what the problem is? or any other way to restart the app after a call?
if you are using emulator then kindly test your application on real device instead of emulator.and ya there is no need of PhoneStateListener and starting your application manually.just test on real device,it will start your application automatically.

Android : why PhoneCallListener still alive after activity finish?

im using a phone call listener in my activity but after finishing my activity , afer user make a call, my phone call listener not dead and brig up activity again !! please help me.
phoneListener = new PhoneCallListener();
telephonyManager = (TelephonyManager)
TransferActivity.this.getSystemService(Context.TELEPHONY_SERVICE);
telephonyManager.listen(phoneListener, PhoneStateListener.LISTEN_CALL_STATE);
PhoneCallListener class :
private class PhoneCallListener extends PhoneStateListener {
boolean isPhoneCalling = false;
#Override
public void onCallStateChanged(int state, String incomingNumber) {
if (TelephonyManager.CALL_STATE_RINGING == state) {
}
if (TelephonyManager.CALL_STATE_OFFHOOK == state) {
isPhoneCalling = true;
}
if (TelephonyManager.CALL_STATE_IDLE == state) {
if (isPhoneCalling) {
isPhoneCalling = false;
Intent intent = getIntent();
startActivity(intent);
}
}
}
}
}
The documentation says:
To un-register a listener, pass the listener object and set the events
argument to PhoneStateListener#LISTEN_NONE (0)
Here is the link to the docs.
Did you try setting the Listener to null as,
telephonyManager.listen(null, PhoneStateListener.LISTEN_NONE);

Categories

Resources