最近碰到一个智能硬件,有两块屏幕,主屏幕和小屏幕,主界面执行各种操作流程,然后小屏幕展示数据,然后做一下数据交互 主要技术是Presentation + eventbus
step1: 清单文件 注册权限
<?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:首页 主界面
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传递的数据类
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: 服务 用于管理双屏的
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: 屏幕管理类
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 实现双屏显示
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());
}
}