android 下不同进程是通过ContentResolver共享数据的,下面通过这个类去查询手机上所有联系人的信息和通话记录,包含号码、内容、日期,通话时长等信息

一、获取手机短信信息

public void getSmsInPhone()   {       final String SMS_URI_ALL   = "content://sms/";                    try{           ContentResolver cr = 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()) {                 String phoneNumber;                      String smsbody;               String date;               String type;                             int phoneNumberColumn = cur.getColumnIndex("address");               int smsbodyColumn = cur.getColumnIndex("body");               int dateColumn = cur.getColumnIndex("date");               int typeColumn = cur.getColumnIndex("type");                           do{                   phoneNumber = cur.getString(phoneNumberColumn);                 smsbody = cur.getString(smsbodyColumn);                                      SimpleDateFormat dateFormat = new SimpleDateFormat(                           "yyyy-MM-dd hh:mm:ss");                   Date d = new Date(Long.parseLong(cur.getString(dateColumn)));                   date = dateFormat.format(d);                                      int typeId = cur.getInt(typeColumn);                   if(typeId == 1){                       type = "接收";                   } else if(typeId == 2){                       type = "发送";                   } else {                       type = "";                   }                                   Message msg = mHandler.obtainMessage(0);                  Bundle b = new Bundle();// 存放数据              b.putString("phone", phoneNumber);                b.putString("smsbody", smsbody);                b.putString("data", date);                b.putString("type", type);                msg.setData(b);                  msg.sendToTarget();                                                               if(smsbody == null) smsbody = "";                 }while(cur.moveToNext());           }     } catch(SQLiteException ex) {           Log.d("SQLiteException in getSmsInPhone", ex.getMessage());       }    }  

二、获取通话记录

private void GetCallsInPhone(){Cursor cursor = getContentResolver().query(Calls.CONTENT_URI,      new String[] { Calls.DURATION, Calls.TYPE, Calls.DATE, Calls.NUMBER },      null,      null,      Calls.DEFAULT_SORT_ORDER);  MainActivity.this.startManagingCursor(cursor);  boolean hasRecord = cursor.moveToFirst();  long incoming = 0L;  long outgoing = 0L;  int count = 0;  String strPhone = "";String date; while (hasRecord) {      int type = cursor.getInt(cursor.getColumnIndex(Calls.TYPE));      long duration = cursor.getLong(cursor.getColumnIndex(Calls.DURATION));      strPhone = cursor.getString(cursor.getColumnIndex(Calls.NUMBER));    SimpleDateFormat dateFormat = new SimpleDateFormat(                           "yyyy-MM-dd hh:mm:ss");                   Date d = new Date(Long.parseLong(cursor.getString(cursor.getColumnIndex(Calls.DATE))));                   date = dateFormat.format(d);                                 Message msg = mHandler.obtainMessage(1);                  Bundle b = new Bundle();// 存放数据                  b.putString("phone", strPhone);                b.putString("date", date);                b.putLong("time", duration);                msg.setData(b);                  msg.sendToTarget();         switch (type) {          case Calls.INCOMING_TYPE:              incoming += duration;              break;          case Calls.OUTGOING_TYPE:              outgoing += duration;          default:              break;      }      count++;      hasRecord = cursor.moveToNext();  }  }

三、通过handler去解析内容并输出日志

private Handler mHandler = new Handler() {    @Override    public void handleMessage(Message msg) {      // TODO Auto-generated method stub      switch(msg.what){      case 0:      {        Bundle b = msg.getData();        String phone = b.getString("phone");      String smsbody = b.getString("smsbody");      String data = b.getString("data");      String type = b.getString("type");      Log.d("phone", phone);      Log.d("smsbody", smsbody);      Log.d("data", data);      Log.d("type", type);          }      break;     case 1:    {    Bundle b = msg.getData();     String phone = b.getString("phone");    String date = b.getString("date");    long time = b.getLong("time");    Log.d("phone", phone);    Log.d("date", date);    Log.d("time", ""+time);    }    break;   }     super.handleMessage(msg);     }  };  

四、由于搜索手机短信和通话信息是一项耗时的事情,所以需要启动一个线程去完成这些事情

@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);new Thread(){public void run(){getSmsInPhone();GetCallsInPhone();}}.start();}

最后需要注意的是要启动访问权限

     

最后奉上源码下载地址: http://download.csdn.net/detail/m370809968/6639563

更多相关文章

  1. Android(安卓)获取手机中微信聊天记录并后台发送到邮箱
  2. Android中获取屏幕信息的几种方式
  3. Android中的Sqlite数据库操作总结
  4. Android相关知识集合
  5. Android实现BMP和PNG转换为JPEG格式
  6. Android客户端通过GET和POST向服务器发送数据
  7. android 广播的知识积累
  8. 由sqlite在手机上的存储位置,引发的onCreate在哪里执行的总结
  9. Android中ContentValues用法

随机推荐

  1. Android(安卓)侧滑菜单的实现
  2. 通过WifiManager,DhcpInfo获取android IP
  3. Android(安卓)获取状态栏的高度
  4. Android启动优化
  5. linux基础教程--安装Android(安卓)SDK
  6. android之View属性
  7. cocos2d-x3.0beta版+NDK-r9b在android上
  8. android点击邮箱链接跳转发送
  9. Android(安卓)深入解析selector
  10. Android(安卓)ScrollView 判断到顶到底,和