您可以嘗試通過存儲在ArrayList中的聯繫電話和聯繫人姓名與另一個ArrayList中存儲從您的電話與我們聯繫驗證碼:
=== Contacts.java ===
package com.showcontacts;
import java.util.ArrayList;
import android.app.Activity;
import android.content.ContentResolver;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.util.Log;
import android.view.Menu;
import android.view.View;
public class Contacts extends Activity {
String phoneNumber;
ArrayList<String> phones = new ArrayList<String>();
ArrayList<String> contact_name = new ArrayList<String>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_contacts);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.contacts, menu);
return true;
}
/**** get all contacts ******/
public void getNumber(View v) {
ContentResolver cr = getContentResolver();
String name;
Cursor cursor = cr.query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null,
null, null);
while (cursor.moveToNext()) {
name = cursor
.getString(cursor
.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
phoneNumber = cursor
.getString(cursor
.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
phones.add(phoneNumber);
contact_name.add(name);// fill the Arraylist
}
cursor.close();// close cursor
for (int i = 0; i < phones.size(); i++) {
Log.d(contact_name.get(i) + i, (phones.get(i)));
}
}
}
** ** activity_contacts.xml
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".Contacts" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="26dp"
android:text="Button"
android:onClick="getNumber"
/>
**不要忘了在你的清單**
<uses-permission android:name="android.permission.READ_CONTACTS"/>
最佳添加權限祝你好運