【发布时间】:2018-07-06 02:36:54
【问题描述】:
我创建了一个新线程,用于在后台接收蓝牙数据。当我关闭应用程序时,线程正确关闭。但是当我更改活动时,线程再次打开。
private void receiveDataInBackground() {
Log.d(TAG, "receive Data in Background");
handlerInBackground = new Handler();
handlerInBackground.postDelayed(myRunnableInBackground = new Runnable() {
@Override
public void run() {
dataReceived=false;
Log.d(TAG, "in receiveBackground");
openBT();
//
if(dataReceived==true){closeBT();}
handlerInBackground.postDelayed(this, 60000);
}
}, 60000);
}
这是我的 onDestroy
public void onDestroy(){
super.onDestroy();
saveArrayList(btDataList, "btDataList");
btAdapter.disable();
Log.d(TAG, "finishing APP");
handlerInBackground.removeCallbacksAndMessages(myRunnableInBackground);
finish();
有人知道这个问题或可以帮助我吗?非常感谢!
编辑: 完整代码可以在 github 上找到: https://github.com/LongDong279/AppGeruest3
【问题讨论】:
标签: android multithreading android-activity