如何从Android应用程序[新版本]启动Viber调用?

时间:2014-01-27 17:19:37

标签: android viber

不久前我创建了this帖子,我的同事和我找到了两个不同的答案(两个都有效):

  1. 第一个解决方案是使用

    Intent callIntent = new 意图( “android.intent.action.CALL_PRIVILEGED”);

    这将打开新窗口,提供所有人的通话 可能的拨打电话的方式 - 在这种特殊情况下,他们是 拨号器和Viber以及Skype(或稍后添加的任何其他方法)。

  2. 第二个解决方案是明确调用Viber并拨打电话 要调用的号码

    Intent viberCallIntent = new Intent(“com.viber.voip.action.CALL”); viberCallIntent.setType( “vnd.android.cursor.item / vnd.com.viber.voip.call”); viberCallIntent.setData(Uri.parse(“tel:”+ dialNumber)); viberCallIntent.putExtra(“external_call”,true); viberCallIntent.putExtra(“contact_id”, - 1L); startActivity(viberCallIntent);

  3. 自上次Viber更新(我们昨天注意到)以来,这些方法都不起作用。第一个只是使用Skype打电话,甚至不提供拨号器(!?!),但如果没有安装Skype,那么它将回落到拨号器并拨打电话。第二个崩溃声称没有找到处理Intent的Activity?

    知道发生了什么以及如何解决这个问题?

    错误日志如下:

    01-27 17:35:03.794: E/AndroidRuntime(7738): FATAL EXCEPTION: main
    01-27 17:35:03.794: E/AndroidRuntime(7738): android.content.ActivityNotFoundException: No Activity found to handle Intent { act=com.viber.voip dat=tel:xxxxxxxxx (has extras) }
    01-27 17:35:03.794: E/AndroidRuntime(7738):     at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1628)
    01-27 17:35:03.794: E/AndroidRuntime(7738):     at android.app.Instrumentation.execStartActivity(Instrumentation.java:1423)
    01-27 17:35:03.794: E/AndroidRuntime(7738):     at android.app.Activity.startActivityForResult(Activity.java:3388)
    01-27 17:35:03.794: E/AndroidRuntime(7738):     at android.app.Activity.startActivityForResult(Activity.java:3349)
    01-27 17:35:03.794: E/AndroidRuntime(7738):     at android.app.Activity.startActivity(Activity.java:3584)
    01-27 17:35:03.794: E/AndroidRuntime(7738):     at android.app.Activity.startActivity(Activity.java:3552)
    01-27 17:35:03.794: E/AndroidRuntime(7738):     at rs.limitline.maxitaxins.MainActivity.callViber(MainActivity.java:96)
    01-27 17:35:03.794: E/AndroidRuntime(7738):     at rs.limitline.maxitaxins.MainActivity.onClick(MainActivity.java:186)
    01-27 17:35:03.794: E/AndroidRuntime(7738):     at android.view.View.performClick(View.java:4212)
    01-27 17:35:03.794: E/AndroidRuntime(7738):     at android.view.View$PerformClick.run(View.java:17476)
    01-27 17:35:03.794: E/AndroidRuntime(7738):     at android.os.Handler.handleCallback(Handler.java:800)
    01-27 17:35:03.794: E/AndroidRuntime(7738):     at android.os.Handler.dispatchMessage(Handler.java:100)
    01-27 17:35:03.794: E/AndroidRuntime(7738):     at android.os.Looper.loop(Looper.java:194)
    01-27 17:35:03.794: E/AndroidRuntime(7738):     at android.app.ActivityThread.main(ActivityThread.java:5431)
    01-27 17:35:03.794: E/AndroidRuntime(7738):     at java.lang.reflect.Method.invokeNative(Native Method)
    01-27 17:35:03.794: E/AndroidRuntime(7738):     at java.lang.reflect.Method.invoke(Method.java:525)
    01-27 17:35:03.794: E/AndroidRuntime(7738):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
    01-27 17:35:03.794: E/AndroidRuntime(7738):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
    01-27 17:35:03.794: E/AndroidRuntime(7738):     at dalvik.system.NativeStart.main(Native Method)
    

    [UPDATE] 这也显示为错误:

    01-27 19:46:56.704: D/Greska(19822): Permission Denial: starting Intent { act=com.viber.voip.action.CALL dat=tel:xxxxxxxxx cmp=com.viber.voip/com.viber.service.OutgoingCallBroadcaster (has extras) } from ProcessRecord{416c41b8 19822:rs.limitline.maxitaxins/u0a10065} (pid=19822, uid=10065) not exported from uid 10147
    

3 个答案:

答案 0 :(得分:20)

根据Viber的清单,有活动“com.viber.voip.phone.PhoneActivity”负责行动“com.viber.voip.action.CALL”。在新版本的Viber(4.2.1.1)中,此活动以android:exported="false"标记。因此,再也无法从外部应用程序启动此活动......

修改

此代码打开指定联系人的欢迎屏幕

String sphone = "12345678";
Uri uri = Uri.parse("tel:" + Uri.encode(sphone)); 
Intent intent = new Intent("android.intent.action.VIEW");
intent.setClassName("com.viber.voip", "com.viber.voip.WelcomeActivity");
intent.setData(uri); 
context.startActivity(intent);

但用户应点击“免费通话”按钮开始通话。

答案 1 :(得分:4)

可以直接按Viber进行呼叫(不使用WelcomeActivity),但用户应在联系人中使用此号码。

public void callToViberContact(String phoneNumber, Context context) {
    Uri uri = getUriFromPhoneNumber(phoneNumber, context);
    if (uri != null) {
        Intent intent = new Intent("android.intent.action.VIEW");
        intent.setClassName("com.viber.voip", "com.viber.voip.SystemDialogActivityPublic");
        intent.setData(uri);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(intent);
    } else {
        Toast.makeText(context, "Number is not in Viber Contacts List", Toast.LENGTH_LONG).show();
    }
}

private Uri getUriFromPhoneNumber(String phoneNumber, Context context) {
    Uri uri = null;
    String contactId = getContactIdByPhoneNumber(phoneNumber, context);
    if (!TextUtils.isEmpty(contactId)) {
        Cursor cursor = context.getContentResolver().query(
                ContactsContract.Data.CONTENT_URI, new String[]{ContactsContract.Data._ID},
                ContactsContract.Data.DATA2 + "=? AND " + ContactsContract.Data.CONTACT_ID + " = ?",
                new String[]{"Viber", contactId}, null);
        if (cursor != null) {
            while (cursor.moveToNext()){
                String id = cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.Data._ID));
                if (!TextUtils.isEmpty(id)) {
                    uri = Uri.parse(ContactsContract.Data.CONTENT_URI + "/" + id);
                    break;
                }
            }
            cursor.close();
        }
    }
    return uri;
}

private String getContactIdByPhoneNumber(String phoneNumber, Context context) {
    ContentResolver contentResolver = context.getContentResolver();
    String contactId = null;
    Uri uri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(phoneNumber));

    String[] projection = new String[]{ContactsContract.PhoneLookup._ID};

    Cursor cursor =
            contentResolver.query(
                    uri,
                    projection,
                    null,
                    null,
                    null);

    if (cursor != null) {
        while (cursor.moveToNext()) {
            contactId = cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.PhoneLookup._ID));
        }
        cursor.close();
    }
    return contactId;
}

需要READ_CONTACTS权限。不要忘记添加Manifest:

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

答案 2 :(得分:0)

String uriString = "content://com.android.contacts/data/" + _id;

Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.setData(Uri.parse(uriString));
intent.setPackage(PKG_VIBER);
startActivity(intent);
可以从contacts2.db(列ContactsContract.Data._ID)中查询

_id