如何在Android中以编程方式从设备中读取SMS消息?我想从设备中检索短信并显示它们吗?
3 回答
红糖糍粑
TA贡献1815条经验 获得超6个赞
// public static final String INBOX = "content://sms/inbox";// public static final String SENT = "content://sms/sent"; // public static final String DRAFT = "content://sms/draft"; Cursor cursor = getContentResolver().query(Uri.parse("content://sms/inbox"), null, null, null, null); if (cursor.moveToFirst()) { // must check the result to prevent exception do { String msgData = ""; for(int idx=0;idx<cursor.getColumnCount();idx++) { msgData += " " + cursor.getColumnName(idx) + ":" + cursor.getString(idx); } // use msgData } while (cursor.moveToNext());} else { // empty box, no SMS}
呼如林
TA贡献1798条经验 获得超3个赞
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { final String myPackageName = getPackageName(); if (!Telephony.Sms.getDefaultSmsPackage(this).equals(myPackageName)) { Intent intent = new Intent(Telephony.Sms.Intents.ACTION_CHANGE_DEFAULT); intent.putExtra(Telephony.Sms.Intents.EXTRA_PACKAGE_NAME, myPackageName); startActivityForResult(intent, 1); }else { List<Sms> lst = getAllSms(); } }else { List<Sms> lst = getAllSms(); }
@Overrideprotected void onActivityResult(int requestCode, int resultCode, Intent data) {if (requestCode == 1) { if (resultCode == RESULT_OK) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { final String myPackageName = getPackageName(); if (Telephony.Sms.getDefaultSmsPackage(mActivity).equals(myPackageName)) { List<Sms> lst = getAllSms(); } } }}}
public List<Sms> getAllSms() { List<Sms> lstSms = new ArrayList<Sms>(); Sms objSms = new Sms(); Uri message = Uri.parse("content://sms/"); ContentResolver cr = mActivity.getContentResolver(); Cursor c = cr.query(message, null, null, null, null); mActivity.startManagingCursor(c); int totalSMS = c.getCount(); if (c.moveToFirst()) { for (int i = 0; i < totalSMS; i++) { objSms = new Sms(); objSms.setId(c.getString(c.getColumnIndexOrThrow("_id"))); objSms.setAddress(c.getString(c .getColumnIndexOrThrow("address"))); objSms.setMsg(c.getString(c.getColumnIndexOrThrow("body"))); objSms.setReadState(c.getString(c.getColumnIndex("read"))); objSms.setTime(c.getString(c.getColumnIndexOrThrow("date"))); if (c.getString(c.getColumnIndexOrThrow("type")).contains("1")) { objSms.setFolderName("inbox"); } else { objSms.setFolderName("sent"); } lstSms.add(objSms); c.moveToNext(); } } // else { // throw new RuntimeException("You have no SMS"); // } c.close(); return lstSms;}
public class Sms{private String _id;private String _address;private String _msg;private String _readState; //"0" for have not read sms and "1" for have read smsprivate String _time;private String _folderName; public String getId(){return _id;}public String getAddress(){return _address;}public String getMsg(){return _msg;} public String getReadState(){return _readState;}public String getTime(){return _time;} public String getFolderName(){return _folderName;}public void setId(String id){_id = id;} public void setAddress(String address){_address = address;}public void setMsg(String msg){_msg = msg;} public void setReadState(String readState){_readState = readState;}public void setTime(String time){_time = time;} public void setFolderName(String folderName){_folderName = folderName;}}
<uses-permission android:name="android.permission.READ_SMS" />
红颜莎娜
TA贡献1842条经验 获得超12个赞
SmsMmsMessage getSmsDetails(Context context, long ignoreThreadId, boolean unreadOnly) long findMessageId(Context context, long threadId, long _timestamp, int messageTypevoid setMessageRead(Context context, long messageId, int messageType)void deleteMessage(Context context, long messageId, long threadId, int messageType)
SmsMmsMessage getSmsDetails(Context context, long ignoreThreadId, boolean unreadOnly){ String SMS_READ_COLUMN = "read"; String WHERE_CONDITION = unreadOnly ? SMS_READ_COLUMN + " = 0" : null; String SORT_ORDER = "date DESC"; int count = 0; // Log.v(WHERE_CONDITION); if (ignoreThreadId > 0) { // Log.v("Ignoring sms threadId = " + ignoreThreadId); WHERE_CONDITION += " AND thread_id != " + ignoreThreadId; } Cursor cursor = context.getContentResolver().query( SMS_INBOX_CONTENT_URI, new String[] { "_id", "thread_id", "address", "person", "date", "body" }, WHERE_CONDITION, null, SORT_ORDER); if (cursor != null) { try { count = cursor.getCount(); if (count > 0) { cursor.moveToFirst(); // String[] columns = cursor.getColumnNames(); // for (int i=0; i<columns.length; i++) { // Log.v("columns " + i + ": " + columns[i] + ": " + cursor.getString(i)); // } long messageId = cursor.getLong(0); long threadId = cursor.getLong(1); String address = cursor.getString(2); long contactId = cursor.getLong(3); String contactId_string = String.valueOf(contactId); long timestamp = cursor.getLong(4); String body = cursor.getString(5); if (!unreadOnly) { count = 0; } SmsMmsMessage smsMessage = new SmsMmsMessage(context, address, contactId_string, body, timestamp, threadId, count, messageId, SmsMmsMessage.MESSAGE_TYPE_SMS); return smsMessage; } } finally { cursor.close(); } } return null;}
- 3 回答
- 0 关注
- 832 浏览
添加回答
举报
0/150
提交
取消