我是android的新手。我需要在我的应用程序中显示手机联系人。我在这里面临的问题是谷歌联系人也随着我的应用程序中的手机联系人。我不知道如何过滤这个问题。我只需要显示我的手机联系人。请帮帮我。 提前致谢
list = new ArrayList<contactlist>();
adptr = new customcontactadptr(this, R.layout.listcontactitem, list);
Uri uri = ContactsContract.Contacts.CONTENT_URI;
ContentResolver cr = getContentResolver();
String sortOrder = ContactsContract.Contacts.DISPLAY_NAME + " COLLATE LOCALIZED ASC";
Cursor cur = cr.query(uri, null, null, null, sortOrder);
if(cur.getCount() > 0)
{
cur.moveToFirst();
do
{
if (Integer.parseInt(cur.getString(
cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0)
{
String contactid = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
contactlist con = new contactlist();
nme = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
Log.e("contactid",contactid);
String status = cur.getString(cur.getColumnIndexOrThrow(ContactsContract.Contacts.CONTACT_STATUS));
Long statuslabel = cur.getLong(cur.getColumnIndexOrThrow(ContactsContract.Contacts.CONTACT_STATUS_LABEL));
if(status!=null)
{
Log.e("status", status);
}
else if(statuslabel!=null)
{
Log.e("status label", Long.toString(statuslabel));
}
Cursor pCur = cr.query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = ?",
new String[]{contactid}, null);
pCur.moveToFirst();
do
{
int phone_type = pCur.getInt(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE));
if(phone_type == ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE)
{
no = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
Log.e("Name",nme);
Log.e("Number",no);
con.setchecked(false);
con.setname(nme);
con.setno(n);
list.add(con);
adptr.notifyDataSetChanged();
}
}while (pCur.moveToNext());
pCur.close();
}
}while(cur.moveToNext());
}
setListAdapter(adptr);
答案 0 :(得分:0)
以下是示例代码....
public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Creating a button click listener for the "Add Contact" button
OnClickListener addClickListener = new OnClickListener() {
@Override
public void onClick(View v) {
// Getting reference to Name EditText
EditText etName = (EditText) findViewById(R.id.et_name);
// Getting reference to Mobile EditText
EditText etMobile = (EditText) findViewById(R.id.et_mobile_phone);
// Getting reference to HomePhone EditText
EditText etHomePhone = (EditText) findViewById(R.id.et_home_phone);
// Getting reference to HomeEmail EditText
EditText etHomeEmail = (EditText) findViewById(R.id.et_home_email);
// Getting reference to WorkEmail EditText
EditText etWorkEmail = (EditText) findViewById(R.id.et_work_email);
ArrayList<ContentProviderOperation> ops =
new ArrayList<ContentProviderOperation>();
int rawContactID = ops.size();
// Adding insert operation to operations list
// to insert a new raw contact in the table ContactsContract.RawContacts
ops.add(ContentProviderOperation.newInsert(ContactsContract.RawContacts.CONTENT_URI)
.withValue(ContactsContract.RawContacts.ACCOUNT_TYPE, null)
.withValue(RawContacts.ACCOUNT_NAME, null)
.build());
// Adding insert operation to operations list
// to insert display name in the table ContactsContract.Data
ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
.withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, rawContactID)
.withValue(ContactsContract.Data.MIMETYPE, StructuredName.CONTENT_ITEM_TYPE)
.withValue(StructuredName.DISPLAY_NAME, etName.getText().toString())
.build());
// Adding insert operation to operations list
// to insert Mobile Number in the table ContactsContract.Data
ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
.withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, rawContactID)
.withValue(ContactsContract.Data.MIMETYPE, Phone.CONTENT_ITEM_TYPE)
.withValue(Phone.NUMBER, etMobile.getText().toString())
.withValue(Phone.TYPE, CommonDataKinds.Phone.TYPE_MOBILE)
.build());
// Adding insert operation to operations list
// to insert Home Phone Number in the table ContactsContract.Data
ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
.withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, rawContactID)
.withValue(ContactsContract.Data.MIMETYPE, Phone.CONTENT_ITEM_TYPE)
.withValue(Phone.NUMBER, etHomePhone.getText().toString())
.withValue(Phone.TYPE, Phone.TYPE_HOME)
.build());
// Adding insert operation to operations list
// to insert Home Email in the table ContactsContract.Data
ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
.withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, rawContactID)
.withValue(ContactsContract.Data.MIMETYPE, Email.CONTENT_ITEM_TYPE)
.withValue(Email.ADDRESS, etHomeEmail.getText().toString())
.withValue(Email.TYPE, Email.TYPE_HOME)
.build());
// Adding insert operation to operations list
// to insert Work Email in the table ContactsContract.Data
ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
.withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, rawContactID)
.withValue(ContactsContract.Data.MIMETYPE, Email.CONTENT_ITEM_TYPE)
.withValue(Email.ADDRESS, etWorkEmail.getText().toString())
.withValue(Email.TYPE, Email.TYPE_WORK)
.build());
try{
// Executing all the insert operations as a single database transaction
getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);
Toast.makeText(getBaseContext(), "Contact is successfully added", Toast.LENGTH_SHORT).show();
}catch (RemoteException e) {
e.printStackTrace();
}catch (OperationApplicationException e) {
e.printStackTrace();
}
}
};
// Creating a button click listener for the "Add Contact" button
OnClickListener contactsClickListener = new OnClickListener() {
@Override
public void onClick(View v) {
// Creating an intent to open Android's Contacts List
Intent contacts = new Intent(Intent.ACTION_VIEW,ContactsContract.Contacts.CONTENT_URI);
// Starting the activity
startActivity(contacts);
}
};
// Getting reference to "Add Contact" button
Button btnAdd = (Button) findViewById(R.id.btn_add);
// Getting reference to "Contacts List" button
Button btnContacts = (Button) findViewById(R.id.btn_contacts);
// Setting click listener for the "Add Contact" button
btnAdd.setOnClickListener(addClickListener);
// Setting click listener for the "List Contacts" button
btnContacts.setOnClickListener(contactsClickListener);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}