欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  技术分享

Android获取手机通话记录_读取短信内容源代码

程序员文章站 2021-11-22 11:15:48
...

Demo是使用程序读取手机通话记录和获取短信内容的小功能、通话记录可以读取电话号码和时间、另外还包括状态、短信也是同样的道理、可以通过代码去读取手机里面短信内容、发短信的号码、以及时间、当然还可以读取到是接收短信还是发送短信等等、当然前提是要保证你手机里面有通话记录和短信内容、下面是截图

Android获取手机通话记录_读取短信内容源代码


获取短信代码

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 nameColumn = cur.getColumnIndex("person");   
            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();  
        }          
        
}


Android获取通话记录和短信记录源代码下载链接: 点击下载获取通话记录和短信记录源码 密码: ivvw