daily pastebin goal
77%
SHARE
TWEET

Untitled

a guest Sep 24th, 2018 67 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. public boolean onKeyDown(int keyCode, KeyEvent event) {
  2.     if (keyCode == KeyEvent.KEYCODE_MENU) {
  3.         // Do Stuff
  4.     } else {
  5.         return super.onKeyDown(keyCode, event);
  6.     }
  7. }
  8.    
  9. public boolean onKeyUp(int keyCode, KeyEvent event) {
  10.     if (keyCode == KeyEvent.KEYCODE_MENU) {
  11.         // ........
  12.         return true;
  13.     }
  14.     return super.onKeyUp(keyCode, event);
  15. }
  16.    
  17. public boolean onKeyUp(int keyCode, KeyEvent event) {
  18.     if (keyCode == KeyEvent.KEYCODE_MENU) {
  19.         // ...
  20.         return true;
  21.     } else {
  22.         return super.onKeyUp(keyCode, event);
  23.     }
  24. }
  25.    
  26. public boolean onKeyDown(int keyCode, KeyEvent event) {
  27.     //Checking for the "menu" key
  28.     if (keyCode == KeyEvent.KEYCODE_MENU) {
  29.         if (mDrawerLayout.isDrawerOpen(mDrawerList)) {
  30.             mDrawerLayout.closeDrawers();
  31.         } else {
  32.             mDrawerLayout.openDrawer(Gravity.RIGHT);
  33.         }
  34.         return true;
  35.     } else {
  36.         return super.onKeyDown(keyCode, event);
  37.     }
  38. }
  39.    
  40. public class HomeWatcher {
  41.     static final String TAG = "HomeWatcher";
  42.     private Context mContext;
  43.     private IntentFilter mFilter;
  44.     private OnHomePressedListener mListener;
  45.     private InnerRecevier mRecevier;
  46.  
  47.     public interface OnHomePressedListener {
  48.         public void onHomePressed();
  49.  
  50.         public void onHomeLongPressed();
  51.  
  52.         //public void onLockLongPressed();
  53.     }
  54.  
  55.     public HomeWatcher(Context context) {
  56.         mContext = context;
  57.         mFilter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
  58.     }
  59.  
  60.  
  61.     public void setOnHomePressedListener(OnHomePressedListener listener) {
  62.         mListener = listener;
  63.         mRecevier = new InnerRecevier();
  64.     }
  65.  
  66.     public void startWatch() {
  67.         try{
  68.         if (mRecevier != null) {
  69.             mContext.registerReceiver(mRecevier, mFilter);
  70.         }
  71.         }catch(Exception e){}
  72.     }
  73.  
  74.  
  75.     public void stopWatch() {
  76.         try{
  77.         if (mRecevier != null) {
  78.             mContext.unregisterReceiver(mRecevier);
  79.         }
  80.         }catch(Exception e){}
  81.     }
  82.  
  83.  
  84.     class InnerRecevier extends BroadcastReceiver {
  85.         final String SYSTEM_DIALOG_REASON_KEY = "reason";
  86.         final String SYSTEM_DIALOG_REASON_GLOBAL_ACTIONS = "globalactions";
  87.         final String SYSTEM_DIALOG_REASON_RECENT_APPS = "recentapps";
  88.         final String SYSTEM_DIALOG_REASON_HOME_KEY = "homekey";
  89.         //final String SYSTEM_DIALOG_REASON_Lock = "lock";
  90.  
  91.         @Override
  92.         public void onReceive(Context context, Intent intent) {
  93.             String action = intent.getAction();
  94.             if (action.equals(Intent.ACTION_CLOSE_SYSTEM_DIALOGS)) {
  95.                 String reason = intent.getStringExtra(SYSTEM_DIALOG_REASON_KEY);
  96.  
  97.                 if (reason != null) {
  98.                     Log.e(TAG, "action:" + action + ",reason:" + reason);
  99.                     if (mListener != null) {
  100.                         if (reason.equals(SYSTEM_DIALOG_REASON_HOME_KEY)) {
  101.                             mListener.onHomePressed();
  102.                         } else if (reason
  103.                                 .equals(SYSTEM_DIALOG_REASON_RECENT_APPS)) {
  104.                             mListener.onHomeLongPressed();
  105.  
  106.                         }
  107.                     /*  else if (reason
  108.                                 .equals(SYSTEM_DIALOG_REASON_Lock)) {
  109.                             mListener.onLockLongPressed();
  110.  
  111.                         }*/
  112.  
  113.                     }
  114.                 }
  115.             }
  116.         }
  117.     }
  118. }
  119.    
  120. public class MainActivity extends  Activity {
  121.  
  122.     private HomeWatcher mHomeWatcher;
  123.  
  124.     @Override
  125.     public void onCreate(Bundle savedInstanceState) {
  126.         super.onCreate(savedInstanceState);
  127.  
  128.          setContentView(R.layout.activity_main);
  129.     try {
  130.  
  131.             mHomeWatcher = new HomeWatcher(this);
  132.             mHomeWatcher.setOnHomePressedListener(new OnHomePressedListener() {
  133.                 @Override
  134.                 public void onHomePressed() {
  135.                     Log.e(TAG, "onHomePressed");
  136.  
  137.  
  138.                 }
  139.                @Override
  140.                 public void onHomeLongPressed() {
  141.                     Log.e(TAG, "recent apps");
  142.  
  143.                 }
  144.  
  145.  
  146.             });
  147.             mHomeWatcher.startWatch();
  148.         } catch (Exception e) {
  149.         }
  150.  
  151.     }
  152.    
  153. @Override
  154.     protected void onResume() {
  155.         super.onResume();
  156.  
  157.         try {
  158.             mHomeWatcher.startWatch();
  159.  
  160.         } catch (Exception e) {
  161.         }
  162.     }
  163.     @Override
  164.     protected void onPause() {
  165.         super.onPause();
  166.  
  167.         try {
  168.             mHomeWatcher.stopWatch();
  169.  
  170.         } catch (Exception e) {
  171.         }
  172. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top