英文:
Android Handler keeps running for a while even after the activity has been destroyed
问题
我读到handler.postDelayed
方法有助于暂停任务一段时间。因此,我将其写入光线传感器中,以便传感器每隔2秒钟检测一次光线,以节省电池寿命:
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
SensorManager sm = (SensorManager) getSystemService(SENSOR_SERVICE);
Sensor light = sm.getDefaultSensor(Sensor.TYPE_LIGHT);
SensorEventListener listener;
if (light != null) {
final Handler handler = new Handler();
final Runnable task = new Runnable() {
@Override
public void run() {
sm.registerListener(listener, light, SensorManager.SENSOR_DELAY_NORMAL);
}
};
listener = new SensorEventListener() {
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
Toast.makeText(MainActivity.this, "accuracy changed!", Toast.LENGTH_SHORT).show();
}
@Override
public void onSensorChanged(SensorEvent event) {
sm.unregisterListener(listener, light);
// 在此处处理接收到的传感器光线值,即event.values[0]
handler.postDelayed(task, 2000);
}
};
}
}
@Override
protected void onPause() {
super.onPause();
sm.unregisterListener(listener, light);
}
@Override
protected void onResume() {
super.onResume();
sm.registerListener(listener, light, SensorManager.SENSOR_DELAY_NORMAL);
}
@Override
protected void onStop() {
super.onStop();
sm.unregisterListener(listener, light);
}
@Override
protected void onDestroy() {
super.onDestroy();
sm.unregisterListener(listener, light);
}
}
起初,它似乎正常工作。但我发现每次退出应用程序时,它会持续大约20到30秒钟检测光线,就好像活动仍然打开一样。我做错了什么?我甚至添加了onStop
和onDestroy
方法,只是为了确保它正确关闭。
英文:
I read that the handler.postDelayed
method helps pause a task for any amount of time. So I wrote it into the light sensor so that the sensor only detects light every 2 seconds, in order to save battery life:
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
SensorManager sm = (SensorManager) getSystemService(SENSOR_SERVICE);
Sensor light = sm.getDefaultSensor(Sensor.TYPE_LIGHT);
SensorEventListener listener;
if (light != null) {
final Handler handler = new Handler();
final Runnable task = new Runnable() {
@Override
public void run() {
sm.registerListener(listener, light, SensorManager.SENSOR_DELAY_NORMAL);
}
};
listener = new SensorEventListener() {
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
Toast.makeText(MainActivity.this, "accuracy changed!", Toast.LENGTH_SHORT).show();
}
@Override
public void onSensorChanged(SensorEvent event) {
sm.unregisterListener(listener, light);
// Do the stuff here with the received sensor light value, which is event.values[0]
handler.postDelayed(task, 2000);
}
};
}
@Override
protected void onPause() {
super.onPause();
sm.unregisterListener(listener, light);
}
@Override
protected void onResume() {
super.onResume();
sm.registerListener(listener, light, SensorManager.SENSOR_DELAY_NORMAL);
}
@Override
protected void onStop() {
super.onStop();
sm.unregisterListener(listener, light);
}
@Override
protected void onDestroy() {
super.onDestroy();
sm.unregisterListener(listener, light);
}
}
At first it seemed to work fine. But I found that every time I exit the app it kept detecting the light for like 20 to 30 seconds as if the activity was still open. What am I doing wrong? I even added both the onStop and onDestroy methods just to be sure it was closing correctly.
答案1
得分: 2
在您的活动的 onDestroy() 中,清除您的处理程序,如下所示:
handler.removeCallbacksAndMessages(null);
这样就会立即停止!
英文:
Inside the onDestroy() of your activity clear your handler like
handler.removeCallbacksAndMessages(null);
And it will stop right away!
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论