首先我调了个打电话的权限,果然报错.哈哈.顺利入坑..要想跳出坑就要慢慢的看百度.资料了...
下面看看我怎么调出去的...
第一步,我们还是要在AndroidManifolest.xml里面添加
android:name="android.permission.CALL_PHONE" />
如果你没有判断请求权限会报以下错误
AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.jie.demo, PID: 17959
java.lang.SecurityException: Permission Denial: starting Intent { act=android.intent.action.CALL dat=tel:xxxxxxxxxx cmp=com.android.server.telecom/.components.UserCallActivity } from ProcessRecord{5d8891e 17959:com.example.jie.demo/u0a63} (pid=17959, uid=10063) with revoked permission android.permission.CALL_PHONE
at android.os.Parcel.readException(Parcel.java:1599)
at android.os.Parcel.readException(Parcel.java:1552)
at android.app.ActivityManagerProxy.startActivity(ActivityManagerNative.java:2658)
at android.app.Instrumentation.execStartActivity(Instrumentation.java:1507)
at android.app.Activity.startActivityForResult(Activity.java:3917)
at android.app.Activity.startActivityForResult(Activity.java:3877)
at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:784)
at android.app.Activity.startActivity(Activity.java:4200)
at android.app.Activity.startActivity(Activity.java:4168)
at com.example.jie.demo.ui.MainActivity.onClick(MainActivity.java:85)
at android.view.View.performClick(View.java:5198)
at android.view.View$PerformClick.run(View.java:21147)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
入坑了...回想下..我明明有在AndroidManifeast.xml里面添加了拨打电话的权限,为什么还报错呢??
启动拨打电话.
//在6.0 系统中请求某些权限需要检查权限
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) { // TODO: Consider calling // ActivityCompat#requestPermissions // here to request the missing permissions, and then overriding // public void onRequestPermissionsResult(int requestCode, String[] permissions, // int[] grantResults) // to handle the case where the user grants the permission. See the documentation // for ActivityCompat#requestPermissions for more details. //在这里面向系统请求权限,如果没有在这里面处理,不会执行下面的方法了 //这里就是向系统请求权限了,这里我还做了一个判断. sdk是M(M = 23 android L)才做这个请求,否则就不做.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { requestPermissions(new String[]{"android.permission.CALL_PHONE"}, 111); }return;}
startActivity(new Intent(Intent.ACTION_CALL, Uri.parse("tel:0123456789")));
@Override public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { if (requestCode == 111) { if (grantResults[0] == PackageManager.PERMISSION_GRANTED) { L.e("ok");//代表用户同意了打电话的请求 }else{ L.e("no"); } } super.onRequestPermissionsResult(requestCode, permissions, grantResults); /**输出的日志 * E/TAG:: ok E/TAG:: permissions=android.permission.CALL_PHONE E/TAG:: grantResults=0 E/TAG:: requestCode=111 */ for (int i = 0; i < permissions.length; i++) { L.e("permissions=" + permissions[i]); } for (int i = 0; i < grantResults.length; i++) { L.e("grantResults=" + grantResults[i]); } L.e("requestCode=" + requestCode); }