Reputation: 5002
I have a SoundService
for play sound on load app.
I start service in splashActivity. I want to stop service when click Home
button.
When I add stopService in onPause
callback of MainActivity it stops the service when home button is clicked but also when starting another activity.
protected void onPause() {
stopService(new Intent(MainActivity.this, SoundService.class));
super.onPause();
}
Upvotes: 1
Views: 834
Reputation: 1332
As there is no built in way to listen to home button click listener, then it is better to use another application event to stop service. The closest one is to listen to when application goes into background. To achieve this ProcessLifecycleOwner
can be used. Just use onStop
event in the observer of process lifecycle to stop service.
More info can be found in this article.
Upvotes: 0
Reputation: 19
For Activity: Forfragment you can go with : ((Activity) getActivity()).finish();
From Activity just Activity.onBackPressed() method or Finish() method will work.
For Service: For Fragment: getActivity().stopService(new Intent(getActivity(), ServiceClassName.class));
ForActivity: .stopService(new Intent(getActivity(), ServiceClassName.class));
Upvotes: 0
Reputation: 434
Use this code for home button press event :
HomeWatcher mHomeWatcher = new HomeWatcher(this);
mHomeWatcher.setOnHomePressedListener(new OnHomePressedListener() {
@Override
public void onHomePressed() {
// do something here...
}
@Override
public void onHomeLongPressed() {
}
});
mHomeWatcher.startWatch();
HomeWatcher.class :
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.util.Log;
public class HomeWatcher {
static final String TAG = "hg";
private Context mContext;
private IntentFilter mFilter;
private OnHomePressedListener mListener;
private InnerRecevier mRecevier;
public HomeWatcher(Context context) {
mContext = context;
mFilter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
}
public void setOnHomePressedListener(OnHomePressedListener listener) {
mListener = listener;
mRecevier = new InnerRecevier();
}
public void startWatch() {
if (mRecevier != null) {
mContext.registerReceiver(mRecevier, mFilter);
}
}
public void stopWatch() {
if (mRecevier != null) {
mContext.unregisterReceiver(mRecevier);
}
}
class InnerRecevier extends BroadcastReceiver {
final String SYSTEM_DIALOG_REASON_KEY = "reason";
final String SYSTEM_DIALOG_REASON_GLOBAL_ACTIONS = "globalactions";
final String SYSTEM_DIALOG_REASON_RECENT_APPS = "recentapps";
final String SYSTEM_DIALOG_REASON_HOME_KEY = "homekey";
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (action.equals(Intent.ACTION_CLOSE_SYSTEM_DIALOGS)) {
String reason = intent.getStringExtra(SYSTEM_DIALOG_REASON_KEY);
if (reason != null) {
Log.e(TAG, "action:" + action + ",reason:" + reason);
if (mListener != null) {
if (reason.equals(SYSTEM_DIALOG_REASON_HOME_KEY)) {
mListener.onHomePressed();
} else if (reason.equals(SYSTEM_DIALOG_REASON_RECENT_APPS)) {
mListener.onHomeLongPressed();
}
}
}
}
}
}
}
OnHomePressedListener.class :
public interface OnHomePressedListener {
public void onHomePressed();
public void onHomeLongPressed();
}
Upvotes: 3