【问题标题】:How to Handle Force Stop如何处理强制停止
【发布时间】:2015-11-25 17:21:58
【问题描述】:

我想知道如何在我的 android 应用程序中处理强制停止。 该应用程序在 Activity 中包含一个 asynctask 类,如下所示:

private class SocketTask extends AsyncTask<String, Void, SocketAnswer> {

    ProgressDialog pd;

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        lockOrientation();
        try {
            if (pd == null) {
                try {
                    pd = new ProgressDialog(MenuDisplayActivity.this);
                } catch (Exception e) {
                }
            }
            pd.setMessage(Global.Labels.get(165).toString());
            pd.setCancelable(false);
            pd.show();
        } catch (Exception e) {
        }           
    }

    public SocketTask(MenuDisplayActivity activity) {
        try {
            pd = new ProgressDialog(activity);
        } catch (Exception e) {
        }
    }

    @Override
    protected SocketAnswer doInBackground(String... params) {

应用程序运行时没有任何异常,但是,在用户强制停止后(使用菜单按钮 位于三星 Galaxy S5 的主页按钮左侧),当应用程序重新打开时,它会显示进度对话框 位于 onPreExecute 方法中的消息“请稍候” asynctask 并无限挂起。

在调试的时候,我注意到应用程序挂在 Looper 类的循环方法中:

public static void loop() {
    final Looper me = myLooper();
    if (me == null) {
        throw new RuntimeException("No Looper; Looper.prepare() wasn't called on this thread.");
    }
    final MessageQueue queue = me.mQueue;

    // Make sure the identity of this thread is that of the local process,
    // and keep track of what that identity token actually is.
    Binder.clearCallingIdentity();
    final long ident = Binder.clearCallingIdentity();

    for (;;) { //The application gets stuck infinitely in this loop
        Message msg = queue.next(); // might block
        if (msg == null) {
            // No message indicates that the message queue is quitting.
            return;
        }

        // This must be in a local variable, in case a UI event sets the logger
        Printer logging = me.mLogging;
        if (logging != null) {
            logging.println(">>>>> Dispatching to " + msg.target + " " +
                    msg.callback + ": " + msg.what);
        }

        msg.target.dispatchMessage(msg);

        if (logging != null) {
            logging.println("<<<<< Finished to " + msg.target + " " + msg.callback);
        }

        // Make sure that during the course of dispatching the
        // identity of the thread wasn't corrupted.
        final long newIdent = Binder.clearCallingIdentity();
        if (ident != newIdent) {
            Log.wtf(TAG, "Thread identity changed from 0x"
                    + Long.toHexString(ident) + " to 0x"
                    + Long.toHexString(newIdent) + " while dispatching to "
                    + msg.target.getClass().getName() + " "
                    + msg.callback + " what=" + msg.what);
        }

        msg.recycle();
    }
}

我们如何确保应用程序摆脱这个无限循环?

谢谢。

最好的问候。

【问题讨论】:

    标签: android android-asynctask infinite-loop progressdialog android-looper


    【解决方案1】:

    最后通过添加“socketTask.cancel(true);”解决了到“onDestroy()”方法如下:

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (socketTask != null)
            socketTask.cancel(true);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-04-22
      • 1970-01-01
      • 1970-01-01
      • 2014-08-23
      • 2017-09-28
      • 1970-01-01
      相关资源
      最近更新 更多