java - 破坏 Activity 导致服务丢失数据

标签 java android

每当我的应用程序最小化时,我都会启动一个服务,该服务向我的 HTTP 服务器发送拉取请求以检查通知,当应用程序恢复时,服务将被终止(以及计划的可运行项)。一切正常,直到我决定终止该应用程序(将其从正在运行的应用程序列表中滑出屏幕)。然后由于某种原因,服务的属性被重置(即使是静态的)和 onStartCommand再次调用它的第一个参数 Intentnull这对我来说很奇怪。

以下是部分代码

public class DnActivity extends Activity {

    protected String cookieString = "";
    protected String userAgent = "";
    protected WebView webview;

    @Override
    protected void onStop() {
        super.onStop();
        try {
            Intent mServiceIntent = new Intent(this, PullService.class);
            mServiceIntent.putExtra("cookieString", cookieString);
            mServiceIntent.putExtra("userAgent", userAgent);
            startService(mServiceIntent);
        } catch (Exception e) {
            Log.d("DNev", e.getMessage());
        }
    }

    @Override
    protected void onStart() {
        super.onStart();

        Intent mServiceIntent = new Intent(this, PullService.class);
        stopService(mServiceIntent);
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        ...
        webview.setWebViewClient(new WebViewClient() {
            @Override
            public void onPageFinished(WebView view, String url) {
                try {
                    cookieString = getCookieFromAppCookieManager(url);
                } catch (Throwable e) {
                    Log.e("DNev", e.getMessage());
                }
            }
        });
    }
}

还有服务

public class PullService extends Service {

    protected static String cookieString;
    protected static String userAgent = "Mobile APP for Android";
    protected Service PullService = this;
    protected ScheduledFuture interval;

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        if (intent != null) {
            if (intent.hasExtra("cookieString")) {
                cookieString = intent.getStringExtra("cookieString");
            }
            if (intent.hasExtra("userAgent")) {
                userAgent = intent.getStringExtra("userAgent");
            }
        }

        return super.onStartCommand(intent, flags, startId);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();

        interval.cancel(true);
    }

    @Override
    public void onCreate() {
        super.onCreate();

        Log.d("DNev", String.valueOf(cookieString));
        Log.d("DNev", String.valueOf(userAgent));

        ScheduledExecutorService scheduledExecutorService = Executors.newScheduledThreadPool(1);

        interval = scheduledExecutorService.scheduleAtFixedRate(new Runnable() {
            public void run() {
                Log.d("DNev", "1");
                Log.d("DNev", String.valueOf(cookieString));
                Log.d("DNev", String.valueOf(userAgent));

                ...

正如我所说,一切正常,直到我销毁该 Activity ,然后 interval继续运行但cookieStringuserAgent成为它们的默认值。

我需要能够在 Activity 被销毁时保留这些值,我该怎么做?

我在 android 和 java 开发方面都没有经验,如果我的代码让任何人哭泣,我想道歉。

这是该服务的 list 条目,它位于 <application

<service  android:name=".PullService" android:exported="false"/>

最佳答案

All works well until I decided to kill the application (slide it off the screen from the running apps list).

当您终止该应用程序(我假设从“设置”->“应用程序”中强制停止)时,整个应用程序将被终止,包括其服务。存储在变量中的所有内容都将随着该过程而消失。如果您希望它生存,您需要将其存储在持久存储中(即数据库或共享首选项中)。

此外,我会在收到数据后将其保存在 onStartCommand() 中,因为如果 onDestroy() 不会被调用(这对于突然终止来说不太可能)处理)那么您的数据将会丢失。

I start a service that is sending pull requests to my HTTP server to check for notifications

不要。使用 GCM 将通知实际推送到应用程序。不要拉扯。

关于java - 破坏 Activity 导致服务丢失数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31841045/

相关文章:

android检查用户是否关闭位置

android - 将一个移动应用程序与其他应用程序(Android 和 IOS)集成

java - Spring Data Neo4j "No property get found for type"异常

android - 如何从用户离开的地方继续?

android - 在更改不同 fragment 时将 webview 状态保存在 fragment 中

android - 如何解决 "WARNING: Application does not specify an API level requirement"?

java - 使用tomcat部署应用程序时出错

java - 如何检查 Apache Camel 中是否存在文件?

java - 从 mybatis select 获取不同 java 对象的多个值

java - 当输入是 'quit' 时无法让 java 识别,这应该结束循环/程序