Android手机通话无效

时间:2016-12-27 09:54:33

标签: android phone-call

我只是想从我的应用程序中拨打电话。我尝试了其他一切。

我厌倦了互联网上的所有解决方案,包括mkyong,tutorialspoint等等。

phoneCalls.setOnClickListener(new View.OnClickListener() {
                        public void onClick(View arg0) {
                            Intent callIntent = new Intent(Intent.ACTION_CALL);
                            callIntent.setData(Uri.parse("tel:0377778888"));

                            if (ActivityCompat.checkSelfPermission(getActivity(),
                                    Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
                                return;
                            }
                            startActivity(callIntent);
                        }
                    });

在我的清单中,我问了许可:

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

上面的代码也没有显示任何错误。我再次尝试了这个

button.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {

                Intent callIntent = new Intent(Intent.ACTION_CALL);
                callIntent.setData(Uri.parse("tel:0377778888"));
                startActivity(callIntent);

            }

        });

    }

尝试此代码时,它会在logcat中显示以下错误:

java.lang.SecurityException: Permission Denial: starting Intent { act=android.intent.action.CALL dat=tel:xxxxxxxxxx cmp=com.android.server.telecom/.components.UserCallActivity } from ProcessRecord{80f03c 26994:com.example.user.home/u0a73} (pid=26994, uid=10073) with revoked permission android.permission.CALL_PHONE
                                                                           at android.os.Parcel.readException(Parcel.java:1620)
                                                                           at android.os.Parcel.readException(Parcel.java:1573)
                                                                           at android.app.ActivityManagerProxy.startActivity(ActivityManagerNative.java:2680)
                                                                           at android.app.Instrumentation.execStartActivity(Instrumentation.java:1509)
                                                                           at android.app.Activity.startActivityForResult(Activity.java:3930)
                                                                           at android.app.Activity.startActivityForResult(Activity.java:3890)
                                                                           at android.app.Activity.startActivity(Activity.java:4213)
                                                                           at android.app.Activity.startActivity(Activity.java:4181)
                                                                           at com.example.user.home.Main3Activity$1.onClick(Main3Activity.java:30)
                                                                           at android.view.View.performClick(View.java:5204)
                                                                           at android.view.View$PerformClick.run(View.java:21158)
                                                                           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:5461)
                                                                           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)

希望我在这里留下这个问题。有人请帮忙

1 个答案:

答案 0 :(得分:4)

因为您在Marshmallow 6.0上尝试它并且您没有为Marshmallow或Android版本设置Runtime权限。

您的代码将在6.0以下但不在6.0或更高版本中运行。

按此重新编写代码。

button.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0)
        {
            callPhoneNumber();
        }
    });
}

使用权限检查创建此调用方法

public void callPhoneNumber()
{
    try
    {
        if(Build.VERSION.SDK_INT > 22)
        {
            if (ActivityCompat.checkSelfPermission(this, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
                // TODO: Consider calling

                ActivityCompat.requestPermissions(TestActivity.this, new String[]{Manifest.permission.CALL_PHONE}, 101);

                return;
            }

            Intent callIntent = new Intent(Intent.ACTION_CALL);
            callIntent.setData(Uri.parse("tel:" + "0377778888"));
            startActivity(callIntent);

        }
        else {
            Intent callIntent = new Intent(Intent.ACTION_CALL);
            callIntent.setData(Uri.parse("tel:" + "0377778888"));
            startActivity(callIntent);
        }
    }
    catch (Exception ex)
    {
        ex.printStackTrace();
    }
}

当用户授予权限时,控制权来自此处

@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions,
                                          int[] grantResults)
{
    if(requestCode == 101)
    {
        if(grantResults[0] == PackageManager.PERMISSION_GRANTED)
        {
            callPhoneNumber();
        }
        else
        {
            Log.e(TAG, "Permission not Granted");
        }
    }
}