Posted on 六月 21, 2018
Android分屏(双屏异显)
新来个机器,双屏幕金融终端,作为一个金融终端,连个非接读卡器都没有也是醉了,读卡写卡全靠NFC。。
既然要分屏,就研究一下分屏显示吧。
获取设备上的屏幕
DisplayManager mDisplayManager; Display[] displays; mDisplayManager = (DisplayManager)getSystemService(Context.DISPLAY_SERVICE); displays = mDisplayManager.getDisplays();
其中主屏是displays[0],副屏是displays[1]
新增类继承Presentation,用来显示副屏
Presentation继承自Dialog,用起来和Dialog挺像。
public class SplashSubScreen extends Presentation { public SplashSubScreen(Context outerContext, Display display) { super(outerContext, display); } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.xx); } }
显示副屏
mSplashSubScreen = new SplashSubScreen(this, displays[1]); mSplashSubScreen.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT); mSplashSubScreen.show();
运行发现崩溃报错:
BadTokenException: Unable to add window android.view.ViewRootImpl$W@2cb086cf — permission denied for window type
不知道是不是因为我这App全屏运行原因导致的,解决方法是加入权限:
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
Presentation的DataBinding
Presentation是继承自Dialog,所以按照Dialog的data binding方法即可。
mSubScreenSplashBinding = DataBindingUtil.inflate(LayoutInflater.from(getContext()), R.layout.sub_screen_splash, null, false); setContentView(mSubScreenSplashBinding.getRoot());
参考:
https://blog.csdn.net/wlwl0071986/article/details/48542923
https://stackoverflow.com/questions/34967868/how-to-use-data-binding-in-dialog