安卓应用程序启动的计数 [英] Android: count number of app launches
问题描述
我需要计数用户启动一个应用程序的次数。我的共享preference存储的价值,并把计数逻辑中的onCreate()
。
I need to count the number of times the user launches an app. I'm storing the value in a shared preference, and putting the count logic inside onCreate()
.
现在,因为的onCreate()
被称为在许多不同的方案,并不是所有的包括用户实际启动应用程序,我使用的保存的实例状态包存储)一个标志的onSaveInstanceState(设置
- 如果该标志不存在,我认为用户手动启动该应用程序
Now, since onCreate()
is called in a number of different scenarios, not all of which include the user actually launching the application, I'm using the saved instance state bundle to store a flag set in OnSaveInstanceState()
-- if the flag is absent, I consider the user started the app manually.
这是必要的,或者只是检查传递给的onCreate()
够空
捆绑?在这两种情况下,为什么?
Is this necessary, or is just checking for a null
bundle passed to onCreate()
enough? In either case, why?
推荐答案
首先,感谢大家对你的答案。他们都帮助一些这样或那样的。
First off, thank you all for your answers. They all helped some way or another.
这是说,在几次尝试和不同的方法后,我认为这是确定当用户已经推出的应用程序没有实际100%可靠的方法。活动生命周期回调都不能真正用于解决这个问题,无论是单独还是合并。
That said, after several tries and different approaches, I think there is no actual 100% reliable way of determining when the user has launched the app. None of the activity life-cycle callbacks can really be used tackle the issue, neither individually nor combined.
我得到的是计数 onResume的()
呼叫的号码,然后在的onPause从中减去()最近的
根据 isFinishing的回报()
。这种做法,但是,不占home键,其中也许其他的东西,用户可以做到隐藏的应用程序。
The closest I got was counting the number of onResume()
calls, and then substracting from it in onPause()
depending on the return of isFinishing()
. This approach, however, doesn't account for the home button, among maybe other things a user can do to hide apps.
我会更新这个答案,如果我曾经找到一个方法来可靠地做到这一点。
I will update this answer if I ever find a way to do this reliably.
这篇关于安卓应用程序启动的计数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!