2013-09-16 120 views
0

我有一個啓動器有兩個配置文件:父母和孩子的孩子無法訪問商店和設置。我正在使用平板電腦8「。首頁按鈕和訪問最近的任務按鈕不在狀態欄。我的問題是,如果孩子訪問最近的任務,他將能夠訪問設置。那麼如何禁用訪問。最近的任務我試着在disable access to recent tasks給出的代碼,但它並沒有爲我工作,我嘗試:如何禁用最近的應用程序對話框按鈕?

@Override 
public void onWindowFocusChanged(boolean hasFocus) { 
    super.onWindowFocusChanged(hasFocus); 

    if (!hasFocus) { 
     windowCloseHandler.postDelayed(windowCloserRunnable, 10); 
    } 
} 

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(); 
     } 
    } 
}; 

它遮住了訪問,但它不是一個有效的解決方案的孩子可能會進入狀態。酒吧所以要設置

+0

可能重複http://stackoverflow.com/a/17774643/2520370 – Steve

+0

i sa我使用這種方法的身份證,但它不是我正在尋找的,因爲我們可以使用此解決方案訪問最近的任務 – user2641131

+1

如果您想禁用最近的對話框,您將需要利用漏洞,因爲它是故意不可能的。 – zapl

回答

3

我發現,最好的辦法是做這樣的:

public class BaseActivity extends Activity { 
    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); 
     } 
    } 
}// all credit goes here: http://www.juliencavandoli.com/how-to-disable-recent-apps-dialog-on-long-press-home-button/ 

這不是我自己的代碼,但是這只是隱藏了最近顯示的應用程序列表。

1

我發現用這種方法進行密切RecentsActivity

private static final String RECENTS_ACTIVITY = "com.android.systemui.recent.RecentsActivity"; 
private static final String SYSTEM_UI_PACKAGE_NAME = "com.android.systemui"; 

private void closeRecents() { 
    sendBroadcast(new Intent("com.android.systemui.recent.action.CLOSE")); 
    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(SYSTEM_UI_PACKAGE_NAME, RECENTS_ACTIVITY); 
    closeRecents.setComponent(recents); 
    startActivity(closeRecents); 
} 

那麼,何時以及如何使用此方法的最佳方式?

重寫活動中的onPause(),並首先檢查ActivityManager中的最近活動。這個方法是不是不準確的,但我沒有找到一個更好的辦法:

private ComponentName getTopActivity(){ 
    ActivityManager activityManager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE); 
    List<ActivityManager.RunningTaskInfo> taskInfo = activityManager.getRunningTasks(1); 
    ActivityManager.RunningTaskInfo runningTaskInfo = taskInfo.get(0); 
    ComponentName topActivity = runningTaskInfo.topActivity; 
    return topActivity; 
} 
@Override 
protected void onPause() { 
    super.onPause(); 
    if (getTopActivity().getClassName().equals(RECENTS_ACTIVITY)){ 
     closeRecents(); 
    }else { 
     validateAgainWithDelay(300); 
    } 
} 
private void validateAgainWithDelay(long delay){ 
    new Handler().postDelayed(new Runnable() { 
     @Override 
     public void run() { 
      if (getTopActivity().getClassName().equals(RECENTS_ACTIVITY)) { 
       closeRecents(); 
      } 
     } 
    },delay); 
} 

我檢查在Android 4.4.2和它的作品,希望這將幫助你。

你也需要把權限在AndroidManifest.xml

<uses-permission android:name="android.permission.GET_TASKS"/> 

如果你也需要關閉狀態欄你可以試試這個代碼:

private final Handler uiHandler = new Handler(new Handler.Callback() { 
    @Override 
    public boolean handleMessage(Message msg) { 
     if (msg.what==REMOVE_STATUS_BAR){ 
      Intent closeDialog = new Intent(Intent.ACTION_CLOSE_SYSTEM_DIALOGS); 
      sendBroadcast(closeDialog); 
      collapseStatusBar(); 
      return true; 
     } 
     return false; 
    } 
});  

public void onWindowFocusChanged(boolean hasFocus) { 
    super.onWindowFocusChanged(hasFocus); 
    if (!hasFocus){ 
     uiHandler.sendEmptyMessage(REMOVE_STATUS_BAR); 
     uiHandler.sendEmptyMessageDelayed(REMOVE_STATUS_BAR, 500); 
     uiHandler.sendEmptyMessageDelayed(REMOVE_STATUS_BAR, 1000); 
     uiHandler.sendEmptyMessageDelayed(REMOVE_STATUS_BAR, 2000); 
    } 
}  



private void collapseStatusBar() { 
    try { 
     Object service = getSystemService("statusbar"); 
     Class<?> statusbarManager = Class.forName("android.app.StatusBarManager"); 
     Method collapse = statusbarManager.getMethod(hasCollapseMethod(statusbarManager) ? "collapse" : "collapsePanels"); 
     collapse.setAccessible(true); 
     collapse.invoke(service); 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
} 

private boolean hasCollapseMethod(Class<?> statusbarManager) { 
    for (Method method : statusbarManager.getMethods()) { 
     if (method.getName().equals("collapse")) { 
      return true; 
     } 
    } 
    return false; 

} 

這種方法也需要AndroidManifest 2個權限。 xml

<uses-permission android:name="android.permission.EXPAND_STATUS_BAR"/> 
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/> 
相關問題