Plamen Nikolov
Plamen Nikolov

Reputation: 4187

How to handle that the application is minimized by HOME button

An issue has appeared a few days ago. I have an application that listen for GPS location. The listener is a background service that works all the time, this service saves data in application level and each activity reads this data. So, when i press back button i am able to catch this event and i can stop the service, but when i press HOME button the service is still working although the application is in background mode and this consumes battery, because the GPS always works. How can i handle this event? I do want to stop all services when the user presses HOME button and start them again when user gets back. 10x

Upvotes: 2

Views: 12687

Answers (6)

Hamid
Hamid

Reputation: 355

   @Override
    protected void onUserLeaveHint() 
   { 
        // When user presses home page
        Log.v(TAG, "Home Button Pressed");
        super.onUserLeaveHint();
    }

Upvotes: 2

UMAR-MOBITSOLUTIONS
UMAR-MOBITSOLUTIONS

Reputation: 77984

You can not handle home button events in your Android application. Google has made it for internal use only.

LINK 1 LINK 2

Upvotes: 4

shantanu
shantanu

Reputation: 9

create a custom activity called customActivity extends Activity now override method(in customActivity) to catch home button event and stop your service(create & start service in application class). Now extends customActivity instead of Activity for any activity_class.

Upvotes: 0

gnclmorais
gnclmorais

Reputation: 4971

You could create a OnKeyListener, and check if the pressed key was Home. I never tried it, though.

Upvotes: 0

Al Sutton
Al Sutton

Reputation: 3924

It sounds like you're catching the back button either via an onKey... method or in onStop. You should place your code in the onPause() method to ensure it's used whenever the app gets backgrounded.

Upvotes: 4

SHW
SHW

Reputation: 145

Long press the HOME button, it will enlist the running process, select the one you want and then exit it gracefully.

OR From Home -> settings -> application -> manage application you can kill the unwanted process.

Upvotes: -5

Related Questions