0

I am developing an application for music. I have an Activity where I have a list with tracks. I can select a track and then I move to another activity in order to play the selected track. I also have a Service class and when I press the home button, I want it to start the service (play music in the background) and move to the home screen.

Also, I have the method OnBackPressed() in order to move to the previous Activity.

How, I can intercept the home button press? Thanks, for yours answers.

koopaking3
  • 3,375
  • 2
  • 25
  • 36
Tim
  • 75
  • 1
  • 10
  • Maybe this topic will help u http://stackoverflow.com/questions/5376501/how-to-control-home-button-in-android but u can overload onPause() to achieve what you want – gregory561 Jul 20 '11 at 13:06

2 Answers2

3

Rather than catching the home button press, you might want to flesh out the android lifecycle a little more and use onPause() instead. If you override the activity's onPause() method to start the service, whenever the user leaves the activity temporarily, i.e. goes "home", that should get you what you need without having to catch all the button presses.

If you want the service to start whenever the user leaves the activity and, say, goes to another application, the lifecycle is your friend. You might want to consider what happens when the user gets an email notification and leaves your application that way. Do you want the service to start then as well? The user didn't hit the home button so your service won't start, but they did leave your application. Do you want the service to start in that scenario?

It sounds like you want the service to start whenever the user leaves that activity, so please look at overriding onPause() if that is the case.

More on the android life cycle here:

http://developer.android.com/reference/android/app/Activity.html

All you would need is to add this method:

@Override
protected void onPause(){
    <start your service here>
}

Edit: Give this a try as a workaround for not being able to intercept the home button, set a back_flag

public class Your_Activity extends Activity {
    private boolean back_flag = false;

    .
    .
    .

    public void onBackPressed(){
        back_flag = true;
        <whatever else you have in onBackPressed()>
    }

    @Override
    protected void onPause(){
        if(!back_flag){
            <start your service>
        }
        back_flag = false;
        super.onPause();
    }
}

Again, if it's not registering the home, this is the only work around I can think of at the moment.

Otra
  • 8,108
  • 3
  • 34
  • 49
  • Thanks, I will try this, but i think that when i press back button(in order to moved to previous activity),the onPause() method called again... – Tim Jul 20 '11 at 13:33
  • Clarification: Do you want the service to start when the user hits the back button? Or are you saying that the service is called again when you hit the back button even though it is running already? – Otra Jul 20 '11 at 13:35
  • I want service starts when i press HOME button, when i press BACK button i want to moved to previous activity. – Tim Jul 20 '11 at 13:41
  • Ok,so, when i press the home button and the music plays, i want the moved to home screen and service starts in order to continue played the music in backround.... – Tim Jul 20 '11 at 13:57
  • Edited my response with a workaround since you can't intercept KEYCODE_HOME – Otra Jul 20 '11 at 14:25
1

Android is designed to NOT allow your application to interrupt the home button like you can the back button. That's a "working as designed" feature. The back button can be captured with onKeyDown, but home does not.

The only thing you can do is listen for the HOME intent. In your manifest add a block like this:

<intent-filter>
  <action android:name="android.intent.action.MAIN" />
  <category android:name="android.intent.category.HOME" />
  <category android:name="android.intent.category.DEFAULT" />
</intent-filter>

Now here's the thing... when the user hits the home button after installing your application, Android will pop up a "what do you want to do" and they can set the default to either their default home screen or your application. After they pick a default, they never see that selector again.

As you can see, it's not useful for much of anything except a home screen replacement app.

Make sure you test any other workaround such as the OnPause suggestion completely. Will your app perform as expected if the screen times out for instance?

Tony Maro
  • 1,854
  • 17
  • 14