Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <blockquote> <p>I was wondering if I can override the action of the back and home button is some cases.</p> </blockquote> <p>Yes you can do override <code>Home</code> button.</p> <p>I have developed an application which disable hard button, you can have a look. I have taken a <strong>toggle button</strong> which locks all hard button to work except <strong>Power</strong> button</p> <pre><code>public class DisableHardButton extends Activity { /** Called when the activity is first created. */ TextView mTextView; ToggleButton mToggleButton; boolean isLock=false; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); mTextView=(TextView) findViewById(R.id.tvInfo); mToggleButton=(ToggleButton) findViewById(R.id.btnLock); mToggleButton.setOnCheckedChangeListener(new OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { // TODO Auto-generated method stub isLock=isChecked; onAttachedToWindow(); } }); } @Override public boolean dispatchKeyEvent(KeyEvent event) { if ( (event.getKeyCode() == KeyEvent.KEYCODE_HOME) &amp;&amp; isLock) { mTextView.setText("KEYCODE_HOME"); return true; } else return super.dispatchKeyEvent(event); } @Override public boolean onKeyDown(int keyCode, KeyEvent event) { // TODO Auto-generated method stub if( (keyCode==KeyEvent.KEYCODE_BACK) &amp;&amp; isLock) { mTextView.setText("KEYCODE_BACK"); return true; } else return super.onKeyDown(keyCode, event); } @Override public void onAttachedToWindow() { System.out.println("Onactivity attached :"+isLock); if(isLock) { this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD); super.onAttachedToWindow(); } else { this.getWindow().setType(WindowManager.LayoutParams.TYPE_APPLICATION); super.onAttachedToWindow(); } } } </code></pre> <p>main.xml</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" &gt; &lt;TextView android:id="@+id/tvInfo" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" /&gt; &lt;ToggleButton android:id="@+id/btnLock" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textOff="UnLocked" android:textOn="Locked" /&gt; &lt;/LinearLayout&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload