无法添加 window 在 android 中使用辅助功能时,令牌 null 不适用于应用程序
unable to add window token null is not for an application when using accessibility in android
我正在尝试在另一个应用程序的登录页面前创建一个透明 activity。 MyAccessibilityService
Class 扩展 AccessibilityService
class。当识别登录页面时,将调用 tranparentActivity()
函数来显示对话框,但我会收到类似 unable to add window token null is not for an application when using accessibility in android
.
的错误
我的代码:
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();}
我在 google 中搜索了有关此错误的信息,据此我了解到这是上下文问题。我认为 google 的给定解决方案不适用于我的情况。所以请任何人 post 关于如何消除此错误的任何建议?
你可以使用getApplicationContext()
,但是在那行之后,你应该添加WindowManager的这个标志:
dialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
错误不会显示
我正在尝试在另一个应用程序的登录页面前创建一个透明 activity。 MyAccessibilityService
Class 扩展 AccessibilityService
class。当识别登录页面时,将调用 tranparentActivity()
函数来显示对话框,但我会收到类似 unable to add window token null is not for an application when using accessibility in android
.
我的代码:
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();}
我在 google 中搜索了有关此错误的信息,据此我了解到这是上下文问题。我认为 google 的给定解决方案不适用于我的情况。所以请任何人 post 关于如何消除此错误的任何建议?
你可以使用getApplicationContext()
,但是在那行之后,你应该添加WindowManager的这个标志:
dialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
错误不会显示