Home > Software design >  call by phone number on button click (java)
call by phone number on button click (java)

Time:11-14

I have a call method on button click

button_call=(Button) findViewById(R.id.button_call);

button_call.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        Intent intent=new Intent(Intent.ACTION_DIAL);
        intent.setData(Uri.parse("tel: 79142214017"));
        startActivity(intent);
    }
});

And the data transfer method in Firebase

private void validateClientInfo() {
    if (TextUtils.isEmpty(client_edit.getText().toString())) {
        Toast.makeText(this, "Введите имя", Toast.LENGTH_SHORT).show();
    }

    if (TextUtils.isEmpty(client_number.getText().toString())){
        Toast.makeText(this, "Введите номер", Toast.LENGTH_SHORT).show();

    }
    else {
        HashMap<String, Object> userMap = new HashMap<>();
        userMap.put("uid", mAuth.getCurrentUser().getUid());          
                    userMap.put("numberphone_client",client_number.getText().toString());

     clientRef.child(mAuth.getCurrentUser().getUid()).updateChildren(userMap);

        startActivity(new Intent(ClientName.this,HomeActivity.class));
    }
}

transcripts

client_number=(EditText) findViewById(R.id.client_number);
mAuth=FirebaseAuth.getInstance();

how to make it so that when the call button is pressed, the number is received and called?

I want that when the button _call button is pressed, the data transmitted by the transfer method is received and a call is made on them.

CodePudding user response:

Request permission android.permission.CALL_PHONE before calling.

Add into AndroidManifest.xml: <uses-permission android:name="android.permission.CALL_PHONE" />

Request permission before calling:

boolean isPermissionGranted = (ContextCompat.checkSelfPermission(this, Manifest.permission.CALL_PHONE) == PackageManager.PERMISSION_GRANTED);

if (isPermissionGranted) {
    Intent intent = new Intent(Intent.ACTION_CALL);
    intent.setData(Uri.parse("tel:"   mNumber));
    
    startActivity(intent);
} else {
    Toast.makeText(this, "Отсутствует разрешение на звонок с устройства", Toast.LENGTH_SHORT).show();

    ActivityCompat.requestPermissions(this, new String[]{ Manifest.permission.CALL_PHONE }, 0);
}

Add request result:

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull @NotNull String[] permissions, @NonNull @NotNull int[] grantResults) {
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    if (requestCode == 0) {
        for (int result : grantResults) {
            if (result != PackageManager.PERMISSION_GRANTED) {
                //RETURN, PERMISSION NOT GRANTED

                Toast.makeText(this, "Вы не выдали разрешение, приложение может работать неккоректно!", Toast.LENGTH_SHORT).show();
                return;
            }
        }

        //PERMISSIONS GRANTED
        Toast.makeText(this, "Спасибо за выданное разрешение!", Toast.LENGTH_SHORT).show();
    }
}

CodePudding user response:

If your are looking to call the selected number then just use the following code.

Intent intentCallForward = new Intent(Intent.ACTION_DIAL); // ACTION_CALL
        Uri uri2 = Uri.fromParts("tel", "79142214017, "#");
        intentCallForward.setData(uri2);
        startActivityForResult(intentCallForward, 101);
  • Related