I have a very cool answer of this question
I have tried it from the past 1 year and just found out the solution.
Please look at the answer below.
Create a class with the name of HomeWatcher.
Exactly use the below code with the name of HomeWatcher.
public class HomeWatcher {
static final String TAG = "HomeWatcher";
private Context mContext;
private IntentFilter mFilter;
private OnHomePressedListener mListener;
private InnerRecevier mRecevier;
public interface OnHomePressedListener {
public void onHomePressed();
public void onHomeLongPressed();
//public void onLockLongPressed();
}
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() {
try{
if (mRecevier != null) {
mContext.registerReceiver(mRecevier, mFilter);
}
}catch(Exception e){}
}
public void stopWatch() {
try{
if (mRecevier != null) {
mContext.unregisterReceiver(mRecevier);
}
}catch(Exception e){}
}
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";
//final String SYSTEM_DIALOG_REASON_Lock = "lock";
@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();
}
/* else if (reason
.equals(SYSTEM_DIALOG_REASON_Lock)) {
mListener.onLockLongPressed();
}*/
}
}
}
}
}
}
You need to declare the Homewatcher on your desired class where you wanted to detect the home button and Recent apps button.
public class MainActivity extends Activity {
private HomeWatcher mHomeWatcher;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
try {
mHomeWatcher = new HomeWatcher(this);
mHomeWatcher.setOnHomePressedListener(new OnHomePressedListener() {
@Override
public void onHomePressed() {
Log.e(TAG, "onHomePressed");
}
@Override
public void onHomeLongPressed() {
Log.e(TAG, "recent apps");
}
});
mHomeWatcher.startWatch();
} catch (Exception e) {
}
}
In the end of this you just need to add onPause and onResume Overide method like this.
@Override
protected void onResume() {
super.onResume();
try {
mHomeWatcher.startWatch();
} catch (Exception e) {
}
}
@Override
protected void onPause() {
super.onPause();
try {
mHomeWatcher.stopWatch();
} catch (Exception e) {
}
}
A very simple and beautiful answer. Waiting for the votes and accepted answer :p