我目前正在为应用程序构思一项功能,我希望有一种通用方法/方法来检测应用程序本身是否已从 ' 启动或恢复outside' 该应用程序。
'Outside',在这种情况下,意味着:
- 应用程序由启动器图标启动/恢复
- 通过按导航栏/按键上的“应用程序按钮”来启动/恢复应用程序(就像在 Nexus 7 上一样)
- 应用程序是从通知启动/恢复的
- 应用程序是从“其他地方”启动/恢复的
此功能的用例如下:
- 该应用程序具有“多用户功能”,允许用户为其数据创建一个或多个配置文件
- A single profile may be pin/password protected to 'hide' the data from other user(s) of the app, or 'hide' data from other user(s) of the device where the app is installed
- If a profile has a password set, the app will show some kind of a lock screen to the current user when the app is started/resumed
- 如果输入正确,应用程序将正常启动,显示最后选择的配置文件的数据
- 如果输入错误,应用程序将以“中性”配置文件或根本没有配置文件启动
我在网上搜索了想法,只在 stackoverflow 上找到了相关帖子:
- 有什么方法可以区分 Android Activity onResume 和主屏幕吗?
- Android - 检测从家庭或历史记录启动的应用程序
- 确定应用程序是否从主屏幕启动?
从我迄今为止阅读和了解到的情况来看,解决方案似乎比我想象的更复杂,并且没有现成的解决方案。
我目前正在考虑一个基于时间标志的实现该功能的方法:
- 将时间标志设置为受影响活动的成员变量
-
onCreate(Bundle savedInstanceState)
--> flag is set to 'null' before checking the savedInstanceState Budle for data
- 这检测到活动开始 --> 如果设置了密码 --> 显示锁定屏幕
-
onSaveInstanceState(Bundle)
--> 将时间标志设置为“当前时间”
- if
onCreate(Bundle savedInstanceState)
is resumed, savedInstanceState will contain a time flag
- 计算当前时间与应用上次暂停时间之间的差异
- 如果此差异高于某个阈值,例如30 分钟 --> 如果设置了密码 --> 显示锁定屏幕
也许你们中的一些人已经实现了类似的东西,或者确实对这个问题/方法有一些投入。
我很高兴听到你的想法。
Cheers
这是一个较旧的问题,但仍然相关。有一个使用 ActivityLifeCycleCallbacks 的简单解决方案。这个答案来源于此blogpost作者:迈克尔·布拉德肖。他解释了这个概念
关键在于理解活动如何相互协调。当在 Activity A 和 B 之间切换时,它们的方法按以下顺序调用:
A.onPause();
B.onCreate();
B.onStart();
B.onResume();
(活动 B 现在具有用户焦点)
A.onStop();
(如果 Activity A 在屏幕上不再可见)
解决方案:您创建一个实现 Application.ActivityLifecycleCallbacks 接口的类并记录恢复和停止的活动的计数。
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());
就是这样!无需为每个活动添加任何代码。一切都包含在 AppLifecycleHelper 中,只需几行代码。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)