gpt4 book ai didi

android - 检测应用程序是否从 'outside' 应用程序启动/恢复

转载 作者:太空宇宙 更新时间:2023-11-03 10:47:36 25 4
gpt4 key购买 nike

我目前正在构思一个应用程序的功能,我想要一种通用的方法/途径来检测应用程序本身是从“”应用程序启动还是恢复。 p>

在外面”,在这种情况下,意味着:

  • 应用程序由启动器图标启动/恢复
  • 通过按导航栏/按键上的“应用按钮”启动/恢复应用(如在 Nexus 7 上)
  • 应用从通知开始/恢复
  • 应用程序是从“其他地方”启动/恢复的

此功能的用例如下:

  • 该应用具有“多用户功能”,允许用户为其数据创建一个或多个配置文件
  • 单个配置文件可能受 PIN/密码保护,以对应用的其他用户“隐藏”数据,或对安装该应用的设备的其他用户“隐藏”数据
    • 如果配置文件设置了密码,应用程序将在应用程序启动/恢复时向当前用户显示某种锁定屏幕
      • 如果输入正确,应用程序将正常启动并显示上次选择的配置文件的数据
      • 如果输入不正确,应用程序将以“中性”个人资料或根本没有个人资料开始

我在网上搜索了一些想法,只在 stackoverflow 上找到了相关帖子:

根据我到目前为止所阅读和了解到的内容,解决方案似乎比我想象的要复杂,而且没有开箱即用的解决方案。

我目前正在考虑一种基于时间标记的方法来实现此功能:

  • 将时间标志设置为受影响 Activity 的成员变量
  • onCreate(Bundle savedInstanceState) --> 在检查 savedInstanceState Budle 中的数据之前,标志设置为“null”
    • 这会检测到 Activity 启动 --> 如果设置了密码 --> 显示锁定屏幕
  • onSaveInstanceState(Bundle) --> 将时间标志设置为“当前时间”
  • 如果 onCreate(Bundle savedInstanceState) 恢复,savedInstanceState 将包含一个时间标志
    • 计算当前时间和应用上次暂停时间之间的差异
    • 如果这个差异高于某个阈值,例如30 分钟 --> 如果设置了密码 --> 显示锁定屏幕

也许你们中的一些人已经实现了类似的东西或者对这个问题/方法有一些意见。我很高兴听到您的想法。

干杯

最佳答案

这是一个较旧的问题,但仍然相关。有一个使用 ActivityLifeCycleCallbacks 的简单解决方案。此答案源自此 blogpost迈克尔·布拉德肖 (Micahel Bradshaw) 着。他解释了这个概念

The key is in understanding how activities coordinate with each other. When switching between activities A and B, their methods are called in this order:

A.onPause();

B.onCreate();

B.onStart();

B.onResume(); (Activity B now has user focus)

A.onStop(); (if Activity A is no longer visible on screen)

解决方案:您创建一个实现 Application.ActivityLifecycleCallbacks 接口(interface)的类,并对恢复和停止的 Activity 进行计数。

public class AppLifecycleHelper implements Application.ActivityLifecycleCallbacks {

// I use two separate variables here. You can, of course, just use one and
// increment/decrement it instead of using two and incrementing both.
private static int resumed;
private static int stopped;

public void onActivityCreated(Activity activity, Bundle savedInstanceState) {
}

public void onActivityDestroyed(Activity activity) {
}

public void onActivityResumed(Activity activity) {
++resumed;
}

public void onActivityPaused(Activity activity) {
}

public void onActivitySaveInstanceState(Activity activity, Bundle outState) {
}

public void onActivityStarted(Activity activity) {
if (!isApplicationInForeground()){
// resumed and stopped both are 0,
// that means it is the first activity to come on display
// i.e. App is launched from outside the app
}
}

public void onActivityStopped(Activity activity) {
++stopped;
if (!isApplicationInForeground()){
// resumed and stopped both are 0
// That means there is NO Activity in resumed state when this activity stopped
// i.e. User came out of the App, perform all Application wide persistence tasks over here
}
}

/**
* Checks whether App is visible to the user or not
* @return true if visible and false otherwise
*/
public static boolean isApplicationInForeground() {
return resumed > stopped;
}

}

然后在您的应用程序的 onCreate() 中为这样的 Activity 回调注册此类

registerActivityLifecycleCallbacks(new AppLifecycleHelper());

就是这样!无需为每个 Activity 添加任何代码。一切都包含在 AppLifecycleHelper 中,只需几行代码。

关于android - 检测应用程序是否从 'outside' 应用程序启动/恢复,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20126231/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com