2

I want to open the native Contact activity, not the Contacts list as I already have done that, but a particular contact information using the native activity. The special requirement here is that all I want is to display the contact's information but not allowing the user to click anything. I can open the activity with this code:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if(requestCode != 1) {
        return;
    }

    if(resultCode != Activity.RESULT_OK) {
        return;
    }

    Uri selectedUserUri = data.getData();
    this.onContactSelected(selectedUserUri);
}

private void onContactsButtonClick() {
    startActivityForResult(new Intent(Intent.ACTION_PICK, Contacts.CONTENT_URI), 1);
}

private void onContactSelected(Uri selectedUserUri) {
    startActivityForResult(new Intent(Intent.ACTION_VIEW, selectedUserUri), 2);
}

I need the user not to be able to click anything because that's not part of the functionalities of my application, in the worst case I would need the activity to report me the clicked phone number or e-mail address instead of opening the dialer or a web browser.

Any help will be greatly appreciated.

Mike

4

1 回答 1

1

试试下面的代码来开始选择联系人活动,比如这个代码写按钮点击事件联系人:

try
{
     Intent intent = new Intent(Intent.ACTION_PICK);
     intent.setType(ContactsContract.Contacts.CONTENT_TYPE);
     startActivityForResult(intent, PICK_CONTACT);
}
catch (Exception e) {
    Intent intent = getIntent();
    finish();
    startActivity(intent);
}

获得以下代码的联系电话后:

@Override
    public void onActivityResult(int requestCode, int resultCode, Intent intent) 
    {
      try
      {


      if (requestCode == PICK_CONTACT)
      {         
          Cursor cursor =  managedQuery(intent.getData(), null, null, null, null);
          cursor.moveToNext();
          String contactId = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
           String  name = cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME)); 
           String phone=cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.Contacts.HAS_PHONE_NUMBER));
           String phoneNumber="test";

          if ( phone.equalsIgnoreCase("1"))
              phone = "true";
          else
              phone = "false" ;

          if (Boolean.parseBoolean(phone)) 
          {
           Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = "+ contactId,null, null);
           while (phones.moveToNext()) 
           {
             phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
           }
           phones.close();
          }
          Toast.makeText(this, "You are selected Contact name "+name, Toast.LENGTH_LONG).show();
          if(phone_no.getText().length()!=0)
          {
              phone_no.setText(phone_no.getText().toString()+","+phoneNumber);
          }
          else
          {
              phone_no.setText(phoneNumber);
          }

      }
        }
      catch (Exception e) {
        // TODO: handle exception
        }
    }

此功能选择一个联系人分配电话号码编辑文本这不允许拨打电话!注意:此函数在 onCreate() 方法之后复制粘贴,如下格式:

@Override
   public void onCreate(Bundle savedInstanceState) {
     ------------Your Code ---------
}
@Override
   public void onActivityResult(int requestCode, int resultCode, Intent intent) 
   {
       ------------
   }
于 2012-07-09T05:32:40.280 回答