2015-10-02 173 views
0

所以我创建了一个简单的启动器,使用this tutorial。我已经创建了几个空的按钮,我打算用它们在按下时立即拨打号码。如何以编程方式创建快速拨号按钮?

默认情况下,按钮没有分配的编号。我希望按钮在第一次按下时启动默认联系人应用程序窗口,因此用户可以选择要分配给按钮的联系人号码。然后我会通过一个意图启动它。

我不知道如何使这项工作的第一部分(调出联系人应用程序,然后将详细信息分配给按钮)。我如何完成这项工作?

回答

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

我希望这会帮助你。