现在,我正在编写一个可以访问用户联系信息的应用。我现在的目标是允许用户从他们的联系人列表中挑选某人作为他们的" Guardian"。这就是我访问联系人的方式:
@Override
public void onActivityResult(int reqCode, int resultCode, Intent data) {
super.onActivityResult(reqCode, resultCode, data);
switch (reqCode) {
case (PICK_CONTACT) :
if (resultCode == Activity.RESULT_OK) {
Uri contactData = data.getData();
Cursor c = getContentResolver().query(contactData, null, null, null, null);
if (c.moveToFirst()) {
String name = c.getString(c.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
Intent i = new Intent(getApplicationContext(), ManageGuardian.class);
i.putExtra("guardian_name", name);
startActivity(i);
}
}
break;
}
}
现在,我只收到联系人姓名。我希望得到名称和与该名称相关联的电话号码。有了这个,我想把它传递到下一个活动,就像我对Intent所做的那样。有没有办法让我同时获得名称和电话号码并将其传递给下一个活动?我现在无法解决这个问题,并且不知所措。
谢谢!
答案 0 :(得分:1)
我绝对建议使用ContentPicker Intent,然后启动另一个活动,传入该数据。您必须首先确保向清单添加READ_CONTACTS使用权限。我能够通过它读取联系人,但不能提取他们的电话号码。
<uses-permission android:name="android.permission.READ_CONTACTS" />
您还需要确保在清单中声明第二个活动。
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.fuuuuuuuuu.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.example.fuuuuuuuuu.DetailActivity"
android:label="@string/title_activity_detail" >
</activity>
</application>
在我的示例代码中,该代码适用于填充了联系人的GenyMotion模拟器,我有两个活动,主要和详细信息。
activity_main.xml中
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.fuuuuuuuuu.MainActivity">
<Button
android:id="@+id/btnSelectContact"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="29dp"
android:layout_marginTop="34dp"
android:text="Pick a Contact" />
</RelativeLayout>
在activity_detail.xml中,我有这个:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.fuuuuuuuuu.DetailActivity">
<TextView
android:id="@+id/txtName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginTop="37dp"
android:text="" />
<TextView
android:id="@+id/txtPhone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="@+id/txtName"
android:layout_marginTop="22dp"
android:text="" />
</RelativeLayout>
在MainActivity.java中,我有这段代码:
package com.example.fuuuuuuuuu;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.provider.ContactsContract;
public class MainActivity extends Activity {
Button _btnSelectContact;
static final int PICK_CONTACT_REQUEST = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
_btnSelectContact = (Button) findViewById(R.id.btnSelectContact);
_btnSelectContact.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
startActivityForResult(intent, PICK_CONTACT_REQUEST);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == PICK_CONTACT_REQUEST) {
if (resultCode == RESULT_OK) {
Uri contactUri = data.getData();
Cursor cursor = getContentResolver().query(contactUri, null, null, null, null);
cursor.moveToFirst();
String contactId = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
String displayName = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
String hasPhoneNumberString = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));
Boolean hasPhoneNumber = Integer.parseInt(hasPhoneNumberString) > 0;
String phoneNumber = "";
String selection = ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = " + contactId;
if (hasPhoneNumber) {
Cursor phones = getContentResolver().query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null,
selection,
null, null);
// Only getting one here ultimately, you could handle multiples depending on type
while (phones.moveToNext()) {
phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
}
phones.close();
}
cursor.close();
Intent secondActivityIntent = new Intent(getBaseContext(), DetailActivity.class);
secondActivityIntent.putExtra("NAME", displayName);
secondActivityIntent.putExtra("PHONE", phoneNumber);
startActivity(secondActivityIntent);
}
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
在DetailActivity.java中,我有这个:
package com.example.fuuuuuuuuu;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
public class DetailActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_detail);
Bundle extras = getIntent().getExtras();
if (extras != null) {
String displayName = extras.getString("NAME");
String phoneNumber = extras.getString("PHONE");
TextView name = (TextView) findViewById(R.id.txtName);
TextView phone = (TextView) findViewById(R.id.txtPhone);
name.setText(displayName);
phone.setText(phoneNumber);
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.detail, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
看起来你大部分都在那里,你只需要做二级光标来根据用户选择获得电话号码。我只是快速将这个项目放在一起,但您可以轻松地将任何内容传递给第二个活动(用户ID,姓名,电话,电子邮件等),然后对其进行操作。