如何防止AndroidOS为了内存而关闭后台应用?

How to prevent Android OS from closing a background app for memory?

我创建了一个使用 ~10MB RAM 的应用程序。似乎当我启动其他应用程序并且我的应用程序在后台时,它有时会关闭。我怀疑这是因为 Android OS 出于 RAM 管理目的关闭后台应用程序(Phone 有 1024MB 的总 RAM)。

有什么方法可以让我的应用程序始终 运行 在后台以编程方式或其他方式运行?

在后台使用服务 运行。

Run Background Service 阅读更多内容。

您不能为了 OS 的意愿而让您的应用程序在后台运行。您能做的最好的事情就是保存和恢复 activities/fragments/views 等

的状态

Recreating an Activity

static final String STATE_SCORE = "playerScore";
static final String STATE_LEVEL = "playerLevel";
...

//saving
@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
    // Save the user's current game state
    savedInstanceState.putInt(STATE_SCORE, mCurrentScore);
    savedInstanceState.putInt(STATE_LEVEL, mCurrentLevel);

    // Always call the superclass so it can save the view hierarchy state
    super.onSaveInstanceState(savedInstanceState);
}

//restoring
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState); // Always call the superclass first

    // Check whether we're recreating a previously destroyed instance
    if (savedInstanceState != null) {
        // Restore value of members from saved state
        mCurrentScore = savedInstanceState.getInt(STATE_SCORE);
        mCurrentLevel = savedInstanceState.getInt(STATE_LEVEL);
    } else {
        // Probably initialize members with default values for a new instance
    }
    ...

}