简体   繁体   中英

how to detect Navigation Buttons Clicks in a service ?Android

i have to build an application in which a functionality requires to detect Navigation Buttons Clicks like: Back,Home & Recent App Button and then performing some functionality via Service.

create Receiver in Service Class:

 class InnerReceiver 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) {

        if (intent.getAction().equals(Intent.ACTION_CLOSE_SYSTEM_DIALOGS)) {
            String reason = intent.getStringExtra(SYSTEM_DIALOG_REASON_KEY);
            if (reason != null) {
                if (reason.equals(SYSTEM_DIALOG_REASON_HOME_KEY)) {
                    // Home Button click

                } else if (reason.equals(SYSTEM_DIALOG_REASON_RECENT_APPS)) {
                    // RecentApp or Overview Button click
                 
                }
            }
        }
    }
}

then Register Broadcast Receiver in Service onStartCommand() method:

 mReceiver = new InnerReceiver();
    IntentFilter mFilter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
    registerReceiver(mReceiver, mFilter);

and Unregister Receiver in onDstroy() Method:

 unregisterReceiver(mReceiver);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM