I am getting the Sent and Received SMS from almost devices. but i am not able to get it from specific device in specific model. Like samsungs S7, S8, S9. and some LG devices.
Strange is some S7 device works and some not.
Below is the way i am using.
Registering observer in Application class.
private void registerMessageObserver() {
String url = "content://sms/";
Uri uri = Uri.parse(url);
this.getApplicationContext()
.getContentResolver()
.registerContentObserver(uri, true,
new MessageObserver(new Handler(), this));
}
//Observer class
import android.content.Context;
import android.database.ContentObserver;
import android.os.Handler;
public class MessageObserver extends ContentObserver {
private static final String TAG = "MessageObserver";
Context moContext;
// Observe message state incoming or outgoing
public MessageObserver(Handler handler, Context foContext) {
super(handler);
moContext = foContext;
}
@Override
public boolean deliverSelfNotifications() {
Log.i(TAG, "deliverSelfNotifications Changed");
return true;
}
@Override
public void onChange(boolean selfChange) {
Log.i(TAG, "Message Changed");
super.onChange(selfChange);
Log.i(TAG, "Message Changed after super");
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && moContext .checkCallingOrSelfPermission(Manifest.permission.READ_SMS) != PackageManager.PERMISSION_GRANTED)
return;
String[] laColumns = new String[]{"_id", "type", "address", "body", "date", "person"};
Uri loUriAllMessages = Uri.parse("content://sms/");
Cursor loCursor = moContext.getContentResolver().query(loUriAllMessages, laColumns, null, null, "_id DESC");
if (loCursor != null) {
if (loCursor.moveToFirst()) {
int liMessageId = loCursor.getInt(loCursor.getColumnIndex("_id"));
String lsType = loCursor.getString(loCursor.getColumnIndex("type")).trim();
//Doing my stuff here
}
}
if (loCursor != null)
loCursor.close();
}
}
It work like charm as i said in most of devices. but some device did not call this even onChange did not calling.
I tried below way but it will not even call onChanged in any scenario.
private void registerMessageObserverSent() {
String url = "content://sms/sent";
Uri uri = Uri.parse(url);
this.getApplicationContext()
.getContentResolver()
.registerContentObserver(uri, true,
new MessageObserverSent(new Handler(), this));
}
private void registerMessageObserverOut() {
String url = "content://sms/out";
Uri uri = Uri.parse(url);
this.getApplicationContext()
.getContentResolver()
.registerContentObserver(uri, true,
new MessageObserverOut(new Handler(), this));
}
I tried Broadcast receiver but the receiver is only for the Received message.
Can anyone help how to resolve the issue for those devices.
The app is offline so no issue with play store policy. its for my personal use.