如何获得联系方式(即为编号名称等)的一次活动是通过推出QuickContactBadge(How t

2019-10-30 17:48发布

我的工作QuickContactBadge。 我想要做的就是展示像Facebook或Gmail在QuickContactBadge我的应用程序图标,在图标上的用户按下它会推出我的应用程序选定的活动。 而当该活动推出的我想哪个用户选择的电话号码。

目前,我的应用程序显示QuickContactBadge和徽章也推出主要活动在应用程序,但我不能够得到的电话号码形式,用户启动应用程序。

我的代码如下:

@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if(getIntent().getData() != null) {
            Cursor cursor = getContentResolver().query(getIntent().getData(), null, null, null, null);
            if(cursor != null) {
                while (cursor.moveToNext()) { 
                    String contactId = cursor.getString(cursor.getColumnIndex( 
                            ContactsContract.Contacts._ID)); 
                    String hasPhone = cursor.getString(cursor.getColumnIndex( 
                            ContactsContract.Contacts.HAS_PHONE_NUMBER)); 
                    if (Integer.parseInt(hasPhone) == 1) { 
                        // You know have the number so now query it like this
                        Cursor phones = getContentResolver().query( 
                                ContactsContract.CommonDataKinds.Phone.CONTENT_URI, 
                                null, 
                                ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = ?" , 
                                new String[]{contactId}, null); 
                        while (phones.moveToNext()) { 
                            String phoneNumber = phones.getString( 
                                    phones.getColumnIndex( 
                                            ContactsContract.CommonDataKinds.Phone.NUMBER));  
                        } 
                        phones.close(); 
                    } else{
                        Toast.makeText(this, "Invalid Contact", Toast.LENGTH_SHORT).show();
                    }
                }   
            }
        }
        setContentView(R.layout.main);
    }

它混得hasPhone号Intent.getData但异常,因为列不存在,并在代码中也下来了,如果我忽略这个例外在获取电话号码无法得到它。 PLZ帮我哪里做错了。

我已经使用这个代码在QuickContactBadge显示我的应用程序

<activity
    android:name=".SandBoxProjectActivity"
    android:label="@string/app_name" >
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />

        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
    <intent-filter>
        <action android:name="android.intent.action.VIEW" />

        <category android:name="android.intent.category.DEFAULT" />

        <data android:mimeType="vnd.android.cursor.item/name" />
    </intent-filter>
</activity>

Answer 1:

我没仔细看你的代码,但是这个工作对我来说:

        // check if activity was launched from contacts
    if (getIntent().getData() != null) {
        Cursor cursor = getContentResolver().query(getIntent().getData(), null, null, null, null);
        if (cursor!=null && cursor.moveToNext()) {
            String contactId = cursor.getString(cursor.getColumnIndex(ContactsContract.Data.CONTACT_ID));
            cursor.close();

            selectContactNumber(contactId);
        }
    }


}

private void selectContactNumber(String contactId) {
    ArrayList<String> numbersArr = new ArrayList<String>();

    // You know it has a number so now query it like this
    Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = " + contactId, null, null);
    while (phones.moveToNext()) {
        String phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
        numbersArr.add(phoneNumber);
    }
    phones.close();

    switch (numbersArr.size()) {
    case 0:
        mNumberSelectorDialog = DialogFactory.getInstance().createNoNumbersMessageDialog(this);
        mNumberSelectorDialog.show();
        break;
    case 1:
        setNumberToCall(numbersArr.get(0));
        break;
    default:
        mNumberSelectorDialog = DialogFactory.getInstance().createNumberSelectorDialog(this, numbersArr.toArray(new String[0]));
        mNumberSelectorDialog.show();
        break;
    }
}


文章来源: How to get Contact Info(i.e Number name etc) once the activity is launch by QuickContactBadge