有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java破坏活动会导致服务丢失数据

每当我的应用程序最小化时,我启动一个服务,该服务将向我的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));

                ...

正如我所说的,在我销毁活动之前,一切正常,然后interval继续运行,但是cookieStringuserAgent成为它们的默认值

当活动被破坏时,我需要能够持久化这些值,我如何才能做到这一点

我对安卓和java开发都没有经验,如果我的代码让任何人大哭,我想道歉

这是服务的清单条目,它位于<application

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

共 (2) 个答案

  1. # 1 楼答案

    DnActivity.onDestroy()方法中,将信息保存在某个地方,您可以让活动控件的“关闭”控制mServiceIntent并对其进行更改(例如也将其关闭)

    例如:

    DnActivity.onDestroy(){
       super.onDestroy();
       stopService(mServiceIntent);
       Intent mServiceIntent = new Intent(this, PullService.class);
            mServiceIntent.putExtra("some_value", the_value);
            mServiceIntent.putExtra("some_other_value", the_other_value);
       startService(mServiceIntent);
    }
    
  2. # 2 楼答案

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

    当您终止应用程序时(我假设Force Stop,即设置->;应用程序),整个应用程序将终止,包括其服务。存储在变量中的所有内容都将随过程而消失。如果您想让它存活下来,您需要将其存储在持久性存储中(即,存储在数据库或共享首选项中)

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

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

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