How to validate what the Android getEventText (event) returns

1

I'm working with accessibility, listening to those events. I'm validating like this.

if ("la administración del dispositivo" == getEventText(event) ){
                        Metodo(); 
                    }

It is assumed that getEventText (event) returns a text to me, and I am validating that if the text is equal to that, the method must be executed, but nothing happens.  However if valid for example

if(event.getPackageName().toString().equals("com.android.settings")){

    Si ejecuta.

    }
    
asked by JDeveloper 30.06.2016 в 23:45
source

2 answers

1

The text obtained by getEventText () is not related to the package, it is related to an interaction event with the application.

private String getEventText(AccessibilityEvent event) {

    if (event.getText() == "TYPE_VIEW_CLICKED"){
         // evento vista click.
         return "TYPE_VIEW_CLICKED";
           } else if( event.getText() == "TYPE_VIEW_FOCUSED"){
          // evento enfoque vista.
          return "TYPE_VIEW_FOCUSED";
    }

 }
    
answered by 01.07.2016 в 00:58
0

I do not know about getEventText(event) but if event.getText () works but not all applications give you the text you want,

  • Test Logear the result
  • To check strings in Java you should not use str1 == str2 but str1.equals(str2)
  • The full text does not seem to be that "device management" if not a part so it uses the contains function
  • Try something like this:

    if(event.getPackageName().toString().equals("com.android.settings o la app que quieres capturar")){
        String text = event.getText().toString();
    Log.d("AppEjemplo",text);
        if (text.contains("la administración del dispositivo")  ){
                                Metodo(); 
    }
    }
    

    You can also filter by classes In this for now if the method works event.getText ()

    if (event.getClassName().equals("android.app.AlertDialog")) {...
    

    For other applications you have to go into the controls to get the text of each component. Ex:

    AccessibilityRecordCompat record = AccessibilityEventCompat.asRecord(event);
    final AccessibilityNodeInfoCompat source = record.getSource();
    StringBuilder sb=new StringBuilder();
    private void logControls(AccessibilityNodeInfoCompat source, StringBuilder sb) {
            if (source != null) {
                for (int i = 0; i < source.getChildCount(); i++) {
                    final AccessibilityNodeInfoCompat currentSource = source.getChild(i);
                    if(currentSource.getClassName().equals("android.widget.TextView")){
                        sb.append(currentSource.getText()+" ");
                    }else if(currentSource.getClassName().equals("android.widget.EditText")){
                        mediatekEditText=currentSource;
                    }
                    logControls(currentSource, sb);
                }
            }
        }
    
        
    answered by 21.07.2017 в 18:38