时间:2021-05-21
我的风格,废话不多说了,直接给大家贴代码了。
具体代码如下所示:
package com.org.demo.demo;import com.org.wangfeng.R;import android.app.Activity;import android.content.Intent;import android.os.Bundle;import android.util.Log;import android.view.View;import android.widget.Button;import android.widget.EditText;public class GetPhoneActivity extends Activity { private EditText editText; private Button button; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.getphoneactivity); editText = (EditText) findViewById(R.id.et_getphone); button = (Button) findViewById(R.id.bb_getphone); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View arg0) { // TODO Auto-generated method stub Intent intent = new Intent(GetPhoneActivity.this, ContactActivity.class); startActivityForResult(intent, 1); } }); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { // System.out.println("resultCode:" + resultCode); // System.out.println("requestCode:" + requestCode); Log.d("jiejie", "被调用了"); if (resultCode == Activity.RESULT_OK) { String phone = data.getStringExtra("phone"); Log.d("jiejie", "_______________"+phone); phone = phone.replaceAll("-", "").replaceAll(" ", "");// 替换-和空格 editText.setText(phone);// 把电话号码设置给输入框 } super.onActivityResult(requestCode, resultCode, data); }}package com.org.demo.demo;import java.util.ArrayList;import java.util.HashMap;import com.org.wangfeng.R;import android.app.Activity;import android.content.Intent;import android.database.Cursor;import android.net.Uri;import android.os.Bundle;import android.view.View;import android.widget.AdapterView;import android.widget.ListView;import android.widget.SimpleAdapter;import android.widget.AdapterView.OnItemClickListener;import android.widget.TextView;public class ContactActivity extends Activity { private ListView lvList; private ArrayList<HashMap<String, String>> readContact; private TextView back; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_contact); back =(TextView)findViewById(R.id.back); back.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View arg0) { // TODO Auto-generated method stub finish(); } }); lvList = (ListView) findViewById(R.id.lv_list); readContact = readContact(); // System.out.println(readContact); lvList.setAdapter(new SimpleAdapter(this, readContact, R.layout.contact_list_item, new String[] { "name", "phone" }, new int[] { R.id.tv_name, R.id.tv_phone })); lvList.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { String phone = readContact.get(position).get("phone");// 读取当前item的电话号码 Intent intent = new Intent(); intent.putExtra("phone", phone); setResult(Activity.RESULT_OK, intent);// 将数据放在intent中返回给上一个页面 finish(); } }); } private ArrayList<HashMap<String, String>> readContact() { // 首先,从raw_contacts中读取联系人的id("contact_id") // 其次, 根据contact_id从data表中查询出相应的电话号码和联系人名称 // 然后,根据mimetype来区分哪个是联系人,哪个是电话号码 Uri rawContactsUri = Uri .parse("content://com.android.contacts/raw_contacts"); Uri dataUri = Uri.parse("content://com.android.contacts/data"); ArrayList<HashMap<String, String>> list = new ArrayList<HashMap<String, String>>(); // 从raw_contacts中读取联系人的id("contact_id") Cursor rawContactsCursor = getContentResolver().query(rawContactsUri, new String[] { "contact_id" }, null, null, null); if (rawContactsCursor != null) { while (rawContactsCursor.moveToNext()) { String contactId = rawContactsCursor.getString(0); // System.out.println(contactId); // 根据contact_id从data表中查询出相应的电话号码和联系人名称, 实际上查询的是视图view_data Cursor dataCursor = getContentResolver().query(dataUri, new String[] { "data1", "mimetype" }, "contact_id=?", new String[] { contactId }, null); if (dataCursor != null) { HashMap<String, String> map = new HashMap<String, String>(); while (dataCursor.moveToNext()) { String data1 = dataCursor.getString(0); String mimetype = dataCursor.getString(1); // System.out.println(contactId + ";" + data1 + ";" // + mimetype); if ("vnd.android.cursor.item/phone_v2".equals(mimetype)) { map.put("phone", data1); } else if ("vnd.android.cursor.item/name" .equals(mimetype)) { map.put("name", data1); } } list.add(map); dataCursor.close(); } } rawContactsCursor.close(); } return list; }}本段代码到此结束,代码比较简单,并附有注释,希望对大家学习android获取手机联系人相关知识有所帮助。
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
Android获取系统联系人信息的实例一、获取手机联系人姓名及手机号//跳转到系统联系人应用Intentintent=newIntent(Intent.ACTI
Android如何获取手机联系人信息,本文为大家揭晓。获取手机联系人信息步骤:1、获取ContentResolverContentResolverresolve
本文实例介绍了Android开发中对联系人修改、新增联系人的方法,通过本实例代码可实现添加联系人、编辑修改联系人,新增联系人和更新联系人等操作,操作主要放在线程
本文实例为大家分享了Android实现手机联系人读取和插入的具体代码,供大家参考,具体内容如下目标效果:先在模拟器随便添加两个联系人,运行程序,再打开联系人,发
本文实例为大家分享了Android获取手机联系人列表的具体代码,供大家参考,具体内容如下下面直接贴代码1.先写一个实体类,来放名字和号码publicclassP