jjzjj

Android 相当于 applicationDidBecomeActive 和 applicationWillResignActive(来自 iOS)

coder 2023-12-12 原文

我希望它存在。

我想存储应用程序失去焦点的时间,然后检查它是否失去焦点超过 n 分钟以调出锁。

看到一个应用程序是如何由 Activity 组成的,我认为不会有直接的等价物。我怎样才能达到类似的结果?

编辑
我尝试将 Application 类扩展到 registerActivityLifecycleCallbacks() 并意识到我将无法使用这种方法,因为它仅在 API 级别 14+ 中可用

最佳答案

请允许我分享我如何制定向后兼容的解决方案。

如果有与帐户关联的密码,我已经在启动时锁定了我的应用程序。为了完整起见,我需要处理其他应用程序(包括家庭 Activity )接管 n 分钟的情况。

我最终创建了一个 BaseActivity,我的所有 Activity 都会扩展。

// DataOperations is a singleton class I have been using for other purposes.
/* It is exists the entire run time of the app
   and knows which activity was last displayed on screen.
   This base class will set triggeredOnPause to true if the activity before
   "pausing" because of actions triggered within my activity.  Then when the
   activity is paused and triggeredOnPause is false, I know the application
   is losing focus.

   There are situations where an activity will start a different application 
   with an intent.  In these situations (very few of them) I went into those 
   activities and hard-coded these lines right before leaving my application

   DataOperations datao = DataOperations.sharedDataOperations();
   datao.lostFocusDate = new Date();
*/

import java.util.Date;

import android.app.Activity;
import android.content.Intent;
import android.util.Log;

public class BaseActivity extends Activity {
    public boolean triggeredOnPause;

    @Override 
    public void onResume(){
        super.onResume();
        DataOperations datao = DataOperations.sharedDataOperations();
        if (datao.lostFocusDate != null)    {
            Date now = new Date();
            long now_ms = now.getTime();
            long lost_focus_ms = datao.lostFocusDate.getTime();
            int minutesPassed = (int) (now_ms-lost_focus_ms)/(60000);
            if (minutesPassed >= 1) {
                datao.displayLock();
            }
                    datao.lostFocusDate = null;
        }
        triggeredOnPause = false;
    }

    @Override
    public void onPause(){
        if (triggeredOnPause == false){
            DataOperations datao = DataOperations.sharedDataOperations();
            datao.lostFocusDate = new Date();
        }
        super.onPause();
    }
    @Override
    public void startActivity(Intent intent)
    {
        triggeredOnPause = true;
        super.startActivity(intent);
    }
    @Override
    public void startActivityForResult(Intent intent, int requestCode)  {
        triggeredOnPause = true;
        super.startActivityForResult(intent, requestCode);
    }

}

如果您打算使用此解决方案并且在实现我的 DataOperations 类的等效项时遇到问题,请发表评论,我可以发布必要的代码。

关于Android 相当于 applicationDidBecomeActive 和 applicationWillResignActive(来自 iOS),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8846811/

