首页 > 代码库 > android 调用拨打电话,程序崩溃
android 调用拨打电话,程序崩溃
FATAL EXCEPTION: main
03-27 17:43:08.012: E/AndroidRuntime(18523): Process: com.mtq.freighthelper, PID: 18523
03-27 17:43:08.012: E/AndroidRuntime(18523): java.lang.SecurityException: Permission Denial: starting Intent { act=android.intent.action.CALL dat=tel:xxxx-xxxxxxxx cmp=com.android.server.telecom/.components.UserCallActivity } from ProcessRecord{1f529f5 18523:com.mtq.freighthelper/u0a491} (pid=18523, uid=10491) with revoked permission android.permission.CALL_PHONE
03-27 17:43:08.012: E/AndroidRuntime(18523): at android.os.Parcel.readException(Parcel.java:1620)
03-27 17:43:08.012: E/AndroidRuntime(18523): at android.os.Parcel.readException(Parcel.java:1573)
03-27 17:43:08.012: E/AndroidRuntime(18523): at android.app.ActivityManagerProxy.startActivity(ActivityManagerNative.java:2681)
03-27 17:43:08.012: E/AndroidRuntime(18523): at android.app.Instrumentation.execStartActivity(Instrumentation.java:1509)
03-27 17:43:08.012: E/AndroidRuntime(18523): at android.app.Activity.startActivityForResult(Activity.java:3978)
03-27 17:43:08.012: E/AndroidRuntime(18523): at android.app.Activity.startActivityForResult(Activity.java:3939)
03-27 17:43:08.012: E/AndroidRuntime(18523): at android.app.Activity.startActivity(Activity.java:4262)
03-27 17:43:08.012: E/AndroidRuntime(18523): at android.app.Activity.startActivity(Activity.java:4230)
03-27 17:43:08.012: E/AndroidRuntime(18523): at com.mtq.freighthelper.ui.activity.contacts.ContactsDetailActivity.onClick(ContactsDetailActivity.java:97)
03-27 17:43:08.012: E/AndroidRuntime(18523): at android.view.View.performClick(View.java:5207)
03-27 17:43:08.012: E/AndroidRuntime(18523): at android.view.View$PerformClick.run(View.java:21177)
03-27 17:43:08.012: E/AndroidRuntime(18523): at android.os.Handler.handleCallback(Handler.java:739)
03-27 17:43:08.012: E/AndroidRuntime(18523): at android.os.Handler.dispatchMessage(Handler.java:95)
03-27 17:43:08.012: E/AndroidRuntime(18523): at android.os.Looper.loop(Looper.java:148)
03-27 17:43:08.012: E/AndroidRuntime(18523): at android.app.ActivityThread.main(ActivityThread.java:5438)
03-27 17:43:08.012: E/AndroidRuntime(18523): at java.lang.reflect.Method.invoke(Native Method)
03-27 17:43:08.012: E/AndroidRuntime(18523): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:739)
03-27 17:43:08.012: E/AndroidRuntime(18523): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:629)
使用设备:小米4
android: 6.0
程序报java.lang.SecurityException: Permission Denial: starting Intent { act=android.intent.action.CALL dat=tel:xxxx-xxxxxxxx cmp=com.android.server.telecom/.components.UserCallActivity } from ProcessRecord{1f529f5 18523:com.mtq.freighthelper/u0a491} (pid=18523, uid=10491) with revoked permission
原因:
在6.0 系统中请求某些权限需要检查权限
1 if (!TextUtils.isEmpty(phone)) { 2 if (!hasPermission()) { 3 // 在6.0 系统中请求某些权限需要检查权限 4 int curApiVersion = Build.VERSION.SDK_INT; 5 if (curApiVersion >= Build.VERSION_CODES.M) { 6 // 动态请求拨打电话权限 7 requestPermissions( 8 new String[] { Manifest.permission.CALL_PHONE }, 9 0x11); 10 } else { 11 intentToCall(phone); 12 } 13 } else { 14 intentToCall(phone); 15 } 16 }
1 private boolean hasPermission() { 2 if (checkSelfPermission(Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) { 3 return false; 4 } 5 return true; 6 } 7 8 private void intentToCall(String phoneNumber) { 9 Intent intent = new Intent(Intent.ACTION_CALL); 10 Uri data = http://www.mamicode.com/Uri.parse("tel:" + phoneNumber); 11 intent.setData(data); 12 startActivity(intent); 13 } 14 15 16 /** 17 * 动态请求拨打电话权限后,监听用户的点击事件 18 */ 19 @Override 20 public void onRequestPermissionsResult(int requestCode, 21 String[] permissions, int[] grantResults) { 22 super.onRequestPermissionsResult(requestCode, permissions, grantResults); 23 if (requestCode == 0x11) { 24 // If request is cancelled, the result arrays are empty. 25 if (grantResults.length > 0 26 && grantResults[0] == PackageManager.PERMISSION_GRANTED) { 27 CldLog.i("CMCC", "权限被允许"); 28 String phone = mContactsInfo.getPhone(); 29 intentToCall(phone); 30 } else { 31 CldLog.i("CMCC", "权限被拒绝"); 32 } 33 } 34 }
android 调用拨打电话,程序崩溃