简体   繁体   English

如何在android中禁用HOME按钮点击

[英]How to disable HOME button click in android

I am new in android.我是安卓新手。 I am creating a Lock screen application.我正在创建一个锁屏应用程序。 In my application, I want to disable all the outside keys like Home key, Back key.. I already disabled the Back key using:在我的应用程序中,我想禁用所有外部键,如 Home 键、Back 键。我已经使用以下方法禁用了 Back 键:

@Override
public void onBackPressed() {
    return;
    // Do nothing!
}

But i referred a lot of sites and questions in Stack Overflow to disable the Home key in my app.但是我在 Stack Overflow 中引用了很多网站和问题来禁用我的应用程序中的 Home 键。 But nothing worked.但没有任何效果。 My App working on API 16 .. Please help me.我的应用程序在 API 16 上工作 .. 请帮助我。 Any help would be appreciated.任何帮助,将不胜感激。

Thanks a lot in advence非常感谢提前

I recommend reading: How-To Create a Working Kiosk Mode in Android我推荐阅读: How-To Create a Working Kiosk Mode in Android

Disable the home button and detect when new applications are opened禁用主页按钮并检测何时打开新应用程序

Since Android 4 there is no effective method to deactivate the home button.由于 Android 4 没有有效的方法来停用主页按钮。 That is the reason why we need another little hack.这就是我们需要另一个小技巧的原因。 In general the idea is to detect when a new application is in foreground and restart your activity immediately.一般来说,这个想法是检测新应用程序何时处于前台并立即重新启动您的活动。

At first create a class called KioskService that extends Service and add the following snippet:首先创建一个名为 KioskService 的类来扩展 Service 并添加以下代码段:

 public class KioskService extends Service { private static final long INTERVAL = TimeUnit.SECONDS.toMillis(2); // periodic interval to check in seconds -> 2 seconds private static final String TAG = KioskService.class.getSimpleName(); private static final String PREF_KIOSK_MODE = "pref_kiosk_mode"; private Thread t = null; private Context ctx = null; private boolean running = false; @Override public void onDestroy() { Log.i(TAG, "Stopping service 'KioskService'"); running =false; super.onDestroy(); } @Override public int onStartCommand(Intent intent, int flags, int startId) { Log.i(TAG, "Starting service 'KioskService'"); running = true; ctx = this; // start a thread that periodically checks if your app is in the foreground t = new Thread(new Runnable() { @Override public void run() { do { handleKioskMode(); try { Thread.sleep(INTERVAL); } catch (InterruptedException e) { Log.i(TAG, "Thread interrupted: 'KioskService'"); } }while(running); stopSelf(); } }); t.start(); return Service.START_NOT_STICKY; } private void handleKioskMode() { // is Kiosk Mode active? if(isKioskModeActive()) { // is App in background? if(isInBackground()) { restoreApp(); // restore! } } } private boolean isInBackground() { ActivityManager am = (ActivityManager) ctx.getSystemService(Context.ACTIVITY_SERVICE); List<ActivityManager.RunningTaskInfo> taskInfo = am.getRunningTasks(1); ComponentName componentInfo = taskInfo.get(0).topActivity; return (!ctx.getApplicationContext().getPackageName().equals(componentInfo.getPackageName())); } private void restoreApp() { // Restart activity Intent i = new Intent(ctx, MyActivity.class); i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); ctx.startActivity(i); } public boolean isKioskModeActive(final Context context) { SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(context); return sp.getBoolean(PREF_KIOSK_MODE, false); } @Override public IBinder onBind(Intent intent) { return null; } }

Add the following method in your AppContext class to start the service via application context creation.在 AppContext 类中添加以下方法以通过应用程序上下文创建启动服务。

 @Override public void onCreate() { super.onCreate(); instance = this; registerKioskModeScreenOffReceiver(); startKioskService(); // add this } private void startKioskService() { // ... and this method startService(new Intent(this, KioskService.class)); }

Last, add the service declaration and the permission for retrieving the foreground process to the manifest:最后,将服务声明和检索前台进程的权限添加到清单中:

 <service android:name=".KioskService" android:exported="false"/> <uses-permission android:name="android.permission.GET_TASKS"/>

Basically, the thread checks every two seconds if your application is running in foreground.基本上,线程每两秒钟检查一次您的应用程序是否在前台运行。 If not, the thread will immediately recreate your activity.如果没有,该线程将立即重新创建您的活动。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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