1、Demo代码
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/main_call"
android:text="打电话给10086"/>
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
requestPermission();
initView();
initListener();
Button call= findViewById(R.id.main_call);
call.setOnClickListener(view ->{
Intent intent = new Intent();
intent.setAction("android.intent.action.CALL");
intent.addCategory(Intent.CATEGORY_DEFAULT);
Uri uri = Uri.parse("tel:10086");
intent.setData(uri);
startActivity(intent);
});
}
2、Error详情
错误原因:此例子没有android.permission.CALL_PHONE权限
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.activityexample, PID: 30711
java.lang.SecurityException: Permission Denial: starting Intent { act=android.intent.action.CALL cat=[android.intent.category.DEFAULT] dat=tel:xxxxx cmp=com.android.server.telecom/.components.UserCallActivity } from ProcessRecord{6c3a921 30711:com.example.activityexample/u0a163} (pid=30711, uid=10163) requires android.permission.CALL_PHONE
at android.os.Parcel.createExceptionOrNull(Parcel.java:2373)
at android.os.Parcel.createException(Parcel.java:2357)
at android.os.Parcel.readException(Parcel.java:2340)
at android.os.Parcel.readException(Parcel.java:2282)
at android.app.IActivityTaskManager$Stub$Proxy.startActivity(IActivityTaskManager.java:3696)
at android.app.Instrumentation.execStartActivity(Instrumentation.java:1723)
at android.app.Activity.startActivityForResult(Activity.java:5320)
at androidx.fragment.app.FragmentActivity.startActivityForResult(FragmentActivity.java:676)
at android.app.Activity.startActivityForResult(Activity.java:5278)
at androidx.fragment.app.FragmentActivity.startActivityForResult(FragmentActivity.java:663)
at android.app.Activity.startActivity(Activity.java:5664)
at android.app.Activity.startActivity(Activity.java:5617)
at com.example.activityexample.MainActivity.lambda$onCreate$0$MainActivity(MainActivity.java:48)
at com.example.activityexample.-$$Lambda$MainActivity$sYWcUfxSFIDWB8mix6ozqxjQX_w.onClick(Unknown Source:2)
at android.view.View.performClick(View.java:7448)
at com.google.android.material.button.MaterialButton.performClick(MaterialButton.java:992)
at android.view.View.performClickInternal(View.java:7425)
at android.view.View.access$3600(View.java:810)
at android.view.View$PerformClick.run(View.java:28305)
at android.os.Handler.handleCallback(Handler.java:938)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:223)
at android.app.ActivityThread.main(ActivityThread.java:7656)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
Caused by: android.os.RemoteException: Remote stack trace:
at com.android.server.wm.ActivityStackSupervisor.checkStartAnyActivityPermission(ActivityStackSupervisor.java:1032)
at com.android.server.wm.ActivityStarter.executeRequest(ActivityStarter.java:999)
at com.android.server.wm.ActivityStarter.execute(ActivityStarter.java:669)
at com.android.server.wm.ActivityTaskManagerService.startActivityAsUser(ActivityTaskManagerService.java:1100)
at com.android.server.wm.ActivityTaskManagerService.startActivityAsUser(ActivityTaskManagerService.java:1072)
I/Process: Sending signal. PID: 30711 SIG: 9
3、修正错误
<uses-permission android:name="android.permission.CALL_PHONE"/>
同时需要注意版本大于Android 6.0时,除了添加在AndroidManifest.xml中添加uses-permission,还需要手动请求相应的权限。
private void requestPermission() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M){
requestPermissions(new String[]{"android.permission.CALL_PHONE"},111);
}
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == 111){
if (grantResults[0] == PackageManager.PERMISSION_GRANTED){
Log.d(TAG,"恭喜你,权限获取成功");
}else {
Log.d(TAG,"对不起,当前权限被拒绝");
}
}
}
仅为学习过程中的记录,方便自己以后查阅,也希望能帮助到每一位查阅者。其它权限详情均可参考Android Developer中的详细介绍。 参考文献地址:https://developer.android.google.cn/training/permissions/requesting#principles
|