There is a task: to call a contact through Viber for this there is the following code:

 public static void callViber(String dialNumber, Context context) { Uri uri = getUriFromPhoneNumber(dialNumber,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 static 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 static 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; } 

I give the phone number to the method, then its ID is defined in the phone book and with this ID, we get the URI which we later put into the Intent to call Viber . It turns out that you need this contact to be saved in the phonebook of the device.

Question: what to do if in my application contacts are not stored in the phone database, but let's say in Realm how can I call Viber in such a case?

  • find a contact from the database of the phone, no more - andreich
  • @andreich i. no other way to generate a URI I can not? and, accordingly, to make a call to Viber in another way is also impossible? - Kirill Stoianov

1 answer 1

Sul by en-SO can be as follows:

 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); 
  • Yes, it works, but the problem is that in the version that I described in the question when you call the activation, the call immediately starts, in the same case, if there is such a contact in Viber, then I’m just thrown onto the screen of the contact, and if there is no such contact anywhere neither in Viber nor in the phone, it simply throws a list of contacts in Viber - Kirill Stoianov
  • @KirillStoianov, they also write "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 what you used to be, and now - down. The only way. As I understand it - YuriySPb
  • those. if there is no contact in the phonebook of the device, then I can not immediately make a call, just open Viber itself, right? - Kirill Stoianov
  • @KirillStoianov, it seems that way. But I’ve only just dug it in Google - I have never tried it myself ) - Yuriy SPb
  • but do not tell me where you can read more about it? - Kirill Stoianov