user3546693
user3546693

Reputation: 364

unable to add window token null is not for an application when using accessibility in android

I am trying to create a transparent activity in front of another app's login page. MyAccessibilityService Class extend the AccessibilityService class. When login page is identified the tranparentActivity() function will be called to show the dialog box, but I will get the error like unable to add window token null is not for an application when using accessibility in android.

My Code:

      public class MyAccessibilityService extends AccessibilityService {
       public void onAccessibilityEvent(AccessibilityEvent event)
{
    try{
    AccessibilityNodeInfo focusedNodeSrc = event.getSource();
    AccessibilityNodeInfo childNode;
    if(focusedNodeSrc.isPassword()){
        transparentActivity();
    }
    else{
        AccessibilityNodeInfo parFocusedNode=focusedNodeSrc.getParent();
        int countChild = parFocusedNode.getChildCount();
        for(int i=0;i<countChild;i++){
            childNode = parFocusedNode.getChild(i);
            if(childNode.isPassword()){
                transparentActivity();
            }}}}
catch(Exception e){
    e.printStackTrace();
    Toast.makeText(MyAccessibilityService.this, e.toString(), Toast.LENGTH_LONG).show();
}}
public void transparentActivity(){
    // custom dialog
                final Dialog dialog = new Dialog(getApplicationContext());
                dialog.setContentView(R.layout.custom);
                dialog.setTitle("Title...");
                // set the custom dialog components - text, image and button
                TextView text = (TextView) dialog.findViewById(R.id.text);
                text.setText("Android custom dialog example!");
                ImageView image = (ImageView) dialog.findViewById(R.id.image);
                image.setImageResource(R.drawable.ic_launcher);
                Button dialogButton = (Button) dialog.findViewById(R.id.dialogButtonOK);
                // if button is clicked, close the custom dialog
                dialogButton.setOnClickListener(new OnClickListener() {
                    public void onClick(View v) {
                        dialog.dismiss();}});
                dialog.show();}

I searched in google about this error and from this I understand it is a context problem. The given solution from google is not applicable in my case I think. So please can anyone post any suggestions on how to eliminate this error?

Upvotes: 0

Views: 265

Answers (1)

Happy
Happy

Reputation: 1091

You can use getApplicationContext(), but after that line, you should add this flag of WindowManager:

dialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);

and the error will not show

Upvotes: 3

Related Questions