当应用程序进入后台时如何处理

时间:2018-08-02 10:53:14

标签: android server

我正在研究应用程序,即从服务器获取任务,然后用户通过移动应用程序进行评估(就像测验应用程序一样)。

在评估任务期间,如果用户按下主页按钮,则应用程序将进入后台。当用户从最近的后台应用程序返回到应用程序时,应用程序将从启动屏幕启动。

我很困惑应该添加什么价格的代码,以便当用户返回到Application时,必须将先前的状态显示给用户?

2 个答案:

答案 0 :(得分:0)

在您的主要活动中覆盖onStop(),onPause(),onResume()方法。

答案 1 :(得分:0)

进一步复制该课程,我会告诉你怎么做

public class Foreground implements Application.ActivityLifecycleCallbacks {

public static final long CHECK_DELAY = 50;
public static final String TAG = Foreground.class.getName();

public interface Listener {

    public void onBecameForeground();

    public void onBecameBackground();

}

private static Foreground instance;

private boolean foreground = false, paused = true;
private Handler handler = new Handler();
private List<Listener> listeners = new CopyOnWriteArrayList<Listener>();
private Runnable check;

/**
 * Its not strictly necessary to use this method - _usually_ invoking
 * get with a Context gives us a path to retrieve the Application and
 * initialise, but sometimes (e.g. in test harness) the ApplicationContext
 * is != the Application, and the docs make no guarantees.
 *
 * @param application
 * @return an initialised Foreground instance
 */
public static Foreground init(Application application){
    if (instance == null) {
        instance = new Foreground();
        application.registerActivityLifecycleCallbacks(instance);
    }
    return instance;
}

public static Foreground get(Application application){
    if (instance == null) {
        init(application);
    }
    return instance;
}

public static Foreground get(Context ctx){
    if (instance == null) {
        Context appCtx = ctx.getApplicationContext();
        if (appCtx instanceof Application) {
            init((Application)appCtx);
        }
        throw new IllegalStateException(
                "Foreground is not initialised and " +
                        "cannot obtain the Application object");
    }
    return instance;
}

public static Foreground get(){
    if (instance == null) {
        throw new IllegalStateException(
                "Foreground is not initialised - invoke " +
                        "at least once with parameterised init/get");
    }
    return instance;
}

public boolean isForeground(){
    return foreground;
}

public boolean isBackground(){
    return !foreground;
}

public void addListener(Listener listener){
    listeners.add(listener);
}

public void removeListener(Listener listener){
    listeners.remove(listener);
}

@Override
public void onActivityResumed(Activity activity) {
    paused = false;
    boolean wasBackground = !foreground;
    foreground = true;

    if (check != null)
        handler.removeCallbacks(check);

    if (wasBackground){
        Log.i(TAG, "went foreground");
        for (Listener l : listeners) {
            try {
                l.onBecameForeground();
            } catch (Exception exc) {
                Log.e(TAG, "Listener threw exception!", exc);
            }
        }
    } else {
        Log.i(TAG, "still foreground");
    }
}

@Override
public void onActivityPaused(Activity activity) {
    paused = true;
    if (check != null)
        handler.removeCallbacks(check);

    handler.postDelayed(check = new Runnable(){
        @Override
        public void run() {
            if (foreground && paused) {
                foreground = false;
                Log.i(TAG, "went background");
                for (Listener l : listeners) {
                    try {
                        l.onBecameBackground();
                    } catch (Exception exc) {
                        Log.e(TAG, "Listener threw exception!", exc);
                    }
                }
            } else {
                Log.i(TAG, "still foreground");
            }
        }
    }, CHECK_DELAY);
}

@Override
public void onActivityCreated(Activity activity, Bundle savedInstanceState) {}

@Override
public void onActivityStarted(Activity activity) {}

@Override
public void onActivityStopped(Activity activity) {}

@Override
public void onActivitySaveInstanceState(Activity activity, Bundle outState) {}

@Override
public void onActivityDestroyed(Activity activity) {}

}

将其添加到年度应用类的onCreate

    Foreground foreground = Foreground.init(this);

    final Foreground.Listener myListener = new Foreground.Listener()
    {
        public void onBecameForeground()
        {
            Log.d("TAG", "FOREGROUND");
        }

        public void onBecameBackground()
        {
            //registerActivityLifecycleCallbacks(new MyLifecycleHandler());

                Intent i = new Intent("android.intent.action.MAIN").putExtra("some_msg", "I will be sent!");
                sendBroadcast(i);


        }
    };

    foreground.addListener(myListener);

将此代码添加到您的基本活动的onCreate中好吗?

    IntentFilter intentFilter = new IntentFilter(
            "android.intent.action.MAIN");

    mReceiver = new BroadcastReceiver()
    {

        @Override
        public void onReceive(Context context, Intent intent)
        {
            //extract our message from intent
            String msg_for_me = intent.getStringExtra("some_msg");
            //log our message value
            Log.i("InchooTutorial", msg_for_me);



                finish();


        }
    };
    //registering our receiver
    this.registerReceiver(mReceiver, intentFilter);

不是,这是您在oncreate之外覆盖onDestroy方法的副本

  @Override
   protected void onDestroy()
    {
     super.onDestroy();
      unregisterReceiver(mReceiver);
   }