如何知道我们的应用程序已经在Android的后台 [英] how to know our app has gone to background in android

查看:144
本文介绍了如何知道我们的应用程序已经在Android的后台的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当用户进入从我的应用程序或presses装置home键的其他应用程序从我的应用程序或等等,那么我的应用程序将被发送到后台。并在后台正在做的一些东西。那么如何知道我的应用程序已经为背景。一种解决方案将是检查是否有任何应用程序的活动都在的OnStart,onrestart,onresume,在onPause,的onStop如果不是那么我们可以在后台考虑。但这种解决方案是单调乏味的,请帮助我,如果你有一个简单的解决方案

When user goes to other apps from my app or presses device home button from my app or etc , then my app will be sent to background. And in the background am doing some stuff . So how to know is my app has gone to background . One solution will be to check if any of app activities are in onstart,onrestart,onresume,onpause,onstop if not then we can consider it in background. But this solution is tedious please help me if you have a easy solution

推荐答案

使用的onPause 不繁琐,并且可能是你唯一的解决办法呢。创建一个抽象的活动类,所有的活动类扩展,做任何你需要在的onPause 有:

Using onPause isn't tedious, and may be your only solution anyway. Create an abstract Activity class that all your Activity classes extend and do whatever you need to do in onPause there:

public abstract class BaseActivity extends Activity {

    @Override
    public void onPause() {

        super.onPause();
        // Do whatever you need to do in onPause here
    }
}

使用这个例子中,所有的活动类现在应该延长 BaseActivity

这篇关于如何知道我们的应用程序已经在Android的后台的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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