Android的最佳途径检测和处理用户不活动途径、用户、Android

2023-09-05 10:25:52 作者:辗啭

活动是一个非常重要的事件。对于许多应用程序,如果用户不与之交互的若干秒的时间来恢复应用程序,并回到主要活动注销,或节省电力。所以我真的很想得到检测最好的方法的一些反馈。事实上,我认为每个人都会从一个很好的解决这个受益。

所以我的问题是双重的:

1)是否有更好的方法来检测用户活动比使用相结合    activity.onUserInteraction()重置CountDownTimer?

注:一个国家报告缺点这种方法的缺点是softkeypad互动可能不          抓住了这一做法。

注:另有报道缺点是CountDownTimer是关闭主线程,可能不会更新          正确。我不知道有多大的问题,这是?

注:CountDownTimer似乎有取消的问题,以及:           如何停止/取消安卓CountDownTimer

2)让我们说,onUserInteraction()/ CountDownTimer是最佳/只解决这个问题    还存在着一些问题:

一)应在每次活动推出自己的倒计时?

b)如果一个倒数计时器在每个活动的onCreate方法重新启动?

C)可以说,我想调暗屏幕或转到主要活动在倒计数结束,其中       应该超时处理程序放置在何处?在每一个活动?在一个服务?

感谢

解决方案

只是偶然发现了这个问题,为的我已经回答了类似的东西刚才。

就个人而言,我会选择选项2,您所建议的,把一个计时器到一个单所以它可以跨所有活动。世界上没有需要单独的倒数计时器,除非你有特别的要求,反应在你的应用程序的不同功能不同。

为什么要重置在OnCreate计时器?你应该做的每一个用户与应用程序进行交互,如在activity.onUserInteraction()方法。时间

从我的 previous引述回答:

  

您需要多花点心思投资到什么你   要求在这里,但我可以告诉你要保持   跟踪用户交互,并且如果由于一个期限届满   最后的互动,执行一些动作,你的情况记录出来   您的应用程序。

     

首先,你需要一些地方,你可以跟踪当最​​后一个   互动发生,因为你会想这是应用广泛的你   可以使用单持有本,或重写应用程序类,   无论采用哪种方式应该做的。

     

接下来,你需要开始跟踪用户交互。从你的   活动中,你可以重写onUserInteraction方法,这得到   调用任何时候用户与应用程序如键相互作用   事件。每当你打这个方法,更新你的单,让它   知道事情已经发生了,一个时间戳。

     

最后,你需要某种形式的循环检查,如果不断地检查   最近事情已经发生了。即使世界各种就是这样做的,你   可以有一个连续的环,当前的时间戳进行比较,以对   最后记录的事件,有点草案code:

 ,而(真)
{
   如果(timeLastEventRecorded≤(现在 -  15))
   {
      //什么也没发生在15分钟内,所以采取纠正措施
   }
}
 

  

presumably你已经有一些code。在您的应用程序   照顾注销的,当用户点击退出,如,你   应该只能够调用上面的样本。

B2B供应链管理平台主流技术架构方案

Inactivity is a very important EVENT. For many apps if the user does not interact with it for a certain number of seconds its time to reset the app and go back to main activity logout, or conserve power. So I would really like to get some feedback on the best way to detect this. In fact I think everyone will benefit from a good solution to this.

So my question is twofold:

1) Is there a better way to detect user inactivity than using a combination of activity.onUserInteraction() to reset a CountDownTimer?

Note: One reported downside to this approach is that softkeypad interaction might not be caught by this approach.

Note: Another reported downside is the CountDownTimer is off main thread and might not update correctly. I am not sure how big an issue this is?

Note: CountDownTimer appears to have cancellation issues as well: how to stop/cancel android CountDownTimer

2) Lets say that onUserInteraction()/CountDownTimer is the best/only solution to this problem there are still some questions:

a) should each activity launch its own countdown timer?

b) Should a single countdown timer be restarted in the onCreate method of each activity?

c) lets say I want to dim the screen or goto main activity when the countdown expires where should the timeout handler be located? In each activity? In a service?

Thanks

解决方案

Just stumbled upon this question as I've answered something similar just now.

Personally, I'd opt for option 2 that you have suggested, and put a timer into a singleton so its available across all activities. Theres no need for a separate countdown timer unless you have a specific requirement to react different under different features of your application.

Why would you want to reset the timer in the onCreate? You should do that each time the user interacts with the application, such as in the activity.onUserInteraction() method.

To quote from my previous answer:

You'll need to invest a little thought into exactly what your requirements are here, but from what I can tell, you want to keep track of the user interactions and if a time limit expires since the last interaction, perform some action, in your case logging them out of your application.

Firstly, you'll need some place that you can track when the last interaction occured, since you'll want this to be application wide you could use a singleton to hold this, or override the Application class, either way should do.

Next, you'll need to start tracking user interactions. From your activities, you can override the onUserInteraction method, this gets invoked anytime the user interacts with the application such as key event. Each time you hit this method, update your singleton and let it know something has happened, with a timestamp.

Finally, you'll need some kind of looping check to constantly check if anything has happened recently. Theres various was of doing this, you could have a continuous loop that compares current timestamp to the last recorded event, a bit of draft code :

while(true)
{
   if (timeLastEventRecorded < (now - 15))
   {
      //nothing has happened in 15 minutes, so take corrective action
   }
}

Presumably you'll already have some code in your application that takes care of logouts, such as when the user clicks "logout", you should just be able to invoke that in the sample above.