3

在我的应用程序中,我需要在某些情况下以编程方式发起呼叫。

它大部分时间都有效,但我可以看到有时它不起作用,特别是在 2.3.7 版本上。也许该特定权限已删除并且在该版本中是非法的?我找不到有关异常起源的线索。

我在清单中获得了许可:

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

的java代码:

private void launchCall(String number, int calltype) {
            Intent callIntent = new Intent();
            callIntent.setAction(Intent.ACTION_CALL);
            callIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            callIntent.setData(Uri.parse("tel:"+ number));
            if (calltype == CALL_TYPE_VIDEO) {
                callIntent.setAction(ACTION_VIDEO_CALL);
                callIntent.putExtra("videocall", true);
            }
            context.startActivity(callIntent);
        }

异常跟踪:

java.lang.SecurityException: Permission Denial: starting Intent { act=android.intent.action.CALL dat=tel:xxx-xxx-xxxx flg=0x10000000 cmp=com.android.phone/.OutgoingCallBroadcaster } from ProcessRecord{415f7158 4016:com.tawkon/10092} (pid=4016, uid=10092) requires android.permission.CALL_PHONE
at android.os.Parcel.readException(Parcel.java:1322)
at android.os.Parcel.readException(Parcel.java:1276)
at android.app.ActivityManagerProxy.startActivity(ActivityManagerNative.java:1359)
at android.app.Instrumentation.execStartActivity(Instrumentation.java:1374)
at android.app.ContextImpl.startActivity(ContextImpl.java:640)
at android.content.ContextWrapper.startActivity(ContextWrapper.java:258)
at com.tawkon.Service.PrecallSuggestionManager.launchCall(PrecallSuggestionManager.java:305)
at com.tawkon.Service.PrecallSuggestionManager.access$12(PrecallSuggestionManager.java:291)
at com.tawkon.Service.PrecallSuggestionManager$6.run(PrecallSuggestionManager.java:215)
at android.os.Handler.handleCallback(Handler.java:587)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:130)
at android.app.ActivityThread.main(ActivityThread.java:3703)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
at dalvik.system.NativeStart.main(Native Method)
4

3 回答 3

0

您需要将其添加到Visual Studio IDECall permission中的应用程序manifest 的项目属性中。. 单击此处查看图像。

于 2018-05-11T15:24:46.207 回答
0

我有同样的问题,我发现权限不在正确的位置!将此复制到您的清单中:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.shalom.itai.automaticdailer" >
    <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="17" />
    <uses-permission android:name="android.permission.CALL_PHONE" />
    <uses-permission android:name="android.permission.READ_PHONE_STATE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
于 2016-04-21T09:21:40.673 回答
0

以下解决方案对我来说效果很好。

AndroidManifest.xml

<uses-sdk android:minSdkVersion="8" android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.CALL_PHONE" />

activity_main.xml

<EditText
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/phoneNumberEditTextId"/>
<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="make phone call"
    android:onClick="makePhoneCall"/>

MainActivity.java

 private static final int MY_PERMISSIONS_REQUEST_CALL_PHONE = 1;

 public void makePhoneCall(View view) {

        int permissionCheck = ContextCompat.checkSelfPermission(this, Manifest.permission.CALL_PHONE);
        if (permissionCheck != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CALL_PHONE}, MY_PERMISSIONS_REQUEST_CALL_PHONE);
        } else {
            callPhone();
        }

    }

    @Override
    public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
        switch (requestCode) {
            case MY_PERMISSIONS_REQUEST_CALL_PHONE: {
                if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    callPhone();
                }
            }
        }
    }

    private void callPhone() {
        EditText editText = (EditText) findViewById(R.id.phoneNumberEditTextId);
        Intent intent = new Intent(Intent.ACTION_CALL);
        intent.setData(Uri.parse("tel:" + editText.getText().toString()));
        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.CALL_PHONE) == PackageManager.PERMISSION_GRANTED) {
            startActivity(intent);
        }
    }
于 2016-08-19T06:11:21.570 回答