如何知道我们的应用程序已经在Android的后台 [英] how to know our app has gone to background in 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屋!