Android手机用户隐私获取,包括读取通讯录、读取通话记录、读取浏览器历史记录、读取手机短信

     最近正好做到有关Android用户的隐私内容获取的问题,包括读取通讯录、读取通话记录、读取浏览器历史记录、读取手机短信;现在把完整的解决代码贴上

 其中,需要权限如下:

   
    
      
    
    
     
    
     

详细代码如下:

/**
 * 获取手机通讯录 包括name(名称)和phoneNumber(号码)以;分隔	
 * @return
 */
	public ArrayList getPhoneContacts(){
		ArrayList contacts = new ArrayList();
		try{
			ContentResolver resolver = mContext.getContentResolver();
			Cursor phoneCursor = resolver.query(Phone.CONTENT_URI,null, null, null, null);
			if(phoneCursor!=null){
				while(phoneCursor.moveToNext()){
					int nameIndex = phoneCursor.getColumnIndex(Phone.DISPLAY_NAME);
					String name = phoneCursor.getString(nameIndex);
					String phoneNumber = phoneCursor.getString(phoneCursor.getColumnIndex(Phone.NUMBER));
					if(TextUtils.isEmpty(phoneNumber)){
						continue;
					}
					contacts.add(name+";"+phoneNumber);
				}
				phoneCursor.close();  
			}
		}catch(Exception e){
			e.printStackTrace();
		}		
		return contacts;
	}
/**
 * 获取SIM卡通讯录 包括name(名称)和phoneNumber(号码)以;分隔
 * @return
 */
	public ArrayList getSIMContacts(){
		ArrayList contacts = new ArrayList();
		try{
			ContentResolver resolver = mContext.getContentResolver();  
			Uri uri = Uri.parse("content://icc/adn");  
			Cursor phoneCursor = resolver.query(uri, null, null, null, null);
			if(phoneCursor!=null){
				while(phoneCursor.moveToNext()){
					String name = phoneCursor.getString(phoneCursor.getColumnIndex("name"));
					String phoneNumber = phoneCursor.getString(phoneCursor.getColumnIndex("number"));
					if(TextUtils.isEmpty(phoneNumber)){
						continue;
					}
					contacts.add(name+";"+phoneNumber);
				}
				phoneCursor.close();  
			}
		}catch(Exception e){
			e.printStackTrace();
		}
		
		return contacts;
	}
/**
 * 获取通话记录	包括type date cachedName number
 * @return
 */
	@SuppressLint("SimpleDateFormat")
	public ArrayList getCallLogs(){
		ArrayList callLogs = new ArrayList();
		String[] projection = {
				CallLog.Calls.DATE, // 日期  
                CallLog.Calls.NUMBER, // 号码  
                CallLog.Calls.TYPE, // 类型  
                CallLog.Calls.CACHED_NAME, // 名字  
		};
		SimpleDateFormat sfd = new SimpleDateFormat("yyyy-MM-dd-hh:mm:ss");  
        Date date;
        try{
        	ContentResolver cr = mContext.getContentResolver();
            Cursor cursor = cr.query(CallLog.Calls.CONTENT_URI, projection, null, null,CallLog.Calls.DEFAULT_SORT_ORDER);
            if (cursor != null && cursor.getCount() > 0){
            	cursor.moveToFirst(); 
            	while(cursor.moveToNext()){
            		date = new Date(cursor.getLong(cursor  
                            .getColumnIndex(CallLog.Calls.DATE)));  
                    String number = cursor.getString(cursor  
                            .getColumnIndex(CallLog.Calls.NUMBER));  
                    int type = cursor.getInt(cursor  
                            .getColumnIndex(CallLog.Calls.TYPE));  
                    String cachedName = cursor.getString(cursor  
                            .getColumnIndex(CallLog.Calls.CACHED_NAME));
                    String callLog = cltype[type]+";"+sfd.format(date)+";"+cachedName+";"+number;
                    callLogs.add(callLog);                
            	}
            	cursor.close();
            }
        }catch(Exception e){
        	e.printStackTrace();
        }       
		return callLogs;
	}
/**
 * 获取浏览器历史记录	包括date title url
 * @return
 */
	@SuppressLint("SimpleDateFormat")
	public ArrayList getBrowserHistory(){
		ArrayList browserHistory = new ArrayList();
		try{
			ContentResolver contentResolver = mContext.getContentResolver(); 
			Cursor cursor = contentResolver.query( Uri.parse("content://browser/bookmarks"), 
					new String[] { "title", "url", "date" }, "date!=?",  
	                null, "date desc");
			SimpleDateFormat sfd = new SimpleDateFormat("yyyy-MM-dd-hh:mm:ss");  
	        Date date;
			if(cursor != null ){
				while(cursor.moveToNext()) {
					date = new Date(cursor.getLong(cursor.getColumnIndex("date")));
					String title = cursor.getString(cursor.getColumnIndex("title"));
					String url = cursor.getString(cursor.getColumnIndex("url"));
					String browserhistory = sfd.format(date)+";"+title+";"+url;
					browserHistory.add(browserhistory);
				}
				cursor.close();
			}		
		}catch(Exception e){
			e.printStackTrace();
		}		
		return browserHistory;		
	}
/**
 * 获取手机短信	
 * @return
 */
	@SuppressLint("SimpleDateFormat")
	public ArrayList getSmsInPhones(){
		ArrayList smsInPhones = new ArrayList();
		final String SMS_URI_ALL   = "content://sms/";      
	    try{
	    	ContentResolver cr = mContext.getContentResolver();  
	        String[] projection = new String[]{"_id", 
	        		"address",  //号码
	        		"person",   //姓名
	                "body",     //短信内容
	                "date",     //日期
	                "type"};    //短信类型
	        Uri uri = Uri.parse(SMS_URI_ALL);  
	        Cursor cur = cr.query(uri, projection, null, null, "date desc");
	        if (cur.moveToFirst()){
	        	int nameColumn = cur.getColumnIndex("person");  
	            int phoneNumberColumn = cur.getColumnIndex("address");  
	            int smsbodyColumn = cur.getColumnIndex("body");  
	            int dateColumn = cur.getColumnIndex("date");  
	            int typeColumn = cur.getColumnIndex("type");
	            while(cur.moveToNext()){
	            	String name = cur.getString(nameColumn);               
	            	String phoneNumber = cur.getString(phoneNumberColumn);  
	            	String smsbody = cur.getString(smsbodyColumn).replaceAll(";", ".");  
	                  
	                SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd-hh:mm:ss");  
	                Date d = new Date(Long.parseLong(cur.getString(dateColumn)));  
	                String date = dateFormat.format(d); 
	                int typeId = cur.getInt(typeColumn);
	                String type = smstype[typeId];
	                String smsInPhone = type+";"+date+";"+name+";"+phoneNumber+";"+smsbody;
	                smsInPhones.add(smsInPhone);
	            }
	            cur.close();
	        }
	    }catch(Exception e){
	    	e.printStackTrace();
	    }
		return smsInPhones;
	}

项目包下载地址 http://download.csdn.net/detail/u011342532/7961491

你可能感兴趣的:(android,用户隐私,android,通讯录,短信)