0

Android アプリケーションで強制停止を処理する方法を知りたいです。アプリケーションには、次のようにアクティビティ内に 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) {

アプリケーションは例外なく実行されますが、ユーザーが (Samsung Galaxy S5 のホーム ボタンの左側にあるメニュー ボタンを使用して) 強制停止した後、アプリケーションを再度開くと、進行状況ダイアログ メッセージが表示されます。しばらくお待ちください」が asynctask の onPreExecute メソッドにあり、無限にハングします。

デバッグすると、アプリケーションが 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();
    }
}

アプリケーションがこの無限ループから抜け出すにはどうすればよいでしょうか?

ありがとうございました。

よろしくお願いします。

4

1 に答える 1

0

最後に「socketTask.cancel(true);」を追加することで解決しました。次のように「onDestroy()」メソッドに:

@Override
protected void onDestroy() {
    super.onDestroy();
    if (socketTask != null)
        socketTask.cancel(true);
}
于 2015-11-26T14:10:00.023 に答える