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);
            }
        });
spectrumsofttechAsked:
Who is Participating?
 
David Johnson, CD, MVPOwnerCommented:
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
1
All Courses

From novice to tech pro — start learning today.