Link to home
Start Free TrialLog in
Avatar of spectrumsofttech
spectrumsofttechFlag for India

asked on

Phone Call in Android

I used following code for phone call.But the call is not working .can anyone help me? I also given manfest Call Phone permission.

 call = (TextView) findViewById(R.id.GuestPhoneNo1);
        call.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String no = call.getText().toString();
            //    Toast.makeText(Accepted_Details.this, no, Toast.LENGTH_SHORT).show();
                Intent callIntent = new Intent(Intent.ACTION_CALL);
                callIntent.setData(Uri.parse("tel:" + no));


                /*if (ActivityCompat.checkSelfPermission(Accepted_Details.this, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
                    // TODO: Consider calling
                    //    ActivityCompat#requestPermissions
                    // here to request the missing permissions, and then overriding
                    //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
                    //                                          int[] grantResults)
                    // to handle the case where the user grants the permission. See the documentation
                    // for ActivityCompat#requestPermissions for more details.
                    return;
                }*/
                startActivity(callIntent);
            }
        });
Avatar of David Johnson, CD
David Johnson, CD
Flag of Canada image

public class MainActivity extends AppCompatActivity {

private static final int MAKE_CALL_PERMISSION_REQUEST_CODE = 1;
private Button dial;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    dial = (Button) findViewById(R.id.dial);
    final EditText numberToDial = (EditText) findViewById(R.id.number);

    dial.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            String phoneNumber = numberToDial.getText().toString();

            if (!TextUtils.isEmpty(phoneNumber)) {
                if (checkPermission(Manifest.permission.CALL_PHONE)) {
                    String dial = "tel:" + phoneNumber;
                    startActivity(new Intent(Intent.ACTION_CALL, Uri.parse(dial)));
                } else {
                    Toast.makeText(MainActivity.this, "Permission Call Phone denied", Toast.LENGTH_SHORT).show();
                }
            } else {
                Toast.makeText(MainActivity.this, "Enter a phone number", Toast.LENGTH_SHORT).show();
            }
         }
    });

    if (checkPermission(Manifest.permission.CALL_PHONE)) {
        dial.setEnabled(true);
    } else {
        dial.setEnabled(false);
        ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CALL_PHONE}, MAKE_CALL_PERMISSION_REQUEST_CODE);
    }
}

private boolean checkPermission(String permission) {
    return ContextCompat.checkSelfPermission(this, permission) == PackageManager.PERMISSION_GRANTED;
}

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    switch(requestCode) {
        case MAKE_CALL_PERMISSION_REQUEST_CODE :
            if (grantResults.length > 0 && (grantResults[0] == PackageManager.PERMISSION_GRANTED)) {
                dial.setEnabled(true);
                Toast.makeText(this, "You can call the number by clicking on the button", Toast.LENGTH_SHORT).show();
            }
            return;
    }
}
}

Open in new window

source: https://stackoverflow.com/questions/50386017/phone-call-in-android
This question needs an answer!
Become an EE member today
7 DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform.
View membership options
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.