简体   繁体   中英

How to lock android screen during screen mirroring?

I am developing an android application for mirroring android screen to car display. I used MediaProjection for getting screen; using that api I was able to mirror screen and now I want to lock android screen during mirroring to avoid distraction of user.

Is there any way to lock only Android device screen during streaming? for example showing a fixed picture or freeze Device screen and stream the other layouts? Maybe using some APIs or etc?

I personally lock/unlock the UI for users by setting / clearing flags:

//The user cannot interact with the UI
private void disableUserInteraction() {
    (getActivity()).getWindow()
            .setFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE,
                    WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE);
}

//The user can interact with the UI
private void enableUserInteraction() {
    (getActivity()).getWindow()
            .clearFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE);
}

This will prevent the user to interact with the screen. You can then create a RelativeLayout with grayish color to make it obvious that the user needs to wait, and playing with its visibility when performing your actions:

<LinearLayout
    android:id="@+id/loading_mask"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="#B0000000"
    android:visibility="gone"/>

You can also add a ProgressDialog at the same time, that you show / dismiss:

//The UI message to show the user during the loading 
private void showProgressDialog() {
    if (progressDialog == null) {
        progressDialog = new ProgressDialog(Objects.requireNonNull(getParentFragment())
                .getActivity());
        progressDialog.setIndeterminate(true);
        progressDialog.setCancelable(false);
    }
    progressDialog.setMessage("Loading...");
    progressDialog.show();
}

private void dismissProgressDialog() {
    if (progressDialog != null) {
        progressDialog.dismiss();
    }
}

If you combine those three, I think it make a good waiting screen, then you can put those methods together like so:

//The user can interact with the UI, end of progressdialog and waiting mask
private void unlockUI() {
    loadingMask.setVisibility(View.GONE);
    dismissProgressDialog();
    enableUserInteraction();
}

//The user cannot interact with the UI, start of progressdialog and waiting mask
private void lockUI() {
    showProgressDialog();
    disableUserInteraction();
    loadingMask.setVisibility(View.VISIBLE);
}

And you just need to call lockUI() when you start and unlockUI() when the user can interact again with the screen.

Hope this helps, let us know how it went !

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM