0

我列出了手机中的所有联系人,并直接从我的 android 应用程序调用 viber 意图打开 viber 应用程序。我在网上找到了一些示例,但所有示例都太旧了,可能是几年前的事了,而且没有一个对我有用。

Uri uri = getUriFromPhoneNumber(phoneNumber, context);
    if (uri != null) {

        Intent localIntent4 = new Intent("com.viber.voip.action.CALL");
        localIntent4.setType("vnd.android.cursor.item/vnd.com.viber.voip.call");
        localIntent4.setData(Uri.parse("tel:" + uri));
        startActivity(localIntent4);

        }else {
        Toast.makeText(context, "Number is not in Viber Contacts List", Toast.LENGTH_LONG).show();
    }

我在我的 androidmanifest.xml 文件中包含了 read_contacts 权限。但在此总是引发安全异常。我推荐的一个解决方案是包括

activity android:name="com.ujjwalmainali.univhub.TestActivity"
        android:launchMode="singleInstance"
        android:exported="true"
        android:screenOrientation="portrait">

android:exported="true" 但这对我不起作用。这里有什么问题。我需要什么样的权限才能进行意图调用。

这是异常的logcat

java.lang.SecurityException: Permission Denial: starting Intent { act=com.viber.voip.action.CALL dat=tel:xxxxxxxxxxxxx.xxxxxxx.xxxxxxxxxxxxxxxxx cmp=com.viber.voip/.phone.PhoneFragmentActivity } from ProcessRecord{271db4b9 3911:com.ujjwalmainali.univhub/u0a219} (pid=3911, uid=10219) not exported from uid 10204
    at android.os.Parcel.readException(Parcel.java:1549)
    at android.os.Parcel.readException(Parcel.java:1502)
    at android.app.ActivityManagerProxy.startActivity(ActivityManagerNative.java:2463)
    at android.app.Instrumentation.execStartActivity(Instrumentation.java:1619)
    at android.app.Activity.startActivityForResult(Activity.java:3761)
    at android.support.v4.app.BaseFragmentActivityJB.startActivityForResult(BaseFragmentActivityJB.java:48)
    at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:77)
    at android.app.Activity.startActivityForResult(Activity.java:3722)
    at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:859)
    at android.app.Activity.startActivity(Activity.java:4032)
    at android.app.Activity.startActivity(Activity.java:4000)
    at com.ujjwalmainali.univhub.TestActivity.callToViberContact(TestActivity.java:57)
    at com.ujjwalmainali.univhub.TestActivity$1.onClick(TestActivity.java:33)
    at android.view.View.performClick(View.java:4785)
    at android.view.View$PerformClick.run(View.java:19869)
    at android.os.Handler.handleCallback(Handler.java:739)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:155)
    at android.app.ActivityThread.main(ActivityThread.java:5696)
    at java.lang.reflect.Method.invoke(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:372)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1028)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:823)
4

1 回答 1

1

将此标签添加到您的清单中:

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

对于 runTimePermission :

if (ActivityCompat.checkSelfPermission(context, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
                        ActivityCompat.requestPermissions(activityCompat, new String[]{Manifest.permission.CALL_PHONE}, 123);
                        return;
                    }
                    context.startActivity(localIntent4);

. . . .

public void onRequestPermissionsResult(int requestCode, String[] permissions,
                                       int[] grantResults) {
    for (int grantResult : grantResults) {
        if (grantResult == PackageManager.PERMISSION_DENIED) {
            return;
        }
    }
    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
        return;
    }
    startActivity(localIntent4 );
}

localIntent4 是您正在使用的意图。

编辑 :

您还需要将此标志设置为您的意图

            localIntent4.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
于 2017-03-26T20:16:25.083 回答