-2

I have a recycler view and want to call to a phonenumber written in edittext holder.textPhone. However when calling Intent.Action_Call, app keeps stopping. What is the error in my code ?

Here is my userAdapter class which contains the recyclerview.

 package com.example.mher.citygo;

import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.support.v4.app.ActivityCompat;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import java.util.List;

public class UserAdapter extends 
RecyclerView.Adapter<UserAdapter.UserViewHolder> {

private List<UserModel> list;

public UserAdapter(List<UserModel> list) {
    this.list = list;
}

@Override
public UserViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    return new UserViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.view_item, parent, false));
}

@Override
public void onBindViewHolder(final UserViewHolder holder, int position) {

    UserModel user = list.get(position);
    holder.textFrom.setText(user.from);
    holder.textTo.setText(user.to);
    holder.textDate.setText(user.date);
    holder.textPhone.setText(user.phone);
    holder.textPhone.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Context context = v.getContext();
            Intent intent = new Intent(Intent.ACTION_CALL);
            if (ActivityCompat.checkSelfPermission(context, android.Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
                context.startActivity(intent);
                return;
            }


        }
    });


}

@Override
public int getItemCount() {
    return list.size();
}

class UserViewHolder extends RecyclerView.ViewHolder
{
    TextView textFrom,textTo,textDate,textPhone;

    public UserViewHolder(View itemView) {
        super(itemView);
        textFrom=(TextView) itemView.findViewById(R.id.text_from);
        textTo=(TextView) itemView.findViewById(R.id.text_to);
        textDate=(TextView) itemView.findViewById(R.id.text_date);
        textPhone=(TextView) itemView.findViewById(R.id.phonenumber);
    }
}
}

LOGCAT

  07-19 15:17:08.500 30009-30009/com.example.mher.citygo E/AndroidRuntime: FATAL EXCEPTION: main
                                                                         Process: com.example.mher.citygo, PID: 30009
                                                                         java.lang.SecurityException: Permission Denial: starting Intent { act=android.intent.action.CALL dat=tel:xxxxxxx.xxxxxxx.xx.xxxxxx.xxxxxxxxxxxxxxxxxxxxxxxxxxx.xx..x..x...x....xxxxxxx-xxxxxxxxx cmp=com.android.server.telecom/.components.UserCallActivity } from ProcessRecord{b536158 30009:com.example.mher.citygo/u0a74} (pid=30009, uid=10074) with revoked permission android.permission.CALL_PHONE
                                                                             at android.os.Parcel.readException(Parcel.java:1684)
                                                                             at android.os.Parcel.readException(Parcel.java:1637)
                                                                             at android.app.ActivityManagerProxy.startActivity(ActivityManagerNative.java:3101)
                                                                             at android.app.Instrumentation.execStartActivity(Instrumentation.java:1518)
                                                                             at android.app.Activity.startActivityForResult(Activity.java:4225)
                                                                             at android.support.v4.app.BaseFragmentActivityJB.startActivityForResult(BaseFragmentActivityJB.java:50)
                                                                             at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:79)
                                                                             at android.app.Activity.startActivityForResult(Activity.java:4183)
                                                                             at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:859)
                                                                             at android.app.Activity.startActivity(Activity.java:4522)
                                                                             at android.app.Activity.startActivity(Activity.java:4490)
                                                                             at com.example.mher.citygo.UserAdapter$1.onClick(UserAdapter.java:45)
                                                                             at android.view.View.performClick(View.java:5637)
                                                                             at android.view.View$PerformClick.run(View.java:22429)
                                                                             at android.os.Handler.handleCallback(Handler.java:751)
                                                                             at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                             at android.os.Looper.loop(Looper.java:154)
                                                                             at android.app.ActivityThread.main(ActivityThread.java:6119)
                                                                             at java.lang.reflect.Method.invoke(Native Method)
                                                                             at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
                                                                             at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)

3 Answers3

1

You added startActivity at wrong place, add it after if condition change this lines

holder.textPhone.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        Context context = v.getContext();
        Intent intent = new Intent(Intent.ACTION_CALL,  Uri.parse("tel:" + "1324567890"));
        if (ActivityCompat.checkSelfPermission(context, android.Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
           Toast.makeText(this, "permission not granted", Toast.LENGTH_SHORT).show();
            ActivityCompat.requestPermissions(this,
              new String[]{Manifest.permission.CALL_PHONE},143);
        }else{
          context.startActivity(intent);
           }


    }
});
AskNilesh
  • 67,701
  • 16
  • 123
  • 163
0

Calling startActivity() on v.getContext() seems to be causing the error here. Try adding following flag to your intent before calling startActivity():

intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Shaishav
  • 5,282
  • 2
  • 22
  • 41
0

Check this out

 Intent callIntent = new Intent(Intent.ACTION_CALL); //use ACTION_CALL class
 callIntent.setData(Uri.parse("tel:" + user.phone));    //this is the phone number calling
      //check permission
      //If the device is running Android 6.0 (API level 23) and the app's targetSdkVersion is 23 or higher,
      //the system asks the user to grant approval.
 if (ActivityCompat.checkSelfPermission(this, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
          //request permission from user if the app hasn't got the required permission
          ActivityCompat.requestPermissions(this,
                  new String[]{Manifest.permission.CALL_PHONE},   //request specific permission from user
                  10);
          return;
 }else {     //have got permission
          try{
              startActivity(callIntent);  //call activity and make phone call
          }
          catch (android.content.ActivityNotFoundException ex){
              Toast.makeText(getApplicationContext(),"yourActivity is not founded",Toast.LENGTH_SHORT).show();
          }
 }