这是我的热线片段代码。问题是它无法拨打电话。出现错误提示,如下所示:
错误:E / AndroidRuntime:致命异常:主要 流程:com.example.julieridon.kabitenyo,PID:16445 java.lang.SecurityException:权限拒绝:启动Intent {act = android.intent.action.CALL dat = tel:xxx cmp = android / com.android.internal.app.ResolverActivity}来自 ProcessRecord {4bd5fc6 16445:com.example.julieridon.kabitenyo / u0a0} (pid = 16445,uid = 10000)具有撤消的权限 android.permission.CALL_PHONE 在android.os.Parcel.readException(Parcel.java:2021) 在android.os.Parcel.readException(Parcel.java:1967) 在android.app.IActivityManager $ Stub $ Proxy.startActivity(IActivityManager.java:4822) 在android.app.Instrumentation.execStartActivity(Instrumentation.java:1619) 在android.app.Activity.startActivityForResult(Activity.java:4621) 在android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:767) 在android.app.Activity.startActivityForResult(Activity.java:4579) 在android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:754) 在android.app.Activity.startActivity(Activity.java:4940) 在android.app.Activity.startActivity(Activity.java:4908) 在com.example.julieridon.kabitenyo.HotlinesActivity $ 1.onClick(HotlinesActivity.java:31) 在android.view.View.performClick(View.java:6935) 在android.widget.TextView.performClick(TextView.java:12742) 在android.view.View $ PerformClick.run(View.java:26211) 在android.os.Handler.handleCallback(Handler.java:790) 在android.os.Handler.dispatchMessage(Handler.java:99) 在android.os.Looper.loop(Looper.java:164) 在android.app.ActivityThread.main(ActivityThread.java:7000) 在java.lang.reflect.Method.invoke(本机方法) 在com.android.internal.os.RuntimeInit $ MethodAndArgsCaller.run(RuntimeInit.java:441) 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1408)
热线片段代码:
public class HotlinesActivity extends Fragment {
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.activity_hotlines, container, false);
Button btnFragments = (Button) view.findViewById(R.id.call);
btnFragments.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent callIntent = new Intent(Intent.ACTION_CALL);
callIntent.setData(Uri.parse("tel:" + "123"));
getActivity().startActivity(callIntent);
}
});
return view;
}
}
答案 0 :(得分:0)
对于通话,您需要在AndroidManifest.xml中添加CALL_PHONE权限,例如
<uses-permission android:name="android.permission.CALL_PHONE"/>
Android有两种权限,一种是普通权限,另一种是 DANGEROUS 权限。 在Android 5.1.1及更低版本中,所有类型的权限都会自动授予。但是在Android 5.1.1及更高版本中,如果权限属于 DANGEROUS 类型,则我们需要要求用户授予权限。这样您就可以要求CALL许可
if (ContextCompat.checkSelfPermission(getContext(), Manifest.permission.CALL_PHONE) == PackageManager.PERMISSION_GRANTED) {
Intent callIntent = new Intent(Intent.ACTION_CALL);
callIntent.setData(Uri.parse("tel:" + "123"));
getActivity().startActivity(callIntent);
} else {
ActivityCompat.requestPermissions(getActivity(), new String[]{Manifest.permission.CALL_PHONE},
CALL_PERMISSION_REQUEST_CODE);
}
您可以使用 onRequestPermissionsResult 方法(如
)检查授予请求的结果@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == CALL_PERMISSION_REQUEST_CODE && grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Toast.makeText(getContext(), "Permission Granted", Toast.LENGTH_SHORT).show();
}
}
完整代码可能如下
public class YourFragment extends Fragment {
private static final int CALL_PERMISSION_REQUEST_CODE = 1234;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.activity_hotlines, container, false);
Button btnFragments = (Button) view.findViewById(R.id.call);
btnFragments.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
call();
}
});
return view;
}
void call() {
if (ContextCompat.checkSelfPermission(getContext(), Manifest.permission.CALL_PHONE) == PackageManager.PERMISSION_GRANTED) {
Intent callIntent = new Intent(Intent.ACTION_CALL);
callIntent.setData(Uri.parse("tel:" + "123"));
getActivity().startActivity(callIntent);
} else {
ActivityCompat.requestPermissions(getActivity(), new String[]{Manifest.permission.CALL_PHONE},
CALL_PERMISSION_REQUEST_CODE);
}
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == CALL_PERMISSION_REQUEST_CODE && grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Toast.makeText(getContext(), "Permission Granted", Toast.LENGTH_SHORT).show();
}
}
}