有关Android 相当于 applicationDidBecomeActive 和 applicationWillResignActive(来自 iOS)的更多相关文章

  1. ruby - 如何验证 IO.copy_stream 是否成功 - 2

    这里有一个很好的答案解释了如何在Ruby中下载文件而不将其加载到内存中:https://stackoverflow.com/a/29743394/4852737require'open-uri'download=open('http://example.com/image.png')IO.copy_stream(download,'~/image.png')我如何验证下载文件的IO.copy_stream调用是否真的成功——这意味着下载的文件与我打算下载的文件完全相同,而不是下载一半的损坏文件?documentation说IO.copy_stream返回它复制的字节数,但是当我还没有下

  2. Ruby 文件 IO 定界符? - 2

    我正在尝试解析一个文本文件,该文件每行包含可变数量的单词和数字,如下所示:foo4.500bar3.001.33foobar如何读取由空格而不是换行符分隔的文件?有什么方法可以设置File("file.txt").foreach方法以使用空格而不是换行符作为分隔符? 最佳答案 接受的答案将slurp文件,这可能是大文本文件的问题。更好的解决方案是IO.foreach.它是惯用的,将按字符流式传输文件:File.foreach(filename,""){|string|putsstring}包含“thisisanexample”结果的

  3. Python 相当于 Perl/Ruby ||= - 2

    这个问题在这里已经有了答案:关闭10年前。PossibleDuplicate:Pythonconditionalassignmentoperator对于这样一个简单的问题表示歉意,但是谷歌搜索||=并不是很有帮助;)Python中是否有与Ruby和Perl中的||=语句等效的语句?例如:foo="hey"foo||="what"#assignfooifit'sundefined#fooisstill"hey"bar||="yeah"#baris"yeah"另外,类似这样的东西的通用术语是什么?条件分配是我的第一个猜测,但Wikipediapage跟我想的不太一样。

  4. java - 什么相当于 ruby​​ 的 rack 或 python 的 Java wsgi? - 2

    什么是ruby​​的rack或python的Java的wsgi?还有一个路由库。 最佳答案 来自Python标准PEP333:Bycontrast,althoughJavahasjustasmanywebapplicationframeworksavailable,Java's"servlet"APImakesitpossibleforapplicationswrittenwithanyJavawebapplicationframeworktoruninanywebserverthatsupportstheservletAPI.ht

  5. Get https://registry-1.docker.io/v2/: net/http: request canceled while waiting - 2

    1.错误信息:Errorresponsefromdaemon:Gethttps://registry-1.docker.io/v2/:net/http:requestcanceledwhilewaitingforconnection(Client.Timeoutexceededwhileawaitingheaders)或者:Errorresponsefromdaemon:Gethttps://registry-1.docker.io/v2/:net/http:TLShandshaketimeout2.报错原因:docker使用的镜像网址默认为国外,下载容易超时,需要修改成国内镜像地址(首先阿里

  6. 安卓apk修改(Android反编译apk) - 2

    最近因为项目需要,需要将Android手机系统自带的某个系统软件反编译并更改里面某个资源,并重新打包,签名生成新的自定义的apk,下面我来介绍一下我的实现过程。APK修改,分为以下几步:反编译解包,修改,重打包,修改签名等步骤。安卓apk修改准备工作1.系统配置好JavaJDK环境变量2.需要root权限的手机(针对系统自带apk,其他软件免root)3.Auto-Sign签名工具4.apktool工具安卓apk修改开始反编译本文拿Android系统里面的Settings.apk做demo,具体如何将apk获取出来在此就不过多介绍了,直接进入主题:按键win+R输入cmd,打开命令窗口,并将路

  7. java - Ruby 相当于 Java 的 Collections.unmodifiableList 和 Collections.unmodifiableMap - 2

    Java的Collections.unmodifiableList和Collections.unmodifiableMap在Ruby标准API中是否有等价物? 最佳答案 使用freeze应用程序接口(interface):Preventsfurthermodificationstoobj.ARuntimeErrorwillberaisedifmodificationisattempted.Thereisnowaytounfreezeafrozenobject.SeealsoObject#frozen?.Thismethodretur

  8. ruby - 为什么不能使用类IO的实例方法noecho? - 2

    print"Enteryourpassword:"pass=STDIN.noecho(&:gets)puts"Yourpasswordis#{pass}!"输出:Enteryourpassword:input.rb:2:in`':undefinedmethod`noecho'for#>(NoMethodError) 最佳答案 一开始require'io/console'后来的Ruby1.9.3 关于ruby-为什么不能使用类IO的实例方法noecho?,我们在StackOverflow上

  9. python - Ruby 相当于 Python str[3 :] - 2

    是否有Ruby等效于Python的方法来获取在字符串末尾结束的子字符串,如str[3:]?必须输入字符串的长度并不方便。 最佳答案 传递最后一个元素=-1的范围str[3..-1] 关于python-Ruby相当于Pythonstr[3:],我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/12978768/

  10. ruby - 可以正常中断的来自 Rake 的长时间运行的 shell 命令? - 2

    在几个项目中,我希望有一个类似rakeserver的rake任务,它将通过任何需要的方式开始为该应用程序提供服务。这是一个示例:task:serverdo%x{bundleexecrackup-p1234}end这行得通,但是当我准备停止它时,按Ctrl+c并没有正常关闭;它中断了Rake任务本身,它说rakeaborted!并给出堆栈跟踪。在某些情况下,我必须执行Ctrl+c两次。我可能可以用Signal.trap写一些东西来更优雅地中断它。有没有更简单的方法? 最佳答案 trap('SIGINT'){puts"Yourmessa

随机推荐