APP global monitoring system Home button
10148 단어 android boost
APP global monitoring system Home button
By customizing the Application, there is a registerActivityLifecycleCallbacks(ActivityLifecycleCallbacks a) method in the Application. To call this method, you need to override the life cycle method of the ActivityLifecycleCallbacks interface (this life cycle method includes the life cycle of all Activity), so in OnActivityResume() and OnActivityPause () method to monitor the Home key (registration and anti-registration system broadcast).
HomeWatch.java
Encapsulates the system broadcast of Home and implements the callback of the interface
public class HomeWatch {
private Context mContext;
private IntentFilter mFilter;
private OnHomePressedListener mListener;
private HomeBroadcastReceiver mRecevier;
public interface OnHomePressedListener {
void onHomePressed();
void onHomeLongPressed();
}
public HomeWatch(Context context) {
mContext = context;
mRecevier = new HomeBroadcastReceiver();
mFilter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
}
/**
*
*
* @param listener
*/
public void setOnHomePressedListener(OnHomePressedListener listener) {
mListener = listener;
}
/**
* ,
*/
public void register() {
if (mRecevier != null) {
mContext.registerReceiver(mRecevier, mFilter);
}
}
/**
* ,
*/
public void unregister() {
if (mRecevier != null) {
mContext.unregisterReceiver(mRecevier);
}
}
class HomeBroadcastReceiver extends BroadcastReceiver {
final String SYSTEM_DIALOG_REASON_KEY = "reason";
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) {
if (mListener != null) {
if (reason.equals(SYSTEM_DIALOG_REASON_HOME_KEY)) {
// home
mListener.onHomePressed();
} else if (reason
.equals(SYSTEM_DIALOG_REASON_RECENT_APPS)) {
// home
mListener.onHomeLongPressed();
}
}
}
}
}
}
}
APP.java
In order to avoid overriding too many methods in the APP, implement the Application.ActivityLifecycleCallbacks interface through a class
public class APP extends Application {
private HomeWatch mHomeWatcher;
@Override
public void onCreate() {
super.onCreate();
regisiterActivityLifecycle();
}
private void regisiterActivityLifecycle() {
registerActivityLifecycleCallbacks(new ActivityLifecycleCallBack() {
@Override
public void onActivityResumed(final Activity activity) {
super.onActivityResumed(activity);
mHomeWatcher = new HomeWatch(activity);
mHomeWatcher.setOnHomePressedListener(new HomeWatch.OnHomePressedListener() {
@Override
public void onHomePressed() {
//
}
@Override
public void onHomeLongPressed() {
//
}
});
mHomeWatcher.register();
}
@Override
public void onActivityPaused(Activity activity) {
super.onActivityPaused(activity);
mHomeWatcher.setOnHomePressedListener(null);
mHomeWatcher.unregister();
}
});
}
}
ActivityLifecycleCallBack.java
Customized a class that implements the interface of Application.ActivityLifecycleCallbacks
public class ActivityLifecycleCallBack implements Application.ActivityLifecycleCallbacks {
@Override
public void onActivityCreated(Activity activity, Bundle savedInstanceState) {
}
@Override
public void onActivityStarted(Activity activity) {
}
@Override
public void onActivityResumed(Activity activity) {
}
@Override
public void onActivityPaused(Activity activity) {
}
@Override
public void onActivityStopped(Activity activity) {
}
@Override
public void onActivitySaveInstanceState(Activity activity, Bundle outState) {
}
@Override
public void onActivityDestroyed(Activity activity) {
}
}
in AndroidManifest.xml ".APP"
...>