`

手机联系人偷偷读取contentprovider(9)

阅读更多

                            作为一名程序猿,最大的痛苦莫过于陪电脑代码度过了大多数时间,而陪你的他(她花费了太少时间,当你熬夜掉发,为IT的你不顾形象,形容枯槁。是不是会想知道你的他(她)在这一段时间干了什么呢,是不是想获取他的通讯录,别着急,作为一个Android的程序员,我们先做到获取他手机的联系人,偷偷给他的手机安一个小程序,读取全部信息,然后调用信息功能全部发过来,嘿嘿,闲话不多说让我们开始吧

想要在不同应用程序间交换信息,有一个非常重要的类,ContentProvider,这个类提供各个程序之间的数据库标准,联系人contactprovider,我们需要ID,以及联系人,为了方便起见我们用listview显示联系人,id,手机号码长按出现上下文菜单显示。

首先定义主布局函数:

<LinearLayout 
	xmlns:android="http://schemas.android.com/apk/res/android"
	android:orientation="vertical" 
	android:layout_width="fill_parent"
	android:layout_height="fill_parent">
	<TextView
		android:id="@+id/mainInfo"
		android:textSize="20px"
		android:layout_width="wrap_content"
		android:layout_height="wrap_content"
		android:text="手机联系人列表"/>
	<ListView
		android:id="@+id/contactsList"
		android:layout_width="fill_parent"
		android:layout_height="fill_parent"/>
</LinearLayout>

 然后是模板定义:

<TableLayout 
	xmlns:android="http://schemas.android.com/apk/res/android"
	android:orientation="vertical" 
	android:layout_width="fill_parent"
	android:layout_height="fill_parent">
	<TableRow>
		<TextView
			android:id="@+id/_id"
			android:layout_height="wrap_content"
			android:layout_width="30px" />
		<TextView
			android:id="@+id/name"
			android:layout_height="wrap_content"
			android:layout_width="300px" />
	</TableRow>
</TableLayout>

 

然后是主函数Activity,这里实现是通过给的uri,uri相当于地址,找到联系人数据库,然后一一存到map里,然后list添加map,然后适配器封装list,之后就是listview添加适配器显示。

public class MainActivity extends Activity {

	
	private Cursor result = null ;	// 既然要查询,查询返回的就是结果
	private ListView contactsList = null ;	// 定义ListView组件
	private List<Map<String,Object>> allContacts = null ;
	private SimpleAdapter simple = null ;

	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		this.contactsList=(ListView)super.findViewById(R.id.contactsList);
		//这里获取通讯录联系人URI里的内容
		this.result = super.getContentResolver().query(
				ContactsContract.Contacts.CONTENT_URI, null, null, null, null);	// 查询
		super.startManagingCursor(this.result) ;	// 将结果集交给容器管理
		this.allContacts = new ArrayList<Map<String,Object>>() ;	// 实例化List集合
		for (this.result.moveToFirst(); !this.result.isAfterLast(); this.result
				.moveToNext()) {	// 取出结果集中的每一个内容
			Map<String,Object> contact = new HashMap<String,Object>() ;
			//将通讯录里人id取出,标准格式
			contact.put("_id", this.result.getInt(this.result
					.getColumnIndex(ContactsContract.Contacts._ID)));
			contact.put("name", this.result.getString(this.result
					.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME)));
			this.allContacts.add(contact) ;
		}
		this.simple = new SimpleAdapter(this, this.allContacts,
				R.layout.contact, new String[] { "_id", "name" }, new int[] {
						R.id._id, R.id.name });
		this.contactsList.setAdapter(this.simple) ;
		super.registerForContextMenu(this.contactsList); // 注册菜单
	}

	

 

然后定义上下文菜单显示手机号码:

public void onCreateContextMenu(ContextMenu menu, View v,
			ContextMenuInfo menuInfo) { // 创建菜单
		super.onCreateContextMenu(menu, v, menuInfo);
		menu.setHeaderTitle("联系人操作");
		menu.add(Menu.NONE, Menu.FIRST + 1, 1, "查看详情");
		menu.add(Menu.NONE, Menu.FIRST + 2, 1, "删除信息");
	}
	public boolean onContextItemSelected(MenuItem item) {
		AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item
				.getMenuInfo();
		int position = info.position; // 取得操作位置
		String contactsId = this.allContacts.get(position).get("_id").toString() ;
		switch(item.getItemId()){	// 进行菜单的操作
		case Menu.FIRST + 1:	// 查看
			String phoneSelection = ContactsContract.CommonDataKinds.Phone.CONTACT_ID
					+ "=?";
			String[] phoneSelectionArgs = new String[] { contactsId};
			Cursor c = super.getContentResolver().query(
					ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
					phoneSelection, phoneSelectionArgs, null);
			StringBuffer buf = new StringBuffer() ;
			buf.append("电话号码是:") ;
			for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) {
				buf.append(
						c.getString(c
								.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)))
						.append("、");
			}
			Toast.makeText(this, buf, Toast.LENGTH_SHORT)
					.show();
			break ;
		case Menu.FIRST + 2:	// 删除
			super.getContentResolver().delete(
					Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_URI,
							contactsId), null, null);
			this.allContacts.remove(position) ;	// 删除集合数据项
			this.simple.notifyDataSetChanged() ;	// 通知改变
			Toast.makeText(this, "数据已删除!", Toast.LENGTH_SHORT).show();
			break ;
		}
		return super.onContextItemSelected(item);
	}

 

显示效果如下:



这里我们做到了读取,下节课我们做盗取通讯记录,无论是谁打给他,她你都会知道

想要做到实时监控么,那么就跟着学习到通信章节吧,当然这种小软件不要玩过火,适当的关心是关心,过分的关心可是会bug的
 
 

  • 大小: 67.5 KB
  • 大小: 53 KB
1
1
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics