Get the implementation code for all SMS messages in Android phone

  • 2020-06-07 05:16:54
  • OfStack

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 = Uri.parse(SMS_URI_ALL); 
Cursor cur = cr.query(uri, projection, null, null, "date desc"); 

if (cur.moveToFirst()) { 
String name; 
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{ 
name = cur.getString(nameColumn); 
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 = " receive "; 
} else if(typeId == 2){ 
type = " send "; 
} else { 
type = ""; 
} 

smsBuilder.append("["); 
smsBuilder.append(name+","); 
smsBuilder.append(phoneNumber+","); 
smsBuilder.append(smsbody+","); 
smsBuilder.append(date+","); 
smsBuilder.append(type); 
smsBuilder.append("] "); 

if(smsbody == null) smsbody = ""; 
}while(cur.moveToNext()); 
} else { 
smsBuilder.append("no result!"); 
} 

smsBuilder.append("getSmsInPhone has executed!"); 
} catch(SQLiteException ex) { 
Log.d("SQLiteException in getSmsInPhone", ex.getMessage()); 
} 
return smsBuilder.toString(); 
}

Note:
1. This function is used to get all SMS messages in the phone, including inbox, outbox, draft box, etc.
2. This function can be run in the Service subclass, because the related functions of Activity class are not used.
3. The obtained SMS include: SMS name, mobile phone number, SMS content, SMS sending and receiving time, and SMS type.
Main structure of sms:
_id: SMS serial number, such as 100
thread_id: The sequence number of a conversation, such as 100, is the same as that of a text message sent to the same phone number
address: Sender address, i.e. mobile phone number, such as +8613811810000
person: Sender, if the sender is in the address book, specific name, stranger null
date: Date, long type, e.g. 1256539465022, date display format can be set
protocol: Protocol 0SMS_RPOTO SMS, 1MMS_PROTO MMS
read: Read 0 unread, 1 read
status: SMS receiving state - 1, 0 complete, 64 pending, 128 failed
type: Type 1 is received and type 2 is sent
body: The details of the message
service_center: SMS service Center number, such as +8613800755500
4. In order to get short messages, you need to add permissions in the file AndroidManifest.xml, as follows:
< uses-permissionandroid:name="android.permission.READ_SMS"/ >
5. This function passed the test on the real machine.


Related articles: