0

这是我的代码。我正在尝试在锁定屏幕上绘制视图。它在 android 7.1 上运行得非常好,但是当我在 oreo 上运行它时,它不起作用(视图不能出现在锁定屏幕上,但是当你解锁屏幕时,视图出现并且服务停止)

@Override
public int onStartCommand(Intent origIntent, int flags, int startId) {
    if (windowParams == null) {
        windowParams = new WindowManager.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.TYPE_SYSTEM_ALERT, 65794, -2);
        if (origIntent != null) {
            demo = origIntent.getBooleanExtra("demo", false);
            windowParams.type = origIntent.getBooleanExtra("demo", false) ? WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY : Utils.isSamsung(getApplicationContext()) ? WindowManager.LayoutParams.TYPE_TOAST : WindowManager.LayoutParams.TYPE_SYSTEM_ERROR;
        } else
            windowParams.type = Utils.isSamsung(getApplicationContext()) ? WindowManager.LayoutParams.TYPE_TOAST : WindowManager.LayoutParams.TYPE_SYSTEM_ERROR;
        if (prefs.orientation.equals("horizontal"))
            //Setting screen orientation if horizontal
            windowParams.screenOrientation = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE;
        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()));
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                if (Utils.doesIntentExist(this, intent))
                    startActivity(intent);
                return super.onStartCommand(origIntent, flags, startId);
            }

        windowManager.addView(frameLayout, windowParams);
        if (prefs.homeButtonDismiss)
            samsungHelper.startHomeButtonListener();
        raiseToWake = origIntent != null && origIntent.getBooleanExtra("raise_to_wake", false);
        if (raiseToWake) {
            final int delayInMilliseconds = 10000;
            UIHandler.postDelayed(() -> {
                stayAwakeWakeLock.acquire();
                stayAwakeWakeLock.release();
            }, 100);
            UIHandler.postDelayed(() -> {
                turnScreenOff();
                stoppedByShortcut = true;
                stopThis();
                Utils.logDebug(MAIN_SERVICE_LOG_TAG, "Stopping service after delay");
            }, delayInMilliseconds);
        }
    }
    return super.onStartCommand(origIntent, flags, startId);
}
 private void showBlackScreen(boolean show) {
    if (blackScreenParams == null) {
        blackScreenParams = new WindowManager.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.TYPE_SYSTEM_ALERT, 65794, -2);
        blackScreenParams.type = Utils.isSamsung(getApplicationContext()) ? WindowManager.LayoutParams.TYPE_TOAST : WindowManager.LayoutParams.TYPE_SYSTEM_ERROR;
    }
    if (blackScreen == null)
        blackScreen = new FrameLayout(this);
    blackScreen.setBackgroundColor(Color.BLACK);
    blackScreen.setForegroundGravity(Gravity.CENTER);
    try {
        if (show) {
            if (!blackScreen.isAttachedToWindow())
                windowManager.addView(blackScreen, blackScreenParams);
        } else if (blackScreen.isAttachedToWindow())
            windowManager.removeView(blackScreen);
    } catch (IllegalStateException ignored) {
    }
}

请检查此代码。有什么问题吗 ?提前致谢。

4

1 回答 1

-1

从 API 26 开始,android 不允许在锁定屏幕和通知托盘上进行绘制。

于 2018-07-25T20:23:54.620 回答