Android - 片段中的联系人选择器
Posted
技术标签:
【中文标题】Android - 片段中的联系人选择器【英文标题】:Android - contact picker in fragment 【发布时间】:2015-06-20 13:27:15 【问题描述】:我有一个viewpager/滑动标签。我希望能够从手机上已经存在的通讯录中获取 4 个联系人并将它们保存在应用程序中以供以后使用。我有代码来做一个活动的联系人选择器,但我的联系人页面是一个片段,我还没有让它工作。
任何想法或建议将不胜感激!
它连接到的主要活动的代码:
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.ViewPager;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity2Activity extends ActionBarActivity implements ActionBar.TabListener, ViewPager.OnPageChangeListener
private ViewPager mViewPager;
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_activity2);
PagerAdapter adapter = new PagerAdapter(getSupportFragmentManager());
mViewPager = (ViewPager) findViewById(R.id.pager);
mViewPager.setAdapter(adapter);
mViewPager.setOnPageChangeListener(this);
ActionBar actionBar = getSupportActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
ActionBar.Tab tab = actionBar.newTab().setText("Contacts").setTabListener(this);
actionBar.addTab(tab);
tab = actionBar.newTab().setText("GPS").setTabListener(this);
actionBar.addTab(tab);
tab = actionBar.newTab().setText("Message").setTabListener(this);
actionBar.addTab(tab);
public class PagerAdapter extends FragmentPagerAdapter
public PagerAdapter(FragmentManager fm)
super(fm);
public Fragment getItem(int arg0)
switch (arg0)
case 0:
return new MainMenu_Contacts();
case 1:
return new MainMenu_GPS();
case 2:
return new MainMenu_Message();
default:
return null;
public int getCount()
return 3;
//implements on pager selected
@Override
public void onPageScrolled(int i, float v, int i2)
@Override
public void onPageSelected(int i)
getSupportActionBar().setSelectedNavigationItem(i);
@Override
public void onPageScrollStateChanged(int i)
//implements tab listener
@Override
public void onTabSelected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction)
mViewPager.setCurrentItem(tab.getPosition());
@Override
public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction)
@Override
public void onTabReselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction)
主要活动的 XML:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.view.ViewPager
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/pager"
android:layout_
android:layout_ />
联系人片段代码:
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.support.v4.app.Fragment;
import android.view.View;
import android.widget.Button;
import android.view.View.OnClickListener;
public class MainMenu_Contacts extends Fragment implements OnClickListener
private Button mBtnContacts;
private final int PICK = 1;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_menu__contacts);
mBtnContacts = (Button) findViewById(R.id.xBtnContacts);
mBtnContacts.setOnClickListener(this);
@Override
public void onClick(View v)
// TODO Auto-generated method stub
// Opening Contacts Window as a Window
Intent intent = new Intent(Intent.ACTION_PICK,
ContactsContract.Contacts.CONTENT_URI);
// calling OnActivityResult with intenet And Some conatct for Identifie
startActivityForResult(intent, PICK);
@Override
public void onActivityResult(int reqCode, int resultCode, Intent data)
super.onActivityResult(reqCode, resultCode, data);
switch (reqCode)
case (PICK):
if (resultCode == Activity.RESULT_OK)
Uri contactData = data.getData();
Cursor c = managedQuery(contactData, null, null, null, null);
if (c.moveToFirst())
String name = c.getString(c
.getColumnIndexOrThrow(Contacts.People.NAME));
// TODO Whatever you want to do with the selected contact
// name.
break;
联系人的 XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_
android:layout_>
<Button android:layout_
android:layout_ android:id="@+id/xBtnContacts"
android:text="Pick Contacts" />
</LinearLayout>
【问题讨论】:
如果您需要进一步的帮助,请提供联系方式的代码 【参考方案1】:通过ContentProvider
读取手机中的联系人,该ContentProvider
需要您的应用程序中的权限。也许这是你的问题,因为你没有提到它们。
<uses-permission android:name="android.permission.READ_CONTACTS" />
【讨论】:
【参考方案2】:我通过以下步骤使用了联系人选择器。你可以用你的视图类改变ImageButton
ImageButton pick_contact = (ImageButton) findViewById(R.id.pick_contact);
pick_contact.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View view)
Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.CommonDataKinds.Phone.CONTENT_URI);
startActivityForResult(intent, PICK_CONTACT);
);
活动后期:
@Override
protected void onActivityResult(final int requestCode, int resultCode, Intent data)
if (data != null)
Uri result = data.getData();
Cursor c = getContentResolver().query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
ContactsContract.CommonDataKinds.Phone._ID + "=?",
new String[]result.getLastPathSegment(), null);
if (c.getCount() >= 1 && c.moveToFirst())
final String number = c.getString(c.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
final String name = c.getString(c.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
重要提示:在AndroidManifest.xml中,添加
<uses-permission android:name="android.permission.READ_CONTACTS" />
更新: 看一看:https://github.com/codinguser/android_contact_picker
【讨论】:
以上是关于Android - 片段中的联系人选择器的主要内容,如果未能解决你的问题,请参考以下文章