2016-02-26 82 views
0

因此,我需要继续运行一项服务,以便将AYT发送到设备并定期报告连接(基本上是否启动或关闭) 。我现在有一个安装程序,它可以工作,但是大约10或15分钟后,它就停止运行。它使用HandlerThread,Handler和Runnable来启动服务,该服务完成其工作,然后自行停止。无法让Android.os.Handler继续运行

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分钟,没有警告后,它会进入“黄色”状态(尝试重新连接)并停留在那里。如果我关闭应用程序并重新启动它,它将继续工作。

任何方式来防止线程被杀害?任何建议,以更好的方式来监测连接?

我试过简单的Thread s和Timer s,无济于事。

回答

0

您的活动中的处理程序应该是静态的,否则您有泄漏活动的风险。另外,如果你删除onResume和onPause中的回调(这两个都没有任何意义btw),它确实有意义,你不再收到任何消息。

+0

我的问题在于线程在大约12分钟左右后自动被杀死,这是我想阻止的。我想不出更好的方法来保持某种连接状态信息,而不仅仅是在发送失败之后更新状态(然后成功的重新连接 - 仅在发送包时测试连接) –