Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/image/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java WindowManager坏令牌异常仅适用于Android 6_Java_Android Service_Android 6.0 Marshmallow - Fatal编程技术网

Java WindowManager坏令牌异常仅适用于Android 6

Java WindowManager坏令牌异常仅适用于Android 6,java,android-service,android-6.0-marshmallow,Java,Android Service,Android 6.0 Marshmallow,我有一个扩展服务的类 public class CustomService extends Service { private ImageView chatHead; private WindowManager.LayoutParams params ; private ImageView iv; @Override public void onCreate() { super.onCreate(); iv = new ImageView(this);

我有一个扩展服务的类

public class CustomService extends Service {
  private ImageView chatHead;
  private WindowManager.LayoutParams params ;
  private ImageView iv;

  @Override
  public void onCreate() {
    super.onCreate();
    iv = new ImageView(this);
    bitmap = BitmapFactory.decodeResource(getResources(), ...);
    bitmap = Bitmap.createScaledBitmap(...);
    iv.setImageBitmap(bitmap);

    //INITIALIZE FROM SCREEN DIMENSIONS
    windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);

    //PARAMETERS
    params= new WindowManager.LayoutParams(
            WindowManager.LayoutParams.WRAP_CONTENT,
            WindowManager.LayoutParams.WRAP_CONTENT,
            WindowManager.LayoutParams.TYPE_PHONE,
            WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
            PixelFormat.TRANSLUCENT);
    params.gravity = Gravity.TOP | Gravity.LEFT;

    params.x = 0;
    params.y = mHeight - chatHeadRadius ;

    windowManager.addView(iv, params); // HERE WE HAVE THE CRASH
  }
}
我通过以下方式从mainActivity创建服务:

        startService(new Intent(getApplication(), MyService.class));
这段代码在安卓4和安卓5的设备上运行得非常好。但当我试着在棉花糖上运行它时,我就崩溃了

我必须通知你,我已经试着申请许可了


在我的清单文件中,但它没有解决问题


有什么想法吗?

我终于找到了解决这个问题的办法(在一位同事的帮助下:)

因此,在我的主要活动中,所有的工作都是在开始服务之前完成的。 我必须检查用户是否已经启用了。 如果用户没有启用该权限,我将启动设置活动,以便用户自己启用该权限

public void checkDrawPermission() {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        if (!Settings.canDrawOverlays(this)) {
            Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION,
                    Uri.parse("package:" + getPackageName()));
            startActivityForResult(intent, 123);
        } else {
            // display over lay from service
            startService(new Intent(getApplicationContext(), MyService.class));
        }
    }else
    {
        // display over lay from service
        startService(new Intent(getApplicationContext(), MyService.class));
    }
}


@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode,resultCode,data);
    if (requestCode == 123) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            if (Settings.canDrawOverlays(this)) {
                // You have permission
                // display over lay from service
                startService(new Intent(getApplicationContext(), MyService.class));
            }
        }
    }
}
public void checkDrawPermission() {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        if (!Settings.canDrawOverlays(this)) {
            Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION,
                    Uri.parse("package:" + getPackageName()));
            startActivityForResult(intent, 123);
        } else {
            // display over lay from service
            startService(new Intent(getApplicationContext(), MyService.class));
        }
    }else
    {
        // display over lay from service
        startService(new Intent(getApplicationContext(), MyService.class));
    }
}


@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode,resultCode,data);
    if (requestCode == 123) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            if (Settings.canDrawOverlays(this)) {
                // You have permission
                // display over lay from service
                startService(new Intent(getApplicationContext(), MyService.class));
            }
        }
    }
}