如何在用户返回后台应用程序时触发功能?

时间:2012-11-22 11:50:21

标签: android ios android-homebutton home-button

例如,用户打开应用程序,按主页按钮,然后再次返回应用程序。

当用户导航回应用时,有没有办法触发某些功能?例如,当用户返回应用程序时自动加载视图对象。

这个问题适用于Android和Android。的iOS。

4 个答案:

答案 0 :(得分:1)

在项目的AppDelegate.m文件(仅限iOS)

中使用以下内容
- (void)applicationWillResignActive:(UIApplication *)application
{
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

- (void)applicationDidEnterBackground:(UIApplication *)application
{
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. 
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

- (void)applicationWillEnterForeground:(UIApplication *)application
{
    // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
}

- (void)applicationDidBecomeActive:(UIApplication *)application
{
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

- (void)applicationWillTerminate:(UIApplication *)application
{
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

答案 1 :(得分:0)

在onPause()方法中编写此代码以了解您的应用程序是否已转到后台。

public void onPause()
{
 ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
Boolean flag=false;     

List<RunningTaskInfo> tasks = am.getRunningTasks(1);
        if (!tasks.isEmpty()) {
          ComponentName topActivity = tasks.get(0).topActivity;
          if (!topActivity.getPackageName().equals(context.getPackageName())) {
            flag=true;
          }
        }
if(flag)
{
//App went to background...
}

}

使用onResume()中的上述标记来了解您的应用已恢复。

答案 2 :(得分:0)

对于Android,你可以在onResume函数中修改你的代码,当backgroud应用程序出现时调用它。但要记住安卓的生命周期

的onCreate - &GT;的onResume

所以onResume总是被调用,即使app第一次运行或来自后台。 但onCreate只在第一次创建Activity时调用。

你可以在onPause方法上设置一些变量,当app进入后台时调用该方法

当你得到变量“true”并且调用onResume时,你可以执行任务。

享受。

答案 3 :(得分:0)

尝试在活动的两个方法中使用布尔变量,onKeyDown和onPause。

boolean backFromBackground = false;

onCreate(){
 //whatever you want
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {     

    if(keyCode == KeyEvent.KEYCODE_HOME)
    {

       backFromBackground = true;
    }
}
onPause(){
 if(backFromBackground){
  //do what ever you want
 }
}