我正在使用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);
}
它获取了Intent.getData但是在hasPhone号码上是异常,因为如果我在忽略此异常的同时获取电话号码无法获取该列,则该列不存在并且在代码中也是如此。 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>
答案 0 :(得分: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;
}
}