窗口类型2002的权限被拒绝

时间:2018-10-13 10:54:49

标签: java android xml permissions runtime-error

错误

 java.lang.RuntimeException: Unable to create service com.appmaster.akash.messageplus.Pie: android.view.WindowManager$BadTokenException: Unable to add window android.view.ViewRootImpl$W@3aaac -- permission denied for window type 2002
                                                                               at android.app.ActivityThread.handleCreateService(ActivityThread.java:3414)
                                                                               at android.app.ActivityThread.-wrap4(Unknown Source:0)
                                                                               at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1683)
                                                                               at android.os.Handler.dispatchMessage(Handler.java:105)
                                                                               at android.os.Looper.loop(Looper.java:164)
                                                                               at android.app.ActivityThread.main(ActivityThread.java:6541)
                                                                               at java.lang.reflect.Method.invoke(Native Method)
                                                                               at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
                                                                               at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
                                                                            Caused by: android.view.WindowManager$BadTokenException: Unable to add window android.view.ViewRootImpl$W@3aaac -- permission denied for window type 2002
                                                                               at android.view.ViewRootImpl.setView(ViewRootImpl.java:789)
                                                                               at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:356)
                                                                               at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:92)
                                                                               at com.appmaster.akash.messageplus.Pie.onCreate(Pie.java:51)
                                                                               at android.app.ActivityThread.handleCreateService(ActivityThread.java:3404)
                                                                               at android.app.ActivityThread.-wrap4(Unknown Source:0) 
                                                                               at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1683) 
                                                                               at android.os.Handler.dispatchMessage(Handler.java:105) 
                                                                               at android.os.Looper.loop(Looper.java:164) 
                                                                               at android.app.ActivityThread.main(ActivityThread.java:6541) 
                                                                               at java.lang.reflect.Method.invoke(Native Method) 
                                                                               at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240) 
                                                                               at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767) 

代码

public class Pie extends Service {
WindowManager wm;
LayoutInflater li;
RelativeLayout ll;
View myview;
@Override
public IBinder onBind(Intent intent) {
    // TODO Auto-generated method stub
    return null;
}

@Override
public void onCreate() {
    // TODO Auto-generated method stub
    super.onCreate();
    wm = (WindowManager) getSystemService(WINDOW_SERVICE);
    li = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
    ll = new RelativeLayout(this);
    ll.setBackgroundColor(Color.RED);
    LinearLayout.LayoutParams layoutParameteres = new LinearLayout.LayoutParams(
            50, 500);
    ll.setBackgroundColor(Color.argb(66,255,0,0));
    ll.setLayoutParams(layoutParameteres);

    final WindowManager.LayoutParams parameters = new WindowManager.LayoutParams(
            50, 500, WindowManager.LayoutParams.TYPE_PHONE,
            WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
            PixelFormat.TRANSLUCENT);
    parameters.gravity = Gravity.CENTER | Gravity.CENTER;
    parameters.x = 0;
    parameters.y = 0;

    wm.addView(ll, parameters);
    WindowManager.LayoutParams updatedParameters = parameters;

    ll.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
           Intent intent = new Intent(Pie.this,PieOptions.class);
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            startService(intent);

        }
    });

}

@Override
public void onDestroy() {
    super.onDestroy();
    stopSelf();
}

wm.addView(ll,parameters); 行上的错误 我已授予“在其他应用程序上显示”权限,但仍显示此错误。这是为什么? 有人请帮帮我。在我将相同的代码用于真实设备之前,它在授予权限后曾经可以工作,但是现在我尝试使用模拟器,但它无法正常工作

0 个答案:

没有答案