您的位置:首页 > 移动开发 > Android开发

Android---获取手机联系人及归属地

2016-03-12 23:17 429 查看
获取通讯录中的联系人以及使用Volley请求调用后台接口,获得联系人归属地。

1.activity_main.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="wrap_content"
tools:context="com.example.contacttest.MainActivity" >

<EditText
android:id="@+id/et_tel"
android:layout_width="match_parent"
android:layout_height="60dp"
android:padding="10dp"
android:textSize="19sp"
android:hint="请输入手机号"
android:background="@null"
android:inputType="phone"/>

<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#E0E0E0"
android:layout_below="@id/et_tel"
/>
<ImageView
android:id="@+id/iv_book"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:paddingRight="10dp"
android:layout_centerVertical="true"
android:src="@drawable/book"
/>

</RelativeLayout>


2.MainActivity.java

import org.json.JSONException;
import org.json.JSONObject;
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.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.ImageView;
import com.android.volley.Request.Method;
import com.android.volley.RequestQueue;
import com.android.volley.Response.ErrorListener;
import com.android.volley.Response.Listener;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;

public class MainActivity extends Activity implements OnClickListener {

private EditText et_tel;
private ImageView iv_book;
private static final int CONTACT_PICK = 1;
private RequestQueue mQueue; // volley的请求队列
String name;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

et_tel = (EditText) findViewById(R.id.et_tel);

iv_book = (ImageView) findViewById(R.id.iv_book);
iv_book.setOnClickListener(this);

mQueue = Volley.newRequestQueue(getApplicationContext());
}

@Override
public void onClick(View v) {
int id = v.getId();
switch (id) {
case R.id.iv_book:
Intent intent = new Intent();
intent.setAction(Intent.ACTION_PICK);
intent.setType(ContactsContract.Contacts.CONTENT_TYPE);
startActivityForResult(intent, CONTACT_PICK);
break;

default:
break;
}
}

@Override
public void onActivityResult(int reqCode, int resultCode, Intent data) {
super.onActivityResult(reqCode, resultCode, data);

switch (reqCode) {
case (CONTACT_PICK):
if (resultCode == Activity.RESULT_OK) {
if (data == null) {
return;
}
Uri contactData = data.getData();
Cursor cur = getContentResolver().query(contactData, null,
null, null, null);
if (cur.moveToFirst()) {
name = cur
.getString(cur
.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
int phoneCount = cur
.getInt(cur
.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));
String contactID = cur.getString(cur
.getColumnIndex(ContactsContract.Contacts._ID));

if (phoneCount > 0) {
Cursor phones = getContentResolver()
.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID
+ " = " + contactID, null, null);
if (phones.moveToFirst()) {
do {
// 遍历所有的电话号码
String phoneNumber = phones
.getString(phones
.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));

try {
getLocate(phoneNumber);
} catch (Exception e) {
e.printStackTrace();
}

} while (phones.moveToNext());
}
}
}

}
}
}

private void getLocate(final String phoneNumber) throws Exception {

StringRequest request = new StringRequest(Method.GET,
ConstantValue.URL+phoneNumber, new Listener<String>() {

@Override
public void onResponse(String arg0) {
Log.d("onResponse", arg0);
String province = null,city = null;
try {
JSONObject jsonObject = new JSONObject(arg0);
province = jsonObject.optJSONObject("result").optString("province");
city = jsonObject.optJSONObject("result").optString("city");

} catch (JSONException e) {
e.printStackTrace();
}
if(province.equals(city)){
province = "";
}

et_tel.setText(phoneNumber + " (" + name + ")" + province + " " + city);
}
}, new ErrorListener() {

@Override
public void onErrorResponse(VolleyError arg0) {
Log.d("onErrorResponse", arg0.toString());
}
});

mQueue.add(request);
}

}


3.在AndroidManifest文件中添加读取联系人的权限及访问网络权限

<uses-permission android:name="android.permission.READ_CONTACTS"/>
<uses-permission android:name="android.permission.INTERNET"/>


4.常量

public class ConstantValue {
public static final String URL = "http://apicloud.mob.com/v1/mobile/address/query?key=105de5b7b0638&phone=";
public static final String key = "105de5b7b0638";
}


运行截图:





源码下载

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: