android-Presentation双屏异显

最近碰到一个智能硬件,有两块屏幕,主屏幕和小屏幕,主界面执行各种操作流程,然后小屏幕展示数据,然后做一下数据交互 主要技术是Presentation + eventbus

step1: 清单文件 注册权限

html 复制代码
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.user.mathgame">
    <uses-permission android:name= "android.permission.SYSTEM_ALERT_WINDOW" />
    <uses-permission android:name= "android.permission.SYSTEM_OVERLAY_WINDOW"  />

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
<!--源码地址 https://github.com/YangHaoyi/MultiScreenDemo.git -->
        <service android:name=".MultiScreenService"/>
    </application>

</manifest>

step2:首页 主界面

java 复制代码
package com.example.user.mathgame;


import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.View;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.TextView;

import org.greenrobot.eventbus.EventBus;


/**
 * @author : YangHaoYi on 2019/1/2.
 * Email  :  yang.haoyi@qq.com
 * Description :多屏显示示例
 * Change : YangHaoYi on 2019/1/2.
 * Version : V 1.0
 * https://github.com/YangHaoyi/MultiScreenDemo.git 源码地址
 */
public class MainActivity extends FragmentActivity implements View.OnClickListener {

    /**
     * 展示第二块屏幕按钮
     **/
    private TextView tvShowPresentation;
    /**
     * Presentation逻辑控制中心
     **/
    private PresentationPresenter presentationPresenter;
    /**
     * 页面根布局
     **/
    private FrameLayout fmActivityContent;
    /** 地图页 **/
//    private MapFrameLayout mapFrameLayout;
    private Button btn_showPresentation;

    /**
     * 搜索页
     **/
//    private SearchFrameLayout searchFrameLayout;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        init();
    }

    /**
     * 初始化
     **/
    private void init() {
        initView();
        initEvent();
    }

    /**
     * 初始化视图
     **/
    private void initView() {
        tvShowPresentation = findViewById(R.id.tvShowPresentation);
        fmActivityContent = findViewById(R.id.fmActivityContent);
        btn_showPresentation= findViewById(R.id.btn_showPresentation);

//        initPage();
    }

 
    private void initEvent() {
        presentationPresenter = new PresentationPresenter();
        tvShowPresentation.setOnClickListener(this);
        btn_showPresentation.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tvShowPresentation:
                presentationPresenter.openSearchPresentation(MainActivity.this);
                break;
            case R.id.btn_showPresentation:
                MapEvent mapEvent = new MapEvent();
                mapEvent.setCode(MapEvent.ZOOM_IN);
                EventBus.getDefault().post(mapEvent);
                break;
            default:
                break;
        }
    }

     
}

step3: eventbus传递的数据类

java 复制代码
package com.example.user.mathgame;



/**
 * @author : YangHaoYi on  2019/5/814:17.
 * Email  :  yang.haoyi@qq.com
 * Description :
 * Change : YangHaoYi on  2019/5/814:17.
 * Version : V 1.0
 */
public class MapEvent {

    public static final int ZOOM_IN = 10086;
    public static final int TO_SEARCH = 2;

    private int code;

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }
}

step4: 服务 用于管理双屏的

java 复制代码
package com.example.user.mathgame;


import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.hardware.display.DisplayManager;
import android.os.Binder;
import android.os.IBinder;
import android.view.Display;
import android.view.WindowManager;

public class MultiScreenService extends Service {
    private DisplayManager mDisplayManager;
    private Display[] displays;
    private SecondScreenPresentation presentation;

    @Override
    public IBinder onBind(Intent intent) {
        return new MultiScreenBinder();
    }

    @Override
    public void onCreate() {
        super.onCreate();
        initPresentation();
    }

    private void initPresentation() {
        if (null == presentation) {
            mDisplayManager = (DisplayManager) this.getSystemService(Context.DISPLAY_SERVICE);
            displays = mDisplayManager.getDisplays();
            if (displays.length > 1) {
                // displays[1]是副屏
                presentation = new SecondScreenPresentation(this, displays[1]);
                presentation.getWindow().setType(WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY - 1);

            }
        }
    }

    public void showSearchPresentation() {
        presentation.show();
    }

    public class MultiScreenBinder extends Binder {
        public MultiScreenService getService() {
            return MultiScreenService.this;
        }
    }
}

step5: 屏幕管理类

