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

后端 未结 20 2152
北恋
北恋 2020-11-22 12:59

I am launching an activity to make a phone call, but when I pressed the \'end call\' button, it does not go back to my activity. Can you please tell me how can I launch a c

20条回答
  •  粉色の甜心
    2020-11-22 13:19

    Here is my example, first the user gets to write in the number he/she wants to dial and then presses a call button and gets directed to the phone. After call cancelation the user gets sent back to the application. In order to this the button needs to have a onClick method ('makePhoneCall' in this example) in the xml. You also need to register the permission in the manifest.

    Manifest

    
    
    

    Activity

    import android.net.Uri;
    import android.os.Bundle;
    import android.app.Activity;
    import android.content.Intent;
    import android.view.View;
    import android.widget.EditText;
    import android.widget.Toast;
    
    public class PhoneCall extends Activity {
    
        EditText phoneTo;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_phone_call);
    
            phoneTo = (EditText) findViewById(R.id.phoneNumber);
    
        }
        public void makePhoneCall(View view) {
    
    
    
    
            try {
                String number = phoneTo.getText().toString();
                Intent phoneIntent = new Intent(Intent.ACTION_CALL);
                phoneIntent.setData(Uri.parse("tel:"+ number));
                startActivity(phoneIntent);
    
    
            } catch (android.content.ActivityNotFoundException ex) {
                Toast.makeText(PhoneCall.this,
                        "Call failed, please try again later!", Toast.LENGTH_SHORT).show();
            }
        }
    
    }
    

    XML

     
    
        

提交回复
热议问题