意图调用操作不适用于Marshmallow

时间:2016-05-02 19:24:33

标签: android android-intent android-permissions

我正在尝试在具有Marshmallow作为操作系统的设备上启动呼叫意图操作,使用与往常相同的步骤(这适用于以下版本):

添加权限:

<uses-permission android:name="android.permission.CALL_PHONE" />

打开意图:

Intent intent = new Intent(Intent.ACTION_CALL);
                    intent.setData(Uri.parse("tel:" + getString(R.string.connect_phone)));
                    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    startActivity(intent);

这是我得到的日志:

  

致命的例外:主要

     

进程:com.app.calling,PID:4250                                                                    java.lang.SecurityException:Permission Denial:启动Intent {   act = android.intent.action.CALL dat = tel:xxxxxxxxxxxxx flg = 0x10000000   CMP = com.android.server.telecom / .components.UserCallActivity   VirtualScreenParam = Params {mDisplayId = -1,null,mFlags = 0x00000000)}}   来自ProcessRecord {1618b01 4250:com.app.calling / u0a234}(pid = 4250,   uid = 10234)具有撤销权限android.permission.CALL_PHONE                                                                        在android.os.Parcel.readException(Parcel.java:1620)                                                                        在android.os.Parcel.readException(Parcel.java:1573)                                                                        在   android.app.ActivityManagerProxy.startActivity(ActivityManagerNative.java:3130)                                                                        在   android.app.Instrumentation.execStartActivity(Instrumentation.java:1540)                                                                        在android.app.Activity.startActivityForResult(Activity.java:4283)                                                                        在android.app.Activity.startActivityForResult(Activity.java:4230)                                                                        在   android.support.v4.app.FragmentActivity.startActivityFromFragment(FragmentActivity.java:849)                                                                        在   android.support.v4.app.FragmentActivity $ HostCallbacks.onStartActivityFromFragment(FragmentActivity.java:907)                                                                        在android.support.v4.app.Fragment.startActivity(Fragment.java:919)                                                                        在   com.app.calling.activity.fragment.ConnectFragment $ 2.onGroupClick(ConnectFragment.java:44)                                                                        在   android.widget.ExpandableListView.handleItemClick(ExpandableListView.java:676)                                                                        在   android.widget.ExpandableListView.performItemClick(ExpandableListView.java:654)                                                                        在android.widget.AbsListView $ PerformClick.run(AbsListView.java:3821)                                                                        在android.widget.AbsListView $ 3.run(AbsListView.java:5841)                                                                        在android.os.Handler.handleCallback(Handler.java:739)                                                                        在android.os.Handler.dispatchMessage(Handler.java:95)                                                                        在android.os.Looper.loop(Looper.java:158)                                                                        在android.app.ActivityThread.main(ActivityThread.java:7224)                                                                        at java.lang.reflect.Method.invoke(Native Method)                                                                        在   com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:1230)                                                                        在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)

同样,这个过程在之前的版本(Lollipop和kitkat)中工作得很好,遗憾的是不在Marshmallow上,是否有人知道为什么或我缺少什么?

3 个答案:

答案 0 :(得分:17)

  

拨打电话的方法

public void onCall() {
        int permissionCheck = ContextCompat.checkSelfPermission(this, Manifest.permission.CALL_PHONE);

        if (permissionCheck != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(
                    this,
                    new String[]{Manifest.permission.CALL_PHONE},
                    "123");
        } else {
            startActivity(new Intent(Intent.ACTION_CALL).setData(Uri.parse("tel:12345678901")));
        }
    }
  

检查权限

@Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        switch (requestCode) {

            case 123:
                if ((grantResults.length > 0) && (grantResults[0] == PackageManager.PERMISSION_GRANTED)) {
                    onCall();
                } else {
                    Log.d("TAG", "Call Permission Not Granted");
                }
                break;

            default:
                break;
        }
    }

答案 1 :(得分:5)

从android 6.0(API 23)开始,必须在清单 AND 中声明危险权限,您必须明确请求该用户的许可。根据{{​​3}}列表,CALL_PHONE被视为危险许可。

每次执行需要危险权限的操作时,都必须检查用户是否已授予该权限。如果没有,您必须要求授予它。请参阅Android开发者上的this

答案 2 :(得分:1)

对于Marshmallow及以上版本,您需要在运行时不仅在清单文件中询问权限。这是文档:

Requesting Permissions at Run Time

希望它有所帮助。