How to get Contact Info(i.e Number name etc) once

2019-09-18 08:19发布

问题:

I am working on QuickContactBadge. What I want to do is to show my app icon in the QuickContactBadge like facebook or gmail, and when the user press on the icon it will launch my application a selected activity. And when the the activity is launch i want to get the phone number which user selected.

Currently my application is showing QuickContactBadge and the badge also launch the main activity in the application but i am not able to get the phone number form which user launch the application.

My code is as follow:

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

It getting the Intent.getData but exception on hasPhone number because column does not exist and also down in the code if I ignore this exception while getting the phone number unable to get it. Plz help me where i am doing wrong.

I have use this code to display my app in the 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>

回答1:

I didn't look closely on your code, but this worked for me:

        // 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;
    }
}