任何知道如何在应用程序中启动允许的应用程序时禁用“最近任务应用程序按钮”的人,就像在SureLock Kiosk Lockdown中一样?如果是这样,你能为此提供代码吗?
答案 0 :(得分:2)
来自source
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
Log.d("Focus debug", "Focus changed !");
if(!hasFocus) {
Log.d("Focus debug", "Lost focus !");
Intent closeDialog = new Intent(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
sendBroadcast(closeDialog);
}
}
答案 1 :(得分:2)
要禁用“最近”按钮,请将此代码添加到主要活动:
@Override
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
if (!hasFocus) {
windowCloseHandler.post(windowCloserRunnable);
}
}
private void toggleRecents() {
Intent closeRecents = new Intent("com.android.systemui.recent.action.TOGGLE_RECENTS");
closeRecents.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
ComponentName recents = new ComponentName("com.android.systemui", "com.android.systemui.recent.RecentsActivity");
closeRecents.setComponent(recents);
this.startActivity(closeRecents);
}
private Handler windowCloseHandler = new Handler();
private Runnable windowCloserRunnable = new Runnable() {@Override public void run() {
ActivityManager am = (ActivityManager) getApplicationContext().getSystemService(Context.ACTIVITY_SERVICE);
ComponentName cn = am.getRunningTasks(1).get(0).topActivity;
if (cn != null && cn.getClassName().equals("com.android.systemui.recent.RecentsActivity")) {
toggleRecents();
}
}
};
答案 2 :(得分:2)
是的,这可能是简单的方法。
将此权限添加到manifest.xml文件
<uses-permission android:name="android.permission.REORDER_TASKS" />
将此代码置于您要阻止/禁用最近使用的应用程序按钮的“期望活动”中:
@Override
protected void onPause() {
super.onPause();
ActivityManager activityManager = (ActivityManager) getApplicationContext()
.getSystemService(Context.ACTIVITY_SERVICE);
activityManager.moveTaskToFront(getTaskId(), 0);
}
对我有用。
答案 3 :(得分:0)
@Override
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
Log.d("Focus debug", "Focus changed !");
if(!hasFocus) {
Log.d("Focus debug", "Lost focus !");
Intent closeDialog = new Intent(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
sendBroadcast(closeDialog);
}
}
答案 4 :(得分:0)
这里的其他答案是一种骇人听闻的方法,我尝试了这些方法但不喜欢。后来我发现 Android提供了一个执行类似操作的库。 请在这里查看我的答案:https://stackoverflow.com/a/59266919/1938507
注意:我直接发布答案而不是评论,因为我在这个主题上浪费了很多时间,人们通常会忽略评论!
答案 5 :(得分:-1)
要在启动其他应用程序时禁用最近的应用程序按钮,您可以创建服务。
公共类StatusBarService扩展了服务{
private static final String TAG = StatusBarService.class.getSimpleName();
private volatile boolean isRunning;
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate() {
super.onCreate();
isRunning = true;
// Lock a recentApps button
RecentAppLockerThread thread = new RecentAppLockerThread();
new Thread(thread).start();
}
/**
* Lock a recent apps button
*
*
*/
private class RecentAppLockerThread implements Runnable {
@Override
public void run() {
while (isRunning) {
Intent closeDialog = new Intent(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
sendBroadcast(closeDialog);
}
}
}
@Override
public boolean stopService(Intent name) {
isRunning = false;
return super.stopService(name);
}
@Override
public void onDestroy() {
super.onDestroy();
isRunning = false;
}
}