前言

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获取,备注来意

更多相关文章

  1. Android对话框的高级设置《二》设置对话框按钮的透明度和对话框
  2. android图片等比例缩放 填充屏幕
  3. android:screenOrientation 强制屏幕方向
  4. 如何让Android屏幕只能上下翻转
  5. Android:漫画APP开发笔记之ListView中图片按屏幕宽度缩放
  6. Android 底层渲染 - 屏幕刷新机制源码分析
  7. Android多屏幕支持Multi-WindowSupport
  8. Android中获取屏幕相关信息(屏幕大小,状态栏、标题栏高度)

随机推荐

  1. android 反编译必知-public.xml
  2. Android单例模式
  3. Android获得文件夹大小
  4. 继承FlexboxLayout自定义可自动换行的tag
  5. android 工具类
  6. android手机游戏入门之back键使用
  7. 2.4.8 滚动视图
  8. 使用Android消息机制实现点击开始计数和
  9. Android(安卓)Map 开发之高德地图
  10. How to get web browser history using c