How to listen to new Sms in Android

2019-06-09 00:56发布

问题:

I am trying to listen to new sms(Message) with the help of BroadcastReceiver, but it is keep on listening to messages throughout the application and even after closing the app, so what to do??

Whether there is any other way to listen to sms or latest method as IntentReceiver kind of solutions(available over I-net) are deprecated and not working

回答1:

Hey Ankit, just check this out, is this what u needed, .............

https://github.com/vivdub/DynamicSMSReceiver

Thanks



回答2:

If you register an Activity or Service as a BroadcastReceiver in the AndroidManifest.xml file, then the broadcast notifying it of the message will be received even if your Activity isn't open. If you only want your app to receive a notification that an SMS has arrived ONLY when it is open, you need to register a BroadcastReciever for SMS messages in the onResume method, using one of the registerReceiver() methods and then un-register it in your Activity's onPause() method using unregisterReceiver(). That way your app will stop listening for SMS messages when it isn't open.



回答3:

You need to dynamically register and unregister your BroadcastReceiver. So, when you detect that your application gets closed, just unregister your receiver. See more here.



标签: android sms