Пожалуйста, проверьте следующие классы для получения SMS:
SmsFromDevice smsFromDevice = new SmsFromDevice(mContext, SmsDetailsActivity.this);
ArrayList<SmsRetriveModel> smsRetrieveModels = smsFromDevice.getSMSofNumber(numberToGetSms, "All", "1469385000000");
Log.d(TAG,smsRetrieveModels.size());
Вы можете изменить "1469385000000" lastSyncDate.
SmsFromDevice. java
class SmsFromDevice {
private static final String TAG = SmsFromDevice.class.getSimpleName();
private Context mContext;
private Activity mActivity;
private String numberToGetSms = "";
private ArrayList<SmsRetriveModel> arrSmsDetails;
SmsFromDevice(Context context, Activity activity) {
this.mContext = context;
this.mActivity = activity;
}
//type: Inbox|All //no:(eg:9898989898) //lastSyncDate in milliseconds
ArrayList<SmsRetriveModel> getSMSofNumber(String no, String type, String lastSyncDate) {
numberToGetSms = no;
if (checkSMSPermission()) {
final ContentResolver contentResolver = mContext.getContentResolver();
Uri myMessage = null;
if (type.equals("Inbox")) {
myMessage = Uri.parse("content://sms/inbox");
} else if (type.equals("All")) {
myMessage = Uri.parse("content://sms/");
}
String filter = "date> " + lastSyncDate + " and address='" + numberToGetSms + "'";
Cursor curSms = contentResolver.query(myMessage, null, filter, null, "date DESC");
if (curSms.getCount() > 0) {
arrSmsDetails = getSmsInModel(curSms);
curSms.close();
} else {
Log.e("===NULL", "SMS not found!!Cursor null...");
}
} else {
getSMSPermission();
}
return arrSmsDetails;
}
private ArrayList<SmsRetriveModel> getSmsInModel(Cursor c) {
try {
arrSmsDetails = new ArrayList<>();
if (c.moveToFirst()) {
do {
if (c.getString(c.getColumnIndexOrThrow("address")) == null) {
c.moveToNext();
continue;
}
String number = c.getString(c.getColumnIndexOrThrow("address"));
LogUtils.logD("===NUMBER", "" + number);
SmsRetriveModel smsRetriveModel = new SmsRetriveModel();
String dateInMilli = c.getString(c.getColumnIndex("date"));
String msgBody = c.getString(c.getColumnIndex("body"));
SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy", Locale.getDefault());
String dateFormatted = formatter.format(new Date(Long.parseLong(dateInMilli)));
smsRetriveModel.setSmsMilliSec(dateInMilli);
smsRetriveModel.setMobileNo(numberToGetSms);
smsRetriveModel.setSmsBody(msgBody);
smsRetriveModel.setSmsDate(dateFormatted);
arrSmsDetails.add(smsRetriveModel);
//}
} while (c.moveToNext());
}
} catch (Exception e) {
LogUtils.logE(TAG, e);
} finally {
c.close();
}
return arrSmsDetails;
}
private boolean checkSMSPermission() {
String permission = "android.permission.READ_SMS";
int res = mContext.checkCallingOrSelfPermission(permission);
return (res == PackageManager.PERMISSION_GRANTED);
}
private void getSMSPermission() {
int version = android.os.Build.VERSION.SDK_INT;
if (version >= 23) {
if (ContextCompat.checkSelfPermission(mContext, Manifest.permission.READ_EXTERNAL_STORAGE)
!= PackageManager.PERMISSION_GRANTED || ContextCompat.checkSelfPermission(mContext, Manifest.permission.READ_SMS)
!= PackageManager.PERMISSION_GRANTED) {
if (ActivityCompat.shouldShowRequestPermissionRationale(mActivity,
Manifest.permission.READ_EXTERNAL_STORAGE) || ActivityCompat.shouldShowRequestPermissionRationale(mActivity,
Manifest.permission.READ_SMS)) {
Log.e(TAG, "shouldShowRequestPermissionRationale");
}
ActivityCompat.requestPermissions(mActivity, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.READ_SMS}, 1);
}
}
}
}
SmsRetriveModel. java
public class SmsRetriveModel {
private String mobileNo,smsDate,smsSender,smsBody,smsMilliSec;
public String getMobileNo() {
return mobileNo;
}
public void setMobileNo(String mobileNo) {
this.mobileNo = mobileNo;
}
public String getSmsDate() {
return smsDate;
}
public void setSmsDate(String smsDate) {
this.smsDate = smsDate;
}
public String getSmsSender() {
return smsSender;
}
public void setSmsSender(String smsSender) {
this.smsSender = smsSender;
}
public String getSmsBody() {
return smsBody;
}
public void setSmsBody(String smsBody) {
this.smsBody = smsBody;
}
public String getSmsMilliSec() {
return smsMilliSec;
}
public void setSmsMilliSec(String smsMilliSec) {
this.smsMilliSec = smsMilliSec;
}
}