Android app: What is running in background?
I have almost finished developing an Android App. I use the GP开发者_运维技巧S location and sms receiver class. I can see that if I press "HOME"(the house), it's still running. I would like to close the GPS listener and the SMS_RECEIVER when home button is pressed.
I am using eclipse and windows.
You can listen the onStop Events and shutdown the listeners.
Activity.onStop documentation
Note: maybe onPause or onDestroy might be better options. Read activity life-cycle and choose the best point to do this.
Using the home button to exit will leave your app running (home, from what I understand, is more of a "minimize" button). Use back to close completely. To override the home button functionality to actually exit, use this:
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
// TODO Auto-generated method stub
if (event.getAction() == KeyEvent.ACTION_DOWN) {
switch (keyCode) {
case KeyEvent.KEYCODE_HOME:
finish();
return true;
}
}
return super.onKeyDown(keyCode, event);
}
If you use BroadCast receivers(you probably use separate classes and declare receivers in AndroidManifest.xml) then consider making them class members of your Activity. Inside Activity class override onResume and onPause and register and unregister receivers there.
精彩评论