Android 副屏开发

前言

Android中的副屏开发需要Android版本API>=23
涉及权限

activity_screen.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.thomas.android.base.screen.ScreenActivity">

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/change_presentation_data"
        android:onClick="changePresentationDataClick"/>


</LinearLayout>

副屏类似dialog和activity
CustomPresentation



import android.app.Presentation;
import android.content.Context;
import android.os.Bundle;
import android.view.Display;
import android.widget.TextView;

import com.thomas.android.base.two.R;

/**
 * 副屏
 */
public class CustomPresentation extends Presentation {
    private boolean isChange;
    private int changeCount;

    private TextView tv_vice_change;

    boolean isChange() {
        return isChange;
    }

    void setChange(){
        changeCount ++ ;
        tv_vice_change.setText(String.valueOf(changeCount));
    }


    CustomPresentation(Context outerContext, Display display) {
        super(outerContext, display);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_presentation);
        tv_vice_change = findViewById(R.id.tv_vice_change);

    }

    /**
     * 副屏发生改变调用(息屏触发)
     */
    @Override
    public void onDisplayChanged() {
        super.onDisplayChanged();
        isChange = true;
        dismiss();
    }
}

ScreenActivity

import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.hardware.display.DisplayManager;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.provider.Settings;
import android.view.Display;
import android.view.View;
import android.view.WindowManager;

import com.thomas.android.base.two.R;

public class ScreenActivity extends AppCompatActivity {

    private DisplayManager displayManager;
    private CustomPresentation customPresentation;
    private boolean hasPermission;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_screen);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M ){
            displayManager = (DisplayManager) getSystemService(DISPLAY_SERVICE);
            initPermission();
        }

    }

    @RequiresApi(api = Build.VERSION_CODES.M)
    private void initPermission() {
        if (!Settings.canDrawOverlays(this)){
            Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION,
                    Uri.parse("package:"+getPackageName()));
            startActivityForResult(intent,10);
        }else {
            hasPermission = true;
        }

    }
    @Override
    protected void onResume() {
        super.onResume();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && hasPermission){
            new Thread(screenRunnable).start();
        }
    }

    /**
     * 初始化副屏
     * 为什么放在onResume中执行呢?为了防止息屏后客屏消失
     */
    private void initViceScreen(final Display display) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                // 屏幕发生改变时副屏会自动消失,因此需要将原本显示的副屏置为空避免占用
                if (customPresentation != null && customPresentation.isChange()){
                    customPresentation.dismiss();
                    customPresentation = null;
                }
                // 屏幕发生改变时需要重新创建
                if (customPresentation == null ){
                    customPresentation = new CustomPresentation(ScreenActivity.this,display);
                    if (customPresentation.getWindow() != null){
                        customPresentation.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
                        customPresentation.show();
                    }
                }
            }
        });

    }

    private Runnable screenRunnable = new Runnable() {
        @Override
        public void run() {
            initDisplay();
        }
    };

    /**
     * 获取屏幕数量
     */
    private void initDisplay() {
        try {
            Display[] displays = displayManager.getDisplays();
            // 说明有副屏
            if (displays.length >1){
                initViceScreen(displays[1]);
            }else {
                // 可能是息屏后又开屏需要时间获取屏幕
                Thread.sleep(1000);
                displays = displayManager.getDisplays();
                if (displays.length > 1){
                    initViceScreen(displays[1]);
                }
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (customPresentation != null){
            customPresentation.dismiss();
        }
    }

    public void changePresentationDataClick(View view) {
        if (customPresentation != null){
            customPresentation.setChange();
        }
    }

    @RequiresApi(api = Build.VERSION_CODES.M)
    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (Settings.canDrawOverlays(this)) {
            hasPermission = true;
            new Thread(screenRunnable).start();
        }
    }

}

如果有需要更多其他源码加Q875831461获取,备注来意

你可能感兴趣的:(Android 副屏开发)