在Android中我想运行谁可以在后台运行也倒数计时器 [英] In Android i want to run countdown timer who can run in background also

查看:1697
本文介绍了在Android中我想运行谁可以在后台运行也倒数计时器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

喜的朋友正在开发一个应用程序中,我的要求是在后台运行的计时器也
喜欢流行的糖果游戏美眉
我的实际需求是,当我在一天打开我的应用程序第一次像我想开始倒计时24:00:00
一段时间后,想我离开我的应用程序,并打开其他应用程序在同一时间我的倒数计时器必须运行它永远不会被暂停或它永远不会被停止

Hi Friends am developing one app in which my requirement is to run the timer in background also like popular candy crush game my actual requirement is like when i open my app first time in a day i want to start countdown timer 24:00:00 after some time suppose i leave my application and open other application during same time my countdown timer must be running it will never be pause or it will never be stop

我参观差不多8,9教程,但我没有得到确切的结果,请帮助我我可以用什么样的阶级或任何教程链接,请在此先感谢

i visit almost 8,9 tutorial but am not getting exact result please help me out what kind of class may i use or any tutorial link please thanks in advance

推荐答案

您有两种选择:服务键,应用程序启动的存储时间。 服务可以通过系统在任何时候被杀死,所以你必须以节省的onDestroy 方法的时间,然后当系统将在那里你需要恢复计时器重新启动你的服务,系统会调用的onCreate 方法。第二种方法是比较容易。你可以存储上启动您的应用程序当前的时间,然后就检查的差异。示例:

You have two options: Service and storing time of app launching. Service could be killed by system at any time, so you have to save time on onDestroy method and then when system will relaunch your Service system will call onCreate method where you need to restore timer. The second method is easier. You could store current time on starting your app and then just check differences. Sample:

@Override
protected void onResume() {
    super.onResume();

    final SharedPreferences appPrefs = getSharedPreferences("appPrefs", MODE_PRIVATE);
    final long launchTime = appPrefs.getLong("launchTime", 0);
    final long currentTime = System.currentTimeMillis();

    if(launchTime == 0){
        //first launch
        appPrefs.edit().putLong("launchTime", currentTime);
    }else{
        long diff = currentTime - launchTime;
        long minutesPassed = diff / 1000 / 60;

        if(24 * 60 <= minutesPassed){
            //24 hours passed since app launching
        }else{
            //24 hours didn't passed since app launching
        }
    }
}

这篇关于在Android中我想运行谁可以在后台运行也倒数计时器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