4

i'm trying to make a phone call from the app

and I want it to return back to the app after the call

i asked that question in this forum but i didn't understand the answer

How to make a phone call in android and come back to my activity when the call is done?

public void call() {
        try {
            Intent callIntent = new Intent(Intent.ACTION_CALL);
            callIntent.setData(Uri.parse("tel:048598077"));
            getContext().startActivity(callIntent);

        } catch (ActivityNotFoundException activityException) {
            Log.e("dialing-example", "Call failed", activityException);}
        finally {           
            EndCallListener callListener = new EndCallListener();
            TelephonyManager mTM = (TelephonyManager)getContext().getSystemService(Context.TELEPHONY_SERVICE);
            mTM.listen(callListener, PhoneStateListener.LISTEN_CALL_STATE);
        }
    }



    private class EndCallListener extends PhoneStateListener {
        @Override
        public void onCallStateChanged(int state, String incomingNumber) {
            if(TelephonyManager.CALL_STATE_RINGING == state) {
                Log.i(LOG_TAG, "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.
                Log.i(LOG_TAG, "OFFHOOK");
            }
            if(TelephonyManager.CALL_STATE_IDLE == state) {
                //when this state occurs, and your flag is set, restart your app
                Log.i(LOG_TAG, "IDLE");
            }
        }

i understand some thing like that, but i didn't start the CallListener at any point. So how can i do that ?

Community
  • 1
  • 1
igor
  • 716
  • 1
  • 9
  • 27

2 Answers2

8

ok , I found the answer here is the code :

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

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


        // add button listener
        button.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {

                call();

            }

        });

    }

    private void call()
    {
        PhoneCallListener phoneListener = new PhoneCallListener();
        TelephonyManager telephonyManager = (TelephonyManager) this
                .getSystemService(Context.TELEPHONY_SERVICE);
        telephonyManager.listen(phoneListener,
                PhoneStateListener.LISTEN_CALL_STATE);

        Intent callIntent = new Intent(Intent.ACTION_CALL);
        callIntent.setData(Uri.parse("tel:0377778888"));
        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
                    Intent i = getBaseContext().getPackageManager()
                            .getLaunchIntentForPackage(
                                    getBaseContext().getPackageName());
                    i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                    startActivity(i);

                    isPhoneCalling = false;
                }

            }
        }
    }
}

and in the manufest we need to add

<uses-permission android:name="android.permission.CALL_PHONE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
igor
  • 716
  • 1
  • 9
  • 27
1

I don't really know how your app is supposed to work,but if you have your phone number in a text view ,just set(in your .xml file) the property: android:autoLink="phone" and everything will work just fine.(On click you will be able to make a phone call and when you hit end call it will return to your activity)

EDIT:If you want to do it automatically then you should use an intent.try this: how to make phone call using intent in android?

Community
  • 1
  • 1
Roman Marius
  • 456
  • 3
  • 9
  • 21
  • i put it in the manifest.xml and it's still stais on call log – igor Nov 13 '12 at 11:37
  • you don't need to put it in the manifest.xml. Put it in the .xml file where you have the text view that contains the number you want to call.Here you have an example: – Roman Marius Nov 13 '12 at 11:58
  • am I doing it right ? main .xml JAVA FILE private void call() { try { Intent callIntent = new Intent(Intent.ACTION_CALL); callIntent.setData(Uri.parse("tel:xxxxxxx")); startActivity(callIntent); } catch (ActivityNotFoundException activityException) { Log.e("dialing-example", "Call failed", activityException); } – igor Nov 13 '12 at 15:56
  • you don't need the intent anymore.put the phone number as text in your text view and the property android:autoLink="phone" will do all the work for you. – Roman Marius Nov 14 '12 at 07:25
  • I understand it's still didn't send me back to my app.. and it diden't ring only send me to the "phone" with the number – igor Nov 14 '12 at 07:55
  • and where it sends you after you hit back,or end call? – Roman Marius Nov 14 '12 at 08:26
  • i am controling my app from anoder device so i want to do it like : when something is going on there it will make a call, but it's send me to the call screen and didn't do it otomaticle if I i call and press "end call" button it send me to the call log screen – igor Nov 14 '12 at 09:02