Android---获取手机联系人及归属地
2016-03-12 23:17
429 查看
获取通讯录中的联系人以及使用Volley请求调用后台接口,获得联系人归属地。
1.activity_main.xml
2.MainActivity.java
3.在AndroidManifest文件中添加读取联系人的权限及访问网络权限
4.常量
运行截图:
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"; }
运行截图:
源码下载
相关文章推荐
- [android] 内容提供者实现
- 使用实验性的 Gradle 工具构建 Android NDK 环境
- Android开发之调用摄像头照相并剪切
- Android studio提示:plugs is too old
- This version of android studio is incompatible with the gradle version used.Try disabling the instant run
- Android布局文件.xml中的自定义属性
- android studio Cannot reload AVD list
- android 圆角图片
- 将android手机屏幕投影到电脑端
- android_________测绘流程
- Android -- XML Drawable
- Android自定义控件之流式布局
- android开发教程之开机启动服务service示例
- Android中实现开机自动启动服务(service)实例
- android屏幕适配总结
- Android MVP 介绍
- Android-使用绝对布局管理器固定组件的位置
- 六款值得推荐的android(安卓)开源框架简介
- Android布局文件.xml中的自定义属性(结合封装的自定义View)
- android学习之路(一) 环境搭建