How to call a contact on swipe right? How to fix the error android studio is telling me about?

Viewed 78


I'm doing an agenda application that has a recycler view with contacts.
I want to add functionality to CALL A CONTACT on swiping right.

I looked at previous questions on StackOverflow, but after doing what has been answered there, Androi Studio still brings me an error -> Code requires permission which can be rejected by the user...


        //CALL A CONTACT ON SWIPE RIGHT
        new ItemTouchHelper(new ItemTouchHelper.SimpleCallback(0, ItemTouchHelper.RIGHT ) {
            @Override
            public boolean onMove(@NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder, @NonNull RecyclerView.ViewHolder target) {
                return false;
            }

            @Override
            public void onSwiped(@NonNull RecyclerView.ViewHolder viewHolder, int direction) {
                //contactViewModel.delete(contactAdapter.getContactAt(viewHolder.getAdapterPosition()));

                Intent callIntent = new Intent(Intent.ACTION_CALL);
                callIntent.setData(Uri.parse("tel:" + contactAdapter.getContactAt(viewHolder.getAdapterPosition()).getPhone_number()));
                startActivity(callIntent);
            }
        }).attachToRecyclerView(recyclerView);


How can I manage to fix that, I guess I need a few more lines of code.
Worth to mention that I have added the

<uses-permission android:name="android.permission.CALL_PHONE"/>

inside of AndroidManifest.XML

1 Answers

Add check permission before call startActivity() like this:

Intent callIntent = new Intent(Intent.ACTION_CALL);
callIntent.setData(Uri.parse("tel:" + contactAdapter.getContactAt(viewHolder.getAdapterPosition()).getPhone_number()));
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
   return;
}
startActivity(callIntent);
Related