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 Ac开发者_如何学Pythontivity
.
How, I can intercept the home button press? Thanks, for yours answers.
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.
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?
精彩评论