Android Dev _ about how to block home keys

Source: Internet
Author: User
<span id="Label3"></p><p><p>today, in the face of a problem to block the home key, The study was finally resolved in the morning, the method is recorded in the Following:</p></p><p><p>  </p></p><p><p>The following methods can be overridden in the Android2.3 version to mask the home key:</p></p><pre><pre><span style="color: #0000ff;"></span> public <span style="color: #0000ff;">void</span> <span style="color: #000000;">Onattachedtowindow () { </span>this<span style="color: #0000ff;"></span><span style="color: #000000;">. GetWindow (). SetType (WindowManager.LayoutParams.TYPE_ keyguard); </span> <span style="color: #0000ff;">Super</span> <span style="color: #000000;">. Onattachedtowindow ();}</span></pre></pre><p><p>Using the Onattachedtowindow () method in Android4.0 or above will cause the error to be masked by the following methods:</p></p><pre><pre><span style="color: #0000ff;"></span> public <span style="color: #0000ff;">Static</span> <span style="color: #0000ff;">Final</span> <span style="color: #0000ff;">int</span> <span style="color: #008000;">//</span> <span style="color: #008000;">need to define your own logo</span> <span style="color: #0000ff;"></span> public <span style="color: #0000ff;">void</span> <span style="color: #000000;">onCreate (Bundle Savedinstancestate) { </span><span style="color: #0000ff;">Super</span><span style="color: #000000;">. OnCreate (savedinstancestate); </span> <span style="color: #0000ff;"></span>this. getWindow (). setflags (flag_homekey_dispatched, flag_homekey_dispatched); <span style="color: #008000;">//</span> <span style="color: #008000;">Key Code</span> <span style="color: #000000;"> Setcontentview (r.layout.main);}</span></pre></pre><p><p>Rewrite the onkeydown method again:</p></p><pre><pre><span style="color: #000000;">@Override</span> <span style="color: #0000ff;"></span> public <span style="color: #0000ff;">Boolean</span> <span style="color: #0000ff;">int</span> <span style="color: #000000;">keycode, keyevent Event) { </span><span style="color: #0000ff;">if</span> (keycode = =<span style="color: #000000;"> Event. Keycode_home) { </span><span style="color: #0000ff;">return</span><span style="color: #0000ff;">true</span><span style="color: #000000;">; </span> <span style="color: #0000ff;">return</span> <span style="color: #0000ff;">Super</span> <span style="color: #000000;">. OnKeyDown (keycode, event);}</span></pre></pre><p><p>In Androidmainfest.xml you need to add permissions:<br><uses-permission android:name = "android.permission.DISABLE_KEYGUARD"/><!--the permissions required to mask the home key--</p></p><p><p>In the configuration file, add an attribute android:android:launchmode= "singleinstance" to the activity you used notification</p></p><p><p>Android Dev _ about how to block home keys</p></p></span>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.