0

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);       
    }
}
Fg.Salim
  • 49
  • 7
  • Your formatting is rather bad. Please make sure you at least capitalize correctly and that your code samples are correctly indented. That's why there is the dynamic preview at the bottom. – Jan Hudec Mar 29 '13 at 12:08
  • @CobraAjgar when i hang down after a call (unhook) i want to see the activity again not the menu call (call log) sorry for the bad formatting... – Fg.Salim Mar 29 '13 at 13:55

1 Answers1

3

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

D'yer Mak'er
  • 1,632
  • 5
  • 24
  • 47