所以我使用this tutorial创建了一个简单的启动器。我创建了一些空按钮,我打算用它们在按下时立即拨号。
默认情况下,按钮没有指定的编号。我希望按钮在第一次按下时启动默认联系人应用程序窗口,因此用户可以选择要分配给该按钮的联系人号码。然后我会通过意图启动它。
我不知道如何完成这项工作的第一部分(打开联系人应用程序,然后为按钮分配详细信息)。我该如何工作?
答案 0 :(得分:1)
如果我从核心理解你的问题,你需要开始一个活动来获得结果。您可以通过单击按钮然后onActivityResult - >开始您的活动(联系人应用)。你得到你想要的数据。
例如:
private static final int REQUEST_CONTACT = 1;
private static final int RESULT_OK = -1;
private static final int RESULT_CANCELED = 0;
private Uri mInfo;
private Context mContext;
Button startContactsApp = (Button)findViewByid(...);
startContactsApp.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new Thread(new Runnable() { // you need a thread because this operation takes plenty of time
@Override
public void run() {
Intent contactIntent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
contactIntent.setType(ContactsContract.CommonDataKinds.Phone.CONTENT_TYPE); // to display contacts who have phone number
startActivityForResult(contactIntent, REQUEST_CONTACT);
}
}).start();
}
});
结果:
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if(requestCode == REQUEST_CONTACT){
if(resultCode == RESULT_OK && data != null){
mInfo = data.getData();
String name = getContactNameAgenda(); // the name you need
String phone = getContactPhoneNumberAgenda(); //the number you need
Toast.makeText(mContext,"CONTACT ADDED !",Toast.LENGTH_SHORT).show();
}
else if (resultCode == RESULT_CANCELED){
Toast.makeText(getActivity(),"CANCELLED OR SOME ERROR OCCURRED !",Toast.LENGTH_SHORT).show();
}
}
在这里,您将获得所选联系人的姓名和电话号码:
private String getContactNameAgenda(){
Cursor cursor = mContext.getContentResolver().query(mInfo, null,
null, null, null);
cursor.moveToFirst();
String name = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
cursor.close();
return name;
}
private String getContactPhoneNumberAgenda(){
Cursor cursor = mContext.getContentResolver().query(mInfo, new String[]{ContactsContract.Contacts._ID},
null, null, null);
cursor.moveToFirst();
String id = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
cursor.close();
// Using the contact ID now we will get contact phone number
Cursor cursorPhone = mContext.getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
new String[]{ContactsContract.CommonDataKinds.Phone.NUMBER},
ContactsContract.CommonDataKinds.Phone._ID + " = ? AND " + // Phone._ID is for the database ; Phone.CONTACT_ID is for contact when you are not querying that table
ContactsContract.CommonDataKinds.Phone.TYPE + " = " +
ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE,
new String[]{id},
null);
cursorPhone.moveToFirst();
String number = cursorPhone.getString(cursorPhone.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
cursorPhone.close();
return number;
}
我希望这会对你有所帮助。