Create a Service class like so.
public class MultiDisplayService extends Service {
@Override
public void onCreate() {
super.onCreate();
DisplayManager dm = (DisplayManager)getApplicationContext().getSystemService(DISPLAY_SERVICE);
if (dm != null){
Display dispArray[] = dm.getDisplays(DisplayManager.DISPLAY_CATEGORY_PRESENTATION);
if (dispArray.length>0){
Display display = dispArray[0];
Log.e(TAG,"Service using display:"+display.getName());
Context displayContext = getApplicationContext().createDisplayContext(display);
WindowManager wm = (WindowManager)displayContext.getSystemService(WINDOW_SERVICE);
View view = LayoutInflater.from(displayContext).inflate(R.layout.fragment_main,null);
final WindowManager.LayoutParams params = new WindowManager.LayoutParams(
WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.TYPE_TOAST,
WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN,
PixelFormat.TRANSLUCENT);
wm.addView(view, params);
}
}
}
Start the service, perhaps in your Application class.
public class MultiDisplayApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
startService(new Intent(this, MultiDisplayService.class));
}
}
You will probably need more complex display add/remove logic based on DisplayManager.DisplayListener
mDisplayManager = (DisplayManager) this.getSystemService(Context.DISPLAY_SERVICE);
mDisplayManager.registerDisplayListener(this, null);
Using WindowManager.LayoutParams.TYPE_TOAST
requires no permissions but seems like a hack. WindowManager.LayoutParams.TYPE_SYSTEM_ALERT
might be more reasonable, but requieres
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
in your AndroidManifest.