关于活动、服务和应用的一些基本问题
我阅读了 Android 开发者指南。我对 Android 应用程序有了很多了解。但现在我很想开发一个应用程序。我脑子里有一些基本问题。我很高兴从专家那里得到答案。
在我的应用程序中,我需要检查从手机中取出 SIM 卡以及放回 SIM 卡时的通知。要检查这一点,哪一个是好主意?我应该在我的应用程序中实现一个计时器,还是应该创建一个可以响应 SIM 卡移除的服务。
如果我实现一个计时器来定期执行某项工作,那么如果所有活动都在后台,计时器仍然可以运行吗?
I read the android developer guide. I got idea about android applcations a lot. But now I really want to develop one application. I have some basic questions in my mind. I would be glad to get their answers from experts.
In my application I need to check for the notification when the SIM card is removed from the phone and when put back. To check this which one will be the good idea? Is it a timer I should implement in my application or should I create a service which can respond to SIM card removal.
If I implement a timer to do a certain job at regular time interval, will the timer still can run if all the activities are in the background?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在我见过的所有手机上,为了取出 SIM 卡,都必须关闭手机,因为电池通常会堵塞 SIM 卡插槽。因此,基本上您的应用程序无法收到有关 SIM 卡删除的通知,因为手机已关闭。您可以做的是在手机重新启动后通过 BroadcastReceiver 监听 BOOT_COMPLETED 来启动服务:请参阅 本教程。然后,服务通过
TelephonyManager 检查 SIM 是否可用。 getSimState()
。不一定。 Android 操作系统决定何时终止并从内存中删除不活动的应用程序。无法保证您的非活动(例如在后台)应用程序不会被删除。要保证按计划执行,请参阅
AlarmManager
。On all phones that I've seen in order to remove SIM the phone must be shut down, because battery usually blocks the SIM slot. So basically your app can not be notified about SIM removal because phone is off. What you can do is start your Service after phone restarts, via BroadcastReceiver listening for BOOT_COMPLETED: see this tutorial. Service then checks if SIM is available via
TelephonyManager.getSimState()
.Not necessarily. Android OS decides when to kill off and remove from memory the inactive apps. There is no guarantee that your inactive (e.g. in the background) app will not be removed. To guarantee scheduled execution look at
AlarmManager
.对于第一种情况,您可能需要一个
BroadCast Reciever
(前提是当SIM卡被移除时发送广播。对于第二种情况,
Alarm Manager
是比计时器更好的选择。for first case you probably need a
BroadCast Reciever
(Provided broadcast are sent when sim Card is removed.and for 2nd case
Alarm Manager
is better choice than timer.