Android 6.0 請求撥打電話error
http://blog.csdn.net/qq541159192/article/details/50570785 支援原創
今天,在網上看到6.0許可權大坑,,,,就想試試跳一下..
首先我調了個打電話的許可權,果然報錯.哈哈.順利入坑..要想跳出坑就要慢慢的看百度.資料了...
下面看看我怎麼調出去的...
第一步,我們還是要在AndroidManifolest.xml裡面新增
<uses-permission 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); }我只做了這一個請求,,其他的應該也是這般處理..歡迎交流指正!謝謝.