Java core code:
public String getSmsInPhone() { final String SMS_URI_ALL = "content://sms/"; final String SMS_URI_INBOX = "content://sms/inbox"; final String SMS_URI_SEND = "content://sms/sent"; final String SMS_URI_DRAFT = "content://sms/draft"; StringBuilder smsBuilder = new StringBuilder(); try{ ContentResolver cr = getContentResolver(); String[] projection = new String[]{"_id", "address", "person", "body", "date", "type"}; Uri uri = (SMS_URI_ALL); Cursor cur = (uri, projection, null, null, "date desc"); if (()) { String name; String phoneNumber; String smsbody; String date; String type; int nameColumn = ("person"); int phoneNumberColumn = ("address"); int smsbodyColumn = ("body"); int dateColumn = ("date"); int typeColumn = ("type"); do{ name = (nameColumn); phoneNumber = (phoneNumberColumn); smsbody = (smsbodyColumn); SimpleDateFormat dateFormat = new SimpleDateFormat( "yyyy-MM-dd hh:mm:ss"); Date d = new Date(((dateColumn))); date = (d); int typeId = (typeColumn); if(typeId == 1){ type = "take over"; } else if(typeId == 2){ type = "send"; } else { type = ""; } ("["); (name+","); (phoneNumber+","); (smsbody+","); (date+","); (type); ("] "); if(smsbody == null) smsbody = ""; }while(()); } else { ("no result!"); } ("getSmsInPhone has executed!"); } catch(SQLiteException ex) { ("SQLiteException in getSmsInPhone", ()); } return (); }
Notes:
1. This function is used to obtain all text messages in the mobile phone, including inbox, outbox, draft box, etc.
2. This function can be run in the Service subclass because the relevant functions of the Activity class are not used.
3. The text messages obtained include: the name of the sending and receiving text messages, mobile phone number, text messages content, time of sending and receiving text messages, and the type of text messages.
Main structure of sms:
_id: SMS serial number, such as 100
thread_id: The serial number of the conversation, such as 100, is the same as the text message sent to the same mobile phone number.
address: the sender's address, that is, the mobile phone number, such as +8613811810000
person: sender, if the sender is in the address book, it is the specific name, and strangers are null
date: date, long type, such as 1256539465022, the date display format can be set
protocol: protocol 0SMS_RPOTO SMS, 1MMS_PROTO MMS
read: whether it is read 0 not read, 1 has been read
status: SMS status-1 reception, 0complete, 64pending, 128failed
type: SMS type 1 is received, 2 is sent
body: SMS specific content
service_center: SMS service center number, such as +8613800755500
4. In order to obtain text messages, you need to add permission instructions to the file, as follows:
<uses-permissionandroid:name=".READ_SMS"/>
5. This function was tested and passed on the real machine.