我要做什么:我想打开列表中带有电话号码的拨号程序。
问题出在哪里::在激发Intent.ACTION_CALL
的意图之前,我需要征求许可,看来onRequestPermissionsResult
没有得到呼叫。
我尝试过的操作:下面是我的代码。
ApplicantsDataAdapter.java
public class ApplicantsDataAdapter extends RecyclerView.Adapter<ApplicantsDataAdapter.ApplicantsViewHolder>
implements
ActivityCompat.OnRequestPermissionsResultCallback {
private static final int REQUEST_CODE = 100;
private Context mContext;
private String mMobileNum;
private List<DashboardStatusBean> mDataList;
private OnApplicantItemClickListener mListener;
public ApplicantsDataAdapter(Context context, List<DashboardStatusBean> dataList, OnApplicantItemClickListener listener) {
mListener = listener;
mContext = context;
mDataList = dataList;
}
@Override
public ApplicantsViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.item_applicant_new, parent, false);
return new ApplicantsViewHolder(itemView);
}
@Override
public void onBindViewHolder(ApplicantsViewHolder holder, int position) {
holder.bindView(position);
holder.mCardView.setOnClickListener(v -> mListener.onApplicantSelected(position));
}
@Override
public int getItemCount() {
return mDataList.size();
}
public interface OnApplicantItemClickListener {
void onApplicantSelected(int position);
}
public class ApplicantsViewHolder extends RecyclerView.ViewHolder {
@BindView(R.id.cv_item)
CardView mCardView;
@BindView(R.id.tv_item_applicant_status)
TextView mApplicantStatusTextView;
@BindView(R.id.tv_item_applicant_mobile)
TextView mApplicantMobileNumber;
@BindView(R.id.iv_call)
ImageView mCallImage;
ApplicantsViewHolder(View itemView) {
super(itemView);
ButterKnife.bind(this, itemView);
}
void bindView(int position) {
mApplicantStatusTextView.setText(mDataList.get(position).getStatus());
mApplicantMobileNumber.setText(mDataList.get(position).getMobileNumber());
mCallImage.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
mMobileNum = mDataList.get(position).getMobileNumber().substring(2);
Log.e("click", "mob: " + mMobileNum);
phoneIntent();
}
});
}
}
private void phoneIntent() {
Intent callIntent = new Intent(Intent.ACTION_CALL);
callIntent.setData(Uri.parse("tel:" + mMobileNum));
if (ActivityCompat.checkSelfPermission(mContext, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
requestPermissions((AppCompatActivity) mContext,
new String[]{Manifest.permission.CALL_PHONE}, REQUEST_CODE);
return;
}
mContext.startActivity(callIntent);
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
Log.e("click", "onRequestPermissionsResult");
switch (requestCode) {
case REQUEST_CODE:
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Log.e("click", "PERMISSION_GRANTED");
phoneIntent();
} else {
Log.e("click", "NOT PERMISSION_GRANTED");
Toast.makeText(mContext, "Call Permission Not Granted!", Toast.LENGTH_SHORT).show();
}
break;
}
}
}
PS:我检查了三遍是否在清单文件中添加了权限<uses-permission android:name="android.permission.CALL_PHONE" />
。
任何帮助将不胜感激。
答案 0 :(得分:2)
我认为该电话不需要任何运行时权限
您只需要像下面这样指定对清单的权限
<uses-permission android:name="android.permission.CALL_PHONE" />
在代码中,您需要编写以下代码行以打开用于拨打任何号码而付费的拨号程序。
String mMobileNum = mDataList.get(position).getMobileNumber().substring(2);
Log.e("click", "mob: " + mMobileNum);
startActivity(new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" +mMobileNum)));