如何在任何活动中禁用虚拟主页按钮? [英] How to disable virtual home button in any activity?
问题描述
我需要在我的应用程序的任何活动中禁用3个虚拟按钮.我以某种方式禁用了后退按钮和多任务按钮,但无法使主页按钮失效.
I need to disable 3 virtual buttons in any activity in my app. I disabled back button and multitask button somehow but I cannot dsable home button.
我在stackoverflow上尝试了onAttachedToWindow()
样式的答案,但它们对我不起作用.
I tried onAttachedToWindow()
style answers on stackoverflow but they didn't work for me.
我不想为整个应用禁用主页按钮,我只是想为单个活动窗口禁用主页按钮.感谢您的帮助!
I don't want to disable home button for entire app, I just want to disable it for a single activity window. Thanks for your helps!
推荐答案
注意:如果您要部署它,强烈建议您不要在您的应用程序中执行此操作.这只是说明我们如何做到.
自Android 4起,没有有效的方法可以禁用主页按钮,它几乎不需要黑客.我认为您的需要是应用程序中的KIOSK模式.通常,此想法是检测新应用程序何时处于前台并立即重新启动Activity
.过程如下.
Since Android 4 there is no effective method to Disable the home button.It needs little hack. I think your need is KIOSK mode in app. In general the idea is to detect when a new application is in foreground and restart your Activity
immediately. The processes are Below..
首先,创建一个名为KioskService
的类,该类扩展Service
并添加以下代码段:
At first create a class called KioskService
that extends Service
and add the following snippet :
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;
}
}
在AppContext
类中添加以下方法,以通过创建应用程序上下文来启动service
.
Add the following method in your AppContext
class to start the service
via application context creation.
@Override
public void onCreate() {
super.onCreate();
instance = this;
registerKioskModeScreenOffReceiver();
startKioskService(); // add this
}
private void startKioskService() { // ... and this method
startService(new Intent(this, KioskService.class));
}
您的AppContext
类看起来像这样
public class AppContext extends Application {
private AppContext instance;
private PowerManager.WakeLock wakeLock;
private OnScreenOffReceiver onScreenOffReceiver;
@Override
public void onCreate() {
super.onCreate();
instance = this;
registerKioskModeScreenOffReceiver();
}
private void registerKioskModeScreenOffReceiver() {
// register screen off receiver
final IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_OFF);
onScreenOffReceiver = new OnScreenOffReceiver();
registerReceiver(onScreenOffReceiver, filter);
}
public PowerManager.WakeLock getWakeLock() {
if(wakeLock == null) {
// lazy loading: first call, create wakeLock via PowerManager.
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
wakeLock = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP, "wakeup");
}
return wakeLock;
}
}
将服务声明和用于检索前台进程的权限添加到清单中:
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"/>
//Added permission Edit 1
<uses-permission android:name="android.permission.WAKE_LOCK" />
我已经在一个站点上看到了所有这些内容,但是忘记了链接,我所拥有的只是内容和代码,因此我将所有内容发布为答案.一旦获得链接,我将与您分享该链接.
I've seen this all at one site but forgot the link, whatever I have is only contents and codes, so that I am posting all as an answer. As soon as I get the link, I will share that with you.
这篇关于如何在任何活动中禁用虚拟主页按钮?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!