How to start Viber call from an Android app [new version]?

后端 未结 3 1507
旧时难觅i
旧时难觅i 2020-12-16 04:34

A while ago I created this post, and my colleague and I have found two different answers to it (both of them worked):

  1. First solution was to use

    Int

相关标签:
3条回答
  • 2020-12-16 05:12
    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);
    

    _id can be queried from contacts2.db(Column ContactsContract.Data._ID)

    0 讨论(0)
  • 2020-12-16 05:29

    According to Viber's manifest, there is activity "com.viber.voip.phone.PhoneActivity" that is responsible for action "com.viber.voip.action.CALL". In new version of Viber (4.2.1.1) this activity is marked by android:exported="false". As result, it's not possible anymore to start this activity from external applications...

    Edit

    This code opens welcome screen for specified contact

    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);
    

    but user should click button "free call" to start call.

    0 讨论(0)
  • 2020-12-16 05:30

    It is possible to call per Viber direct (without WelcomeActivity), but user should have this number in Contacts.

    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 permission is required. Don't forget to add in Manifest:

    <uses-permission android:name="android.permission.READ_CONTACTS" />
    
    0 讨论(0)
提交回复
热议问题