如何在oreo的锁定屏幕上绘制视图?

时间:2018-07-16 00:44:01

标签: android lockscreen android-windowmanager layoutparams android-layoutparams

这是我的代码。我试图在锁定屏幕上绘制视图。

在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) {
    }
}

请检查此代码。有什么问题吗 ? 预先感谢。

1 个答案:

答案 0 :(得分:0)

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

相关问题