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?