我是Android新手并开发具有锁功能的Android应用程序。我想在活动中禁用设备的主页按钮,我在过去4天用谷歌搜索它,我没有得到正确的解决方案。我尝试了所有解决方案,我在下面的链接,但我无法禁用主页按钮。请建议我如何以编程方式禁用主页和所有设备的键。任何帮助将不胜感激。
这是我访问的链接,并尝试了描述他们的解决方案
How to disable android hardware buttons programmatically?
How to lock android buttons/phone from code (screen lock)?
http://sunil-android.blogspot.in/2013/07/mostly-used-android-code.html
http://www.technonutty.com/2013/11/disable-home-power-back-button-android.html
以下是我最后编辑的代码:
MainActivity.java
package com.example.lockdemo;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity {
private LockLayer lockLayer;
private View lockView;
Button button1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
init();
button1 = (Button)lockView.findViewById(R.id.button1);
button1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
lockLayer.unlock();
finish();
}
});
}
private void init() {
lockView = View.inflate(this, R.layout.activity_main, null);
lockLayer = LockLayer.getInstance(this);
lockLayer.setLockView(lockView);
lockLayer.lock();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
LockLayer.java
public class LockLayer {
private Activity mActivty;
private WindowManager mWindowManager;
private View mLockView;
private LayoutParams mLockViewLayoutParams;
private static LockLayer mLockLayer;
private boolean isLocked;
public static LockLayer getInstance(Activity act){
if(mLockLayer == null){
mLockLayer = new LockLayer(act);
}
return mLockLayer;
}
private LockLayer(Activity act) {
mActivty = act;
init();
}
private void init(){
isLocked = false;
mWindowManager = mActivty.getWindowManager();
mLockViewLayoutParams = new LayoutParams();
mLockViewLayoutParams.width = LayoutParams.MATCH_PARENT;
mLockViewLayoutParams.height = LayoutParams.MATCH_PARENT;
mLockViewLayoutParams.type = LayoutParams.TYPE_SYSTEM_ERROR;
}
public void lock() {
if(mLockView!=null){
mWindowManager.addView(mLockView, mLockViewLayoutParams);
}
isLocked = true;
}
public void unlock() {
if(mWindowManager!=null && isLocked){
mWindowManager.removeView(mLockView);
}
isLocked = false;
}
public void setLockView(View v){
mLockView = v;
}
}
activity_main.xml中
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.lockdemo"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="21" />
<uses-permission android:name="android.permission.DISABLE_KEYGUARD" />
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
谢谢。
答案 0 :(得分:1)
将此行添加到项目manifest.xml文件中
<uses-permission android:name="android.permission.REORDER_TASKS" />
以及您的活动中的该标签
<category android:name="android.intent.category.LAUNCHER" />
<category android:name="android.intent.category.HOME" />
<category android:name="android.intent.category.DEFAULT" />
在您的活动中像这样修改onPause方法
@Override
protected void onPause() {
super.onPause();
ActivityManager activityManager = (ActivityManager) getApplicationContext()
.getSystemService(Context.ACTIVITY_SERVICE);
activityManager.moveTaskToFront(getTaskId(), 0);
}
在活动中重写onKeyDown方法
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
// Do nothing or catch the keys you want to block
}
答案 1 :(得分:0)
首先,我想说主页按钮比其他按键更难禁用是有原因的 - 不应该这样做。您可以想象有多少恶意应用程序会这样做。即使你确实实现了它,它也需要一些强大的权限,这将使人们重新考虑使用你的应用程序。我看到这个成功完成的唯一方法是使用自定义启动器
如果您有合法需求,则以下方法应该有效 - 但通知栏仍然可用,这与您的问题不同,但可能仍然相关。
@Override
public void onAttachedToWindow() {
getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD);
super.onAttachedToWindow();
}
对于一个也包含其他一些有用答案的问题,here可以找到相同的答案