无法使Android.os.Handler继续运行

时间:2016-02-26 01:44:51

标签: java android multithreading tcp-ip

因此,我需要继续运行以便将AYT发送到设备并定期报告连接(基本上是上升还是下降)。我现在有一个设置,它可以工作,但是大约10或15分钟后,它就会停止运行。它使用HandlerThread,Handler和Runnable来启动服务,然后自行停止。

MainActivity.java片段:

private HandlerThread hThread = new HandlerThread("MaintainConnection");
private Handler h;
private Runnable hTask = new Runnable() {
    @Override
    public void run() {
        startService(new Intent(getApplicationContext(), MaintainConnectionService.class));
        h.postDelayed(this, 2000);
    }
};

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    hThread.start();
    h = new Handler(hThread.getLooper());
    /* OTHER CODE GOES HERE */
}

@Override
protected void onPause() {
    super.onPause();
    Log.v(TAG, "I am in onPause");
    h.removeCallbacks(hTask);
}

@Override
protected void onResume() {
    super.onResume();
    Log.v(TAG, "I am in onResume");
    h.post(hTask);
}

@Override
protected void onStop() {
    super.onStop();
    Log.v(TAG, "I am in onStop");
    h.removeCallbacks(hTask);
}

MaintainConnectionService.java

public class MaintainConnectionService extends IntentService {

    public MaintainConnectionService() {
        super(MaintainConnectionService.class.getName());
    }

    private static final String TAG = "MaintainConnectionSVC";

    public static Handler UIHandler;
    static {
        UIHandler = new Handler(Looper.getMainLooper());
    }

    public static void runOnUI(Runnable runnable) {
        UIHandler.post(runnable);
    }

    @Override
    protected void onHandleIntent(Intent intent) {
        try {
            if (Comms.telnet.sendAYT(Comms.AYT_TIMEOUT)) {
                Log.d(TAG, "Sent AYT to => " + Comms.telnet.getRemoteAddress() + ":" + Comms.telnet.getRemotePort());
                runOnUI(new Runnable() {
                    public void run() {
                        ColourControlFragment.connStat.setBackgroundColor(Color.GREEN);
                        ColourControlFragment.connStatTxt.setText("Connection Established");
                    }
                });
            } else {
                runOnUI(new Runnable() {
                    public void run() {
                        ColourControlFragment.connStat.setBackgroundColor(Color.YELLOW);
                        ColourControlFragment.connStatTxt.setText("Attempting Reconnect");
                    }
                });
            }
        } catch (Exception e) {
            Log.e(TAG, "Failed to send AYT to: " + Comms.TELNET_ADDRESS);
            runOnUI(new Runnable() {
                public void run() {
                    ColourControlFragment.connStat.setBackgroundColor(Color.RED);
                    ColourControlFragment.connStatTxt.setText("Not Connected");
                }
            });
            startService(new Intent(getApplicationContext(), EstablishConnectionService.class));
        }
        stopSelf();
    }
}

这一切似乎按预期工作,但正如我所说,在大约10或15分钟后,没有任何警告,它会进入"黄色"状态(尝试重新连接)并保持在那里。如果我关闭应用程序并重新启动它,它将恢复工作。

有什么方法可以防止线程被杀死?有关更好的监控连接方式的任何建议吗?

我已经尝试过普通ThreadTimer,但无济于事。

1 个答案:

答案 0 :(得分:0)

您的活动中的处理程序应该是静态的,否则您可能会泄露您的活动。

另外,如果你删除onResume和onPause中的回调(两者都没有这样做有意义),那么你不再接收任何消息是有意义的。