想要从contacts.db获取列索引,直接打开viber聊天屏幕

时间:2016-06-24 05:29:04

标签: android sqlite

代码低于......

Cursor c=context.getContentResolver().query(ContactsContract.Data.CONTENT_URI,
                new String[]{ContactsContract.Contacts.Data._ID}, 
ContactsContract.Data.DATA1 + " like '%" + contactNumber + "' and data2='Viber' and mimetype='vnd.android.cursor.item/vnd.com.viber.voip.viber_number_message'",
                null, null);
c.moveToFirst();
intent = new Intent(Intent.ACTION_VIEW, Uri.parse("content://com.android.contacts/data/" + c.getString(0)));

........

1 个答案:

答案 0 :(得分:0)

为我工作

    public void sendViberMessageTo(String contactNumber) {
    if (contactNumber.length() > 4) {
        contactNumber = contactNumber.substring(3);
    }
    Intent intent = null;
    Cursor c = context.getContentResolver().query(ContactsContract.Data.CONTENT_URI,
            new String[]{ContactsContract.Contacts.Data._ID}, ContactsContract.Data.DATA1 + " like '%" + contactNumber + "' and data2='Viber' and mimetype='vnd.android.cursor.item/vnd.com.viber.voip.viber_number_message'",
            null, null);
    if (c.moveToFirst()) {
        intent = new Intent(Intent.ACTION_VIEW, Uri.parse("content://com.android.contacts/data/" + c.getString(0)));
        c.close();
        intent.addFlags(Intent.FLAG_ACTIVITY_FORWARD_RESULT | Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET | Intent.FLAG_ACTIVITY_PREVIOUS_IS_TOP);
        intent.setPackage("com.viber.voip");
    }
    if (intent != null) {
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(intent);
        Toast.makeText(context, "Viber : " + contactNumber, Toast.LENGTH_SHORT).show();
    } else {
        Toast.makeText(context, "Viber not available for this contact", Toast.LENGTH_SHORT).show();
    }
}