I want to write an app that will do something when it received a message. I have google for answer for many days and found some solution like this one: Link but it is not working on android 6 or marshmallow at all. So is there a way to wake up my app to do something whenever it received a message on android marshmallow?
Asked
Active
Viewed 377 times
1
-
your app has need to run time permission for read sms. – iAndroid Jul 20 '16 at 13:12
-
@iAndroid, yeah, i have those permission, read sms, SMS received, sent sms. But it did not work on android MM, but working on lower version like jellybean instead. – Nara Na Jul 21 '16 at 02:53
-
can you share code? – iAndroid Jul 21 '16 at 04:22
-
@iAndroid, you can check the "Link" in the description or question. I follow that link too. It work well on Jellybeans devices i have, except running on MM devices. – Nara Na Jul 21 '16 at 05:58
-
which device you test app? Because some device has not such functionality? Like in Mi device there is no auto detected functionality. – iAndroid Jul 21 '16 at 07:29
-
@iAndroid: i use emulator and galaxy s5 to test it out. – Nara Na Jul 22 '16 at 11:54
1 Answers
1
Below example work for me in MM also
BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
if (Telephony.Sms.Intents.SMS_RECEIVED_ACTION.equals(intent.getAction())) {
final Bundle bundle = intent.getExtras();
try {
if (bundle != null) {
final Object[] pdusObj = (Object[]) bundle.get("pdus");
for (int i = 0; i < pdusObj.length; i++) {
SmsMessage currentMessage = SmsMessage.createFromPdu((byte[]) pdusObj[i]);
String phoneNumber = currentMessage.getDisplayOriginatingAddress();
String senderNum = phoneNumber;
String message = currentMessage.getDisplayMessageBody();
Logger.e("sender Num" + senderNum);
Logger.e("message" + message);
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
};
IntentFilter filter = new IntentFilter(Telephony.Sms.Intents.SMS_RECEIVED_ACTION);
registerReceiver(broadcastReceiver, filter);
onDestroy method unregister broadcast receiver
unregisterReceiver(broadcastReceiver);

iAndroid
- 951
- 7
- 18
-
code is look alike to the one in the link that i test too, my friend. : ( – Nara Na Jul 26 '16 at 08:21