安卓手机各种信息获取---(通讯录短信息通话记录)

由于最近公司在做手机信息采集项目,做得很吃力,效果也不理想。

加上今天负能量较多,谢谢一些近日搜索回来的安卓手机经验。

1.目标

在PC端程序获取手机中的各种信息,主要通讯录、短信息、通话记录等

2.过程

(1)开发环境:由于在PC端是无法直接通过ADB接口直接获取手机中的通讯录等信息的,所以花了些时间重新拿起JAVA。用起来真的非常不习惯,包括环境部署,项目建立,基本流程都一头雾水。通过在csdn下载了一些android开发教学视频手把手的部署了环境,并看了一些基本的开发流程,花了一天时间,大概能做出个小程序在手机上运行了。

(2)获取思路:android开发环境搭建好了之后,开始考虑如何获取手机中的各项信息。由于对android开发不熟悉,所以在获取信息的方法也都是在网上搜索寻找合适的调用方法并稍作修改。主要三项,通讯录、短信息、通话记录。

public List getThjl(){
    	
    	List listThjl=new ArrayList();
    	Date date;
    	String time= "";
    	final Cursor cursor = getContentResolver().query(CallLog.Calls.CONTENT_URI, new String[]{CallLog.Calls.NUMBER,CallLog.Calls.CACHED_NAME,CallLog.Calls.TYPE, CallLog.Calls.DATE}, null, null,CallLog.Calls.DEFAULT_SORT_ORDER);
    	        for (int i = 0; i < cursor.getCount(); i++) {  
    	        	String[] thjlItem=new String[4];
    	            cursor.moveToPosition(i);
    	            thjlItem[0]=cursor.getString(0);
    	            thjlItem[1]=cursor.getString(1);
    	            thjlItem[2]=cursor.getString(2);
    	            
    	            listThjl.add(thjlItem);
    	            SimpleDateFormat sfd = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
    	            date = new Date(Long.parseLong(cursor.getString(3)));
    	            time = sfd.format(date);
    	            thjlItem[3]=time;
    	}
    	return listThjl;
    }
    public List getSmsInPhone() {
		final String SMS_URI_ALL = "content://sms/";
		List MsgList=new ArrayList();
		try {
			Uri uri = Uri.parse(SMS_URI_ALL);
			String[] projection = new String[] { "_id", "address", "person", "body", "date", "type" };
			
			Cursor cur = getContentResolver().query(uri, projection, null, null, "date desc");		// 获取手机内部短信

			if (cur.moveToFirst()) {
				int index_Address = cur.getColumnIndex("address");
				int index_Person = cur.getColumnIndex("person");
				int index_Body = cur.getColumnIndex("body");
				int index_Date = cur.getColumnIndex("date");
				int index_Type = cur.getColumnIndex("type");

				do {
					String[] msgItem=new String[5];
					String strAddress = cur.getString(index_Address);
					int intPerson = cur.getInt(index_Person);
					String strbody = cur.getString(index_Body);
					long longDate = cur.getLong(index_Date);
					int intType = cur.getInt(index_Type);

					SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
					Date d = new Date(longDate);
					String strDate = dateFormat.format(d);

					String strType = "";
					if (intType == 1) {
						strType = "接收";
					} else if (intType == 2) {
						strType = "发送";
					} else {
						strType = "null";
					}

					msgItem[0]=strAddress;
					msgItem[1]=intPerson+"";
					msgItem[2]=strbody;
					msgItem[3]=strDate;
					msgItem[4]=strType;
					MsgList.add(msgItem);
				} while (cur.moveToNext());

				if (!cur.isClosed()) {
					cur.close();
					cur = null;
				}
			} 
		} catch (SQLiteException ex) {
			Log.d("SQLiteException in getSmsInPhone", ex.getMessage());
		}

		return MsgList;
	}
    private List> getGxr() {
        List> items = new ArrayList>();

        Cursor cur = null;
        try {
                cur = getContentResolver().query(
                                ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
                if (cur.moveToFirst()) {
                        int idColumn = cur.getColumnIndex(
                                        ContactsContract.Contacts._ID);
                int displayNameColumn = cur.getColumnIndex(
                                ContactsContract.Contacts.DISPLAY_NAME);
                do {
                                String contactId;
                                String displayName;
                                String phoneNumber = "";
                                contactId = cur.getString(idColumn);
                                displayName = cur.getString(displayNameColumn);
                                int numberCount = cur.getInt(cur.getColumnIndex(
                                                ContactsContract.Contacts.HAS_PHONE_NUMBER));
                                if (numberCount>0) {
                                        Cursor phones = getContentResolver().query(
                                                        ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                                                        null,
                                                        ContactsContract.CommonDataKinds.Phone.CONTACT_ID 
                                                        + " = " + contactId 
                                                       ,
                                                        null, null);
                                        if (phones.moveToFirst()) {
                                                int numberColumn = phones.getColumnIndex(
                                                                ContactsContract.CommonDataKinds.Phone.NUMBER);
                                                do {
                                                        phoneNumber += phones.getString(numberColumn) + ",";
                                                } while (phones.moveToNext());
                                        } 
                                }
                                HashMap i = new HashMap();
                                i.put("name", displayName);
                                i.put("key", phoneNumber);
                                items.add(i);
                        } while (cur.moveToNext());
                } else {
                        HashMap i = new HashMap();
                        i.put("name", "Your Phone");
                        i.put("key", "Have No Contacts.");
                        items.add(i);
                }
        }catch( Exception ex ){
        	String exMsg=ex.getMessage();
        	new AlertDialog.Builder(MainActivity.this).setMessage("异常:"+exMsg).setPositiveButton("确定", null).show(); 
        	
        } finally {
                if (cur != null)
                        cur.close();
        }
        return items;
}
在自己开发的apk程序中可以获取到信息后,开始考虑如何与PC通讯传送到PC。开始在网上搜索了一些方法都是说socket通讯来实现,但由于没有socket使用的经验,还是自己想了一个简单的方法来实现数据交换。

首先把自己开发的apk包通过ADB推送到目标手机安装,并通过ADB启动该程序,该程序启动后自动获取手机信息,并以写文件形式生成XML文件在手机上。而在PC端程序在启动该程序后是无法获知该程序的运行状态的,所以PC程序就还是通过ADB不断判断监测手机端指定路径下的XML文件是否生成。如果生成了,则通过ADB拷贝的本地PC并解析获取数据(当然如果apk程序运行报错也可以通过写文件形式交换错误信息给PC端)。个人觉得通过该方式交换数据速度会较快,但状态却增加了不可控,有改进空间。

附ADB解疑:adb.exe,做过安卓开发应该都了解,是安卓手机与pc通讯的一个程序。只有手机开启调试模式,并且PC安装好驱动后,在设备管理器中显示为后,才可使用ADB通讯(包括APK安装,文件拷贝推送等)。

使用到的CMD命令:

1.安装自己的apk包: adb install "d:/xx.apk"

2.启动自己的程序: adb shell am start -n com.xdh.xdhandroid/com.xdh.xdhandroid.MainActivity(后半部分为  apk命名空间/apk程序名)

3.从手机拷贝XML文件到PC端: adb pull /sdcard/XX.xml "d:/xx.xml"
基本流程就这样,算是这几天研究的成果吧。PS:vs与eclipse交换着用真不习惯,快捷键截然不同。

你可能感兴趣的:(手机信息)