How to listen to new Sms in Android

笑着哭i 提交于 2019-12-23 02:58:15

问题


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.



来源:https://stackoverflow.com/questions/4470318/how-to-listen-to-new-sms-in-android

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!