当APP在后台时,如何防止配置类被释放

时间:2017-07-17 12:27:19

标签: android

我制作了一个Android APP,它有一个配置结构,其中包含我在整个活动中需要的数据和服务。

我现在面临的问题是,如果我的APP在后台停留了一段时间,我的APP会崩溃,因为我的配置结构已被删除。

在我的配置结构中,我也有数据,我无法在运行时轻松重新创建。

所以在我的第一个Activity中,我创建了配置结构。

FreightWeightConfig config = new FreightWeightConfig(getApplicationContext()); // make sure our config is up and running

我的配置类的开头看起来像

public FreightWeightConfig(Context appContext) {
        instance = this;
        mApplicationContext = appContext;
        tcBlue.setCallingContext(appContext);
        tcBlueConfig = Config.getInstance(); // to make sure it is available straight away
        mFirebaseAuth = FirebaseAuth.getInstance();
        ....
}

我在配置结构中有第二个功能,它允许我获取配置类的实例,我需要访问配置和服务中的函数和接口。

public static synchronized FreightWeightConfig getInstance () {
        //if (FreightWeightConfig.instance == null) {
        //    FreightWeightConfig.instance = new FreightWeightConfig(getApplication().getApplicationContext());
        //}

        if (FreightWeightConfig.instance == null){
            FirebaseCrash.logcat(Log.ERROR, LOG_TAG, "Fatal Error : FreightWeightConfig.getInstance()==null. Try restarting the APP");
            FirebaseCrash.logcat(Log.ERROR, LOG_TAG, "Fatal Error : Killing ourself, as we have no chance to go on");
            //System.exit(0); // we are in a bad state
            // Toast.makeText(mApplicationContext, "Fatal Error : FreightWeightConfig.getInstance()==null. Try restarting the APP", Toast.LENGTH_SHORT).show();
  }

    return FreightWeightConfig.instance;
}

在每个Activity中,我创建了一个包含副本实例的变量。这很简单,因为我认为它告诉系统,我仍然需要这个类,不要杀它。这似乎不起作用。

我首先想到,每当我发现我的配置类死了,我就可以重新创建它。但这不是一项简单的任务,因为我需要APP上下文并需要在后台重新创建我的服务。此外,我存储选择,同时导航我的APP

任何人都有一个好主意,如何解决卸载/删除我的配置类?

基于这个建议,我扩展了这样的应用程序:

public class FreightWeightApp extends Application implements DialogInterface.OnCancelListener {

    private String LOG_TAG = "FreightWeightApp";
    private FreightWeightConfig config;

    public static int GOOGLE_PLAY_SERVIE_ABBORTED = 1001;

    public void onCreate() {
        super.onCreate();
        // We first check if the google services are present, if not, better abort!!
        int result = GoogleApiAvailability.getInstance().isGooglePlayServicesAvailable(this);
        switch (result) {
            case SUCCESS:
                Log.d(LOG_TAG, "Google Services available");
                break;
            case SERVICE_MISSING:
                Log.e(LOG_TAG, "Google Services missing, STOP");
                googleServiceNotUpToDateDialog(result);
                break;
            case SERVICE_VERSION_UPDATE_REQUIRED:
                Log.w(LOG_TAG, "Service update required");
                googleServiceNotUpToDateDialog(result);
                break;
            case SERVICE_DISABLED:
                Log.e(LOG_TAG, "Service disabled, STOP");
                googleServiceNotUpToDateDialog(result);
                break;
        }


        config = new FreightWeightConfig(getApplicationContext()); // make sure our config is up and running
    }

    private void googleServiceNotUpToDateDialog(int result) {
        // Try to ask the user to update or finish off
//        GoogleApiAvailability gaa = GoogleApiAvailability.getInstance();
//        Dialog dialog = gaa.getErrorDialog(this, result, GOOGLE_PLAY_SERVIE_ABBORTED, this);    //<==== Can not call this, as I have no Activity Context
//        dialog.show();
    }

    @Override
    public void onCancel(DialogInterface dialogInterface) {
        // Now I should abbort the APP, or we will crash.
    }
}

但现在我在验证Google服务时遇到了问题。

Dialog dialog = gaa.getErrorDialog(this, result, GOOGLE_PLAY_SERVIE_ABBORTED, this);

因为我没有活动背景。

1 个答案:

答案 0 :(得分:0)

创建自己的Application实现,然后在onCreate()方法中初始化配置对象(在这种情况下,getInstance()方法也可以初始化对象)。

public class MyApplication extends Application {

    @Override
    public void onCreate() {
        super.onCreate();
        FreightWeightConfig config = FreightWeightConfig.getInstance(getApplicationContext());
    }
}

在您应用的模块清单中声明您的实施:

<?xml version="1.0" encoding="utf-8"?>                                         
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="your.package">                                            

    <application
        android:name="your.package.MyApplication"                                                              
        android:icon="@mipmap/ic_launcher"                                     
        android:label="@string/app_name"                                       
        android:theme="@style/AppTheme">
        ...            

    </application>                                                             

</manifest>                                                                    

现在你可以像活动一样在活动中使用你的配置实例,调用getInstance();应用程序onCreate将执行加载工作。但是,在应用程序进程被杀死/缓存之前,没有办法卸载资源&#34;,你只需要了解Android组件&#39;生命周期工作。