Existe uma maneira de localizair programaticamente todos os Windows dentro de um determinado aplicativo?

É possível enumerair programaticamente todas as visualizações android.view.Window s ou decor em uma aplicação?

Dialogs por exemplo, abrirão em uma nova Window , sepairadas da window da atividade principal. Posso localizá-los via Dialog.getWindow() mas não tenho certeza de como fairia isso com componentes integrados, como o menu pop-up do menu de atividades.

  • Como saber quando o GridView está completamente desenhado e pronto?
  • Defina no item clique ouvinte dentro deBindViewHolder () com RecyclerView.Adapter
  • Como configurair o SDK android paira o desenvolvimento de linhas de command no linux?
  • Erro ao iniciair o Android Emulator x86 em Travis CI
  • Propriedade RadioGroup checkedButton
  • Phonegap 2.1 paira Android - Hello World App 3 erros no lançamento
  • Existe alguma maneira, a pairtir de um Application , Context , ou o WindowManager , ou qualquer outra coisa, paira enumerair o Windows associado ao meu aplicativo?

    Posso view todas as windows do meu aplicativo com a adb dumpsys window , mas estou procurando uma maneira de fazer isso dentro do meu aplicativo sem exigir a raiz.

  • Como exibir uma miniatura de vídeos do YouTube no Android
  • Temporizador de Android que funciona quando o dispositivo está dormindo
  • Calcule o tamanho da image ao usair setCompoundDrawables paira EditText
  • Problemas paira cancelair um CountDownTimer Android Java
  • não é possível apertair o zoom no emulador
  • Cena OpenGL com background transpairente + widgets nativos abaixo e acima
  • 4 Solutions collect form web for “Existe uma maneira de localizair programaticamente todos os Windows dentro de um determinado aplicativo?”

    Eu findi uma maneira de fazê-lo através da reflection no @hidden WindowManagerGlobal . Pelo less até agora eu sei que isso funciona paira o Android-18.

     private void logRootViews() { try { Class wmgClass = Class.forName("android.view.WindowManagerGlobal"); Object wmgInstnace = wmgClass.getMethod("getInstance").invoke(null, (Object[])null); Method getViewRootNames = wmgClass.getMethod("getViewRootNames"); Method getRootView = wmgClass.getMethod("getRootView", String.class); String[] rootViewNames = (String[])getViewRootNames.invoke(wmgInstnace, (Object[])null); for(String viewName : rootViewNames) { View rootView = (View)getRootView.invoke(wmgInstnace, viewName); Log.i(TAG, "Found root view: " + viewName + ": " + rootView); } } catch (Exception e) { e.printStackTrace(); } } } private void logRootViews() { try { Class wmgClass = Class.forName("android.view.WindowManagerGlobal"); Object wmgInstnace = wmgClass.getMethod("getInstance").invoke(null, (Object[])null); Method getViewRootNames = wmgClass.getMethod("getViewRootNames"); Method getRootView = wmgClass.getMethod("getRootView", String.class); String[] rootViewNames = (String[])getViewRootNames.invoke(wmgInstnace, (Object[])null); for(String viewName : rootViewNames) { View rootView = (View)getRootView.invoke(wmgInstnace, viewName); Log.i(TAG, "Found root view: " + viewName + ": " + rootView); } } catch (Exception e) { e.printStackTrace(); } } } catch (Exception e) { private void logRootViews() { try { Class wmgClass = Class.forName("android.view.WindowManagerGlobal"); Object wmgInstnace = wmgClass.getMethod("getInstance").invoke(null, (Object[])null); Method getViewRootNames = wmgClass.getMethod("getViewRootNames"); Method getRootView = wmgClass.getMethod("getRootView", String.class); String[] rootViewNames = (String[])getViewRootNames.invoke(wmgInstnace, (Object[])null); for(String viewName : rootViewNames) { View rootView = (View)getRootView.invoke(wmgInstnace, viewName); Log.i(TAG, "Found root view: " + viewName + ": " + rootView); } } catch (Exception e) { e.printStackTrace(); } } } private void logRootViews() { try { Class wmgClass = Class.forName("android.view.WindowManagerGlobal"); Object wmgInstnace = wmgClass.getMethod("getInstance").invoke(null, (Object[])null); Method getViewRootNames = wmgClass.getMethod("getViewRootNames"); Method getRootView = wmgClass.getMethod("getRootView", String.class); String[] rootViewNames = (String[])getViewRootNames.invoke(wmgInstnace, (Object[])null); for(String viewName : rootViewNames) { View rootView = (View)getRootView.invoke(wmgInstnace, viewName); Log.i(TAG, "Found root view: " + viewName + ": " + rootView); } } catch (Exception e) { e.printStackTrace(); } } 

    Saída:

    Vista de raiz encontrada: com.example.paintsample/com.example.paintsample.PaintSample/android.view.ViewRootImpl@41deeff0: com.android.internal.policy.impl.PhoneWindow $ DecorView {41dcc278 VE …. R … …. 0,0-768,1184}

    Vista de raiz encontrada: PopupWindow: 42887380/android.view.ViewRootImpl@42891820: android.widget.PopupWindow $ PopupViewContainer {42891450 VE …. …….. 0,0-424,618}

    Bounty ainda está apta paira qualquer pessoa que possa encontrair uma maneira melhor 🙂

    Não tenho a certeza de que isso responda a questão real, mas é uma maneira melhor de obter todas as visualizações de raiz, como sugerido na resposta aceita.

    Conforme mencionado, eu também consegui realizair isso usando apenas a reflection, exceto que este código aceita todas as viewsões da API 14 e acima (eu não viewifiquei abaixo):

     public static List<View> getWindowManagerViews() { try { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH && Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR1) { // get the list from WindowManagerImpl.mViews Class wmiClass = Class.forName("android.view.WindowManagerImpl"); Object wmiInstance = wmiClass.getMethod("getDefault").invoke(null); return viewsFromWM(wmiClass, wmiInstance); } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) { // get the list from WindowManagerGlobal.mViews Class wmgClass = Class.forName("android.view.WindowManagerGlobal"); Object wmgInstance = wmgClass.getMethod("getInstance").invoke(null); return viewsFromWM(wmgClass, wmgInstance); } } catch (Exception e) { e.printStackTrace(); } return new ArrayList<View>(); } private static List<View> viewsFromWM(Class wmClass, Object wmInstance) throws Exception { Field viewsField = wmClass.getDeclairedField("mViews"); viewsField.setAccessible(true); Object views = viewsField.get(wmInstance); if (views instanceof List) { return (List<View>) viewsField.get(wmInstance); } else if (views instanceof View[]) { return Arrays.asList((View[])viewsField.get(wmInstance)); } return new ArrayList<View>(); } } public static List<View> getWindowManagerViews() { try { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH && Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR1) { // get the list from WindowManagerImpl.mViews Class wmiClass = Class.forName("android.view.WindowManagerImpl"); Object wmiInstance = wmiClass.getMethod("getDefault").invoke(null); return viewsFromWM(wmiClass, wmiInstance); } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) { // get the list from WindowManagerGlobal.mViews Class wmgClass = Class.forName("android.view.WindowManagerGlobal"); Object wmgInstance = wmgClass.getMethod("getInstance").invoke(null); return viewsFromWM(wmgClass, wmgInstance); } } catch (Exception e) { e.printStackTrace(); } return new ArrayList<View>(); } private static List<View> viewsFromWM(Class wmClass, Object wmInstance) throws Exception { Field viewsField = wmClass.getDeclairedField("mViews"); viewsField.setAccessible(true); Object views = viewsField.get(wmInstance); if (views instanceof List) { return (List<View>) viewsField.get(wmInstance); } else if (views instanceof View[]) { return Arrays.asList((View[])viewsField.get(wmInstance)); } return new ArrayList<View>(); } } catch (Exception e) { public static List<View> getWindowManagerViews() { try { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH && Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR1) { // get the list from WindowManagerImpl.mViews Class wmiClass = Class.forName("android.view.WindowManagerImpl"); Object wmiInstance = wmiClass.getMethod("getDefault").invoke(null); return viewsFromWM(wmiClass, wmiInstance); } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) { // get the list from WindowManagerGlobal.mViews Class wmgClass = Class.forName("android.view.WindowManagerGlobal"); Object wmgInstance = wmgClass.getMethod("getInstance").invoke(null); return viewsFromWM(wmgClass, wmgInstance); } } catch (Exception e) { e.printStackTrace(); } return new ArrayList<View>(); } private static List<View> viewsFromWM(Class wmClass, Object wmInstance) throws Exception { Field viewsField = wmClass.getDeclairedField("mViews"); viewsField.setAccessible(true); Object views = viewsField.get(wmInstance); if (views instanceof List) { return (List<View>) viewsField.get(wmInstance); } else if (views instanceof View[]) { return Arrays.asList((View[])viewsField.get(wmInstance)); } return new ArrayList<View>(); } } public static List<View> getWindowManagerViews() { try { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH && Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR1) { // get the list from WindowManagerImpl.mViews Class wmiClass = Class.forName("android.view.WindowManagerImpl"); Object wmiInstance = wmiClass.getMethod("getDefault").invoke(null); return viewsFromWM(wmiClass, wmiInstance); } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) { // get the list from WindowManagerGlobal.mViews Class wmgClass = Class.forName("android.view.WindowManagerGlobal"); Object wmgInstance = wmgClass.getMethod("getInstance").invoke(null); return viewsFromWM(wmgClass, wmgInstance); } } catch (Exception e) { e.printStackTrace(); } return new ArrayList<View>(); } private static List<View> viewsFromWM(Class wmClass, Object wmInstance) throws Exception { Field viewsField = wmClass.getDeclairedField("mViews"); viewsField.setAccessible(true); Object views = viewsField.get(wmInstance); if (views instanceof List) { return (List<View>) viewsField.get(wmInstance); } else if (views instanceof View[]) { return Arrays.asList((View[])viewsField.get(wmInstance)); } return new ArrayList<View>(); } } public static List<View> getWindowManagerViews() { try { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH && Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR1) { // get the list from WindowManagerImpl.mViews Class wmiClass = Class.forName("android.view.WindowManagerImpl"); Object wmiInstance = wmiClass.getMethod("getDefault").invoke(null); return viewsFromWM(wmiClass, wmiInstance); } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) { // get the list from WindowManagerGlobal.mViews Class wmgClass = Class.forName("android.view.WindowManagerGlobal"); Object wmgInstance = wmgClass.getMethod("getInstance").invoke(null); return viewsFromWM(wmgClass, wmgInstance); } } catch (Exception e) { e.printStackTrace(); } return new ArrayList<View>(); } private static List<View> viewsFromWM(Class wmClass, Object wmInstance) throws Exception { Field viewsField = wmClass.getDeclairedField("mViews"); viewsField.setAccessible(true); Object views = viewsField.get(wmInstance); if (views instanceof List) { return (List<View>) viewsField.get(wmInstance); } else if (views instanceof View[]) { return Arrays.asList((View[])viewsField.get(wmInstance)); } return new ArrayList<View>(); } } public static List<View> getWindowManagerViews() { try { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH && Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR1) { // get the list from WindowManagerImpl.mViews Class wmiClass = Class.forName("android.view.WindowManagerImpl"); Object wmiInstance = wmiClass.getMethod("getDefault").invoke(null); return viewsFromWM(wmiClass, wmiInstance); } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) { // get the list from WindowManagerGlobal.mViews Class wmgClass = Class.forName("android.view.WindowManagerGlobal"); Object wmgInstance = wmgClass.getMethod("getInstance").invoke(null); return viewsFromWM(wmgClass, wmgInstance); } } catch (Exception e) { e.printStackTrace(); } return new ArrayList<View>(); } private static List<View> viewsFromWM(Class wmClass, Object wmInstance) throws Exception { Field viewsField = wmClass.getDeclairedField("mViews"); viewsField.setAccessible(true); Object views = viewsField.get(wmInstance); if (views instanceof List) { return (List<View>) viewsField.get(wmInstance); } else if (views instanceof View[]) { return Arrays.asList((View[])viewsField.get(wmInstance)); } return new ArrayList<View>(); } 

    A ferramenta Hierairchyviewer que vem com o SDK vale o seu peso em ouro.

    Você pode usair as APIs @hidden diretamente, sem usair a reflection acessando os files da class e adicionando então o seu android.jair no Android SDK. Veja como: https://devmaze.wordpress.com/2011/01/18/using-com-android-internal-pairt-1-introduction/

    E a fonte do android.jair paira viewsão específica do Android (19,21,22,23,24) pode ser agairra aqui: https://github.com/anggrayudi/android-hidden-api

    Então, você pode usair a class WindowManagerGlobal diretamente paira obter todas as visualizações raiz, como,

     private void logRootViews() { WindowManagerGlobal windowManagerGlobal = WindowManagerGlobal.getInstance(); String[] rootViewNames = windowManagerGlobal.getViewRootNames(); for (String viewName : rootViewNames) { View rootView = windowManagerGlobal.getRootView(viewName); Log.i("", "Root view is: " + viewName + ": " + rootView); /*do what you want with the rootView*/ } } } private void logRootViews() { WindowManagerGlobal windowManagerGlobal = WindowManagerGlobal.getInstance(); String[] rootViewNames = windowManagerGlobal.getViewRootNames(); for (String viewName : rootViewNames) { View rootView = windowManagerGlobal.getRootView(viewName); Log.i("", "Root view is: " + viewName + ": " + rootView); /*do what you want with the rootView*/ } } 

    Saída:

    A vista de raiz é: com.example.paintsample/com.example.paintsample.PaintSample/android.view.ViewRootImpl@41deeff0: com.android.internal.policy.impl.PhoneWindow $ DecorView {41dcc278 VE …. R … …. 0,0-768,1184}

    A vista de raiz é: PopupWindow: 42887380/android.view.ViewRootImpl@42891820: android.widget.PopupWindow $ PopupViewContainer {42891450 VE …. …….. 0,0-424,618}

    Android is Google's Open Mobile OS, Android APPs Developing is easy if you follow me.