Android 中的关机事件 [英] Shutdown Events in Android

查看:41
本文介绍了Android 中的关机事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有可能拦截电话关机事件或对此使用不同的黑客?我有一个始终运行的服务,它是收集一些分析数据的非市场应用程序的一部分.我想告诉分析结束会话和报告,然后恢复系统关闭.

Is it possible to intercept a phone shutdown event or perhaps use a different hack around this? I have an always running service that's part of a non-market application which is collecting some analytics data. I'd like to tell the analytics to end session and report, then resume the system shutdown.

我已经尝试从服务的 onDestroy 或 onShutdown 部分触发事件,但手机会在没有时间报告的情况下关机.

I've tried triggering the event from the onDestroy or onShutdown parts of the services, but the phone will shutdown without giving it time to report.

通过根据活动屏幕进行许多简短的会话来解决这个问题,会产生我想避免的无意义的分析信息.

Working around this by making many short sessions based on the activity screens yields non-sensical analytics information that I'd like to avoid.

推荐答案

在捕获 ACTION_SHUTDOWN 意图时构建一个广播接收器.

Build a broadcast receiver at catches the ACTION_SHUTDOWN intent.

在 onReceive() 方法中,您可以完成您想要做的事情.

In the onReceive() method you can accomplish what you are trying to do.

http://developer.android.com/reference/android/content/Intent.html#ACTION_SHUTDOWN

这篇关于Android 中的关机事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