java 复制代码
package com.example.user.mathgame;


        import android.app.Activity;
        import android.content.ComponentName;
        import android.content.Context;
        import android.content.Intent;
        import android.content.ServiceConnection;
        import android.os.IBinder;



/**
 * @author : YangHaoYi on  2019/4/30.
 * Email  :  yang.haoyi@qq.com
 * Description :离屏逻辑控制中心
 * Change : YangHaoYi on  2019/4/30.
 * Version : V 1.0
 */
public class PresentationPresenter {

    private MultiScreenService multiScreenService;
    private ServiceConnection serviceConnection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            multiScreenService = ((MultiScreenService.MultiScreenBinder) service).getService();
            //显示第二块屏幕
            multiScreenService.showSearchPresentation();
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
            //恢复置空
            multiScreenService = null;
        }
    };

    public void openSearchPresentation(Activity activity){
        Intent intent = new Intent(activity,MultiScreenService.class);
        activity.bindService(intent,serviceConnection, Context.BIND_AUTO_CREATE);

    }
}

step6: 第二块屏幕 集成谷歌的Presentation 实现双屏显示

java 复制代码
package com.example.user.mathgame;



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

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.EventBus;
 

/**
 * @author : YangHaoYi on 2019/1/2.
 *         Email  :  yang.haoyi@qq.com
 *         Description :双屏异显辅助屏幕
 *         Change : YangHaoYi on 2019/1/2.
 *         Version : V 1.0
 */
public class SecondScreenPresentation extends Presentation     {

    /** TAG **/
    private static final String TAG = "Presentation";
    /** 根布局 **/
//    private FrameLayout fmContent;

    private TextView tvSearch;


    public SecondScreenPresentation(Context outerContext, Display display) {
        super(outerContext, display);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.presentation_search);
        init();
    }

    private void init(){
        initView();
        initEvent();
    }

    private void initView(){
        tvSearch= findViewById(R.id.tvSearch);
//        fmContent = findViewById(R.id.fmContent);
//        initPage();
    }
 


    private void initEvent(){
        windowWidthAndHeightTest();
    }

    private void windowWidthAndHeightTest(){
        int height = getContext().getResources().getDisplayMetrics().heightPixels;
        int width = getContext().getResources().getDisplayMetrics().widthPixels;
        Log.d(TAG,"height_is"+height+"  width_is:"+width);
        System.out.println("SearchPresentation________height_is"+height+"  width_is:"+width);
    }

    /**  EventBus模拟点击  */

    /**  EventBus注册  */
    @Override
    public void show() {
        super.show();
        if(!EventBus.getDefault().isRegistered(this)){
            EventBus.getDefault().register(this);
        }
    }

    /**  EventBus解注册  */
    @Override
    public void dismiss() {
        EventBus.getDefault().unregister(this);
        super.dismiss();
    }


    /**  地图Event事件  */
    @Subscribe
    public void onMapEvent(MapEvent mapEvent){
        tvSearch.setText("onMapEvent:"+mapEvent.getCode());
     
    }
}
相关推荐
千里马学框架5 天前
一起学 Android 14:ShellTransition 屏幕旋转过程深度剖析
android·智能手机·性能优化·framework·性能·屏幕旋转·rotation
美狐美颜SDK开放平台5 天前
开发直播APP时如何接入视频美颜SDK?开发流程与注意事项
android·人工智能·计算机视觉·音视频·直播美颜sdk
AFinalStone5 天前
Android7 SystemUI源码解析(七)Keyguard锁屏模块深度解析
android·systemui
致远ccc5 天前
Google Play 上架前如何测试 App?多国家 Android 环境测试
android·app测试·googleplay·多国家应用测试
ttyyttemo5 天前
Kotlin 协程中的 Job 结构化并发与取消
android
sun0077005 天前
tbox 4g/5g切换,导致wan ip 改变,导致车机旧网络不可用。需要重启车机才行
android
其实防守也摸鱼5 天前
内网穿透与反向代理:原理、工具与实战指南
android·大数据·运维·安全·网络安全·自动化·渗透
AFinalStone5 天前
Android7 SystemUI 源码解析(四)NavigationBar 导航栏与 SystemBars
android·systemui
JMchen5 天前
属性动画原理与高级动画实现
android·kotlin·canvas
AFinalStone5 天前
Android7 SystemUI 源码解析(二)启动流程深度解析
android·systemui