gpt4 book ai didi

android - 在辅助监视器上运行单独的应用程序

转载 作者:行者123 更新时间:2023-12-04 23:53:50 26 4
gpt4 key购买 nike

Android 支持将应用程序渲染到副屏(4.2 中添加),但是否可以在主显示器上运行一个应用程序而在副显示器上运行另一个应用程序?

最佳答案

Android SDK 附带一个Presentation 类,它允许前台 Activity 在外部显示器上显示替代内容。这是针对“第二屏幕”应用程序构建的,您可以在其中在外部显示器上观看电影并使用触摸屏控制播放、与 friend 聊天等。Presentation 的局限性是它实际上是一个 Dialog,因此仅在由前台 Activity 驱动时才有效。

但是,使用 WindowManager 可以让 Service 在外部显示器上显示内容。我有 a PresentationService class演示了该技术并简化了它的使用。给定一个表示外部显示器的 Display 对象(例如,DisplayManager),您可以让 WindowManager 将内容直接发送到该 Display,来自您膨胀或以其他方式创建的一些 View

/***
Copyright (c) 2014 CommonsWare, LLC

Licensed under the Apache License, Version 2.0 (the "License"); you may
not use this file except in compliance with the License. You may obtain
a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package com.commonsware.cwac.preso;

import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.graphics.PixelFormat;
import android.os.IBinder;
import android.view.ContextThemeWrapper;
import android.view.Display;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;

/**
* A service that drives a presentation from the background. Use this when
* presentations need to span activities or when they should be occurring even
* if the app's UI has moved to the background.
*
* This is an abstract class -- create a subclass and override getThemeId() and
* buildPresoView().
*/
public abstract class PresentationService extends Service implements
PresentationHelper.Listener {
/**
* @return the theme to use for driving the resources used by
* this presentation
*/
protected abstract int getThemeId();

/**
* Override this to provide the UI that goes into the presentation.
* This works somewhat like a fragment's onCreateView().
*
* @param ctxt a Context, in case you need one, but note that it will
* <i>not</i> be an activity
* @param inflater a LayoutInflater, in case you need one for creating
* the UI
* @return the View that should be shown on the external display
*/
protected abstract View buildPresoView(Context ctxt,
LayoutInflater inflater);

private WindowManager wm=null;
private View presoView=null;
private PresentationHelper helper=null;

/**
* {@inheritDoc}
*/
@Override
public IBinder onBind(Intent intent) {
return(null);
}

/**
* {@inheritDoc}
*/
@Override
public void onCreate() {
super.onCreate();

helper=new PresentationHelper(this, this);
helper.onResume();
}

/**
* {@inheritDoc}
*/
@Override
public void onDestroy() {
helper.onPause();

super.onDestroy();
}

/**
* {@inheritDoc}
*/
@Override
public void showPreso(Display display) {
Context presoContext=createPresoContext(display);
LayoutInflater inflater=LayoutInflater.from(presoContext);

wm=
(WindowManager)presoContext.getSystemService(Context.WINDOW_SERVICE);

presoView=buildPresoView(presoContext, inflater);
wm.addView(presoView, buildLayoutParams());
}

/**
* {@inheritDoc}
*/
@Override
public void clearPreso(boolean switchToInline) {
if (presoView != null) {
try {
wm.removeView(presoView);
}
catch (Exception e) {
// probably the window is gone, don't worry, be
// happy
}
}

presoView=null;
}

protected WindowManager.LayoutParams buildLayoutParams() {
return(new WindowManager.LayoutParams(
WindowManager.LayoutParams.MATCH_PARENT,
WindowManager.LayoutParams.MATCH_PARENT,
0,
0,
WindowManager.LayoutParams.TYPE_TOAST,
0, PixelFormat.OPAQUE));
}

private Context createPresoContext(Display display) {
Context displayContext=createDisplayContext(display);
final WindowManager wm=
(WindowManager)displayContext.getSystemService(WINDOW_SERVICE);

return(new ContextThemeWrapper(displayContext, getThemeId()) {
@Override
public Object getSystemService(String name) {
if (Context.WINDOW_SERVICE.equals(name)) {
return(wm);
}

return(super.getSystemService(name));
}
});
}
}

(来源v0.4.6)

关于android - 在辅助监视器上运行单独的应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42567220/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com