Die projizierte Bildschirm mit der Medienprojektions -API wird in einem Overlay -Überlagerungen in Android Studio nicht
Posted: 19 Aug 2025, 13:56
Ich entwickle eine App, die den Bildschirm mithilfe der Medienprojektions -API aufzeichnet und die Daten in eine Overlay -Oberflächenansicht übertragen. Die Oberflächenansicht und die Überlagerungskomponenten sind jedoch sichtbar, aber die projizierte Anzeige war in der Oberflächenansicht nicht sichtbar. (here is my repository to try it out: https://github.com/xinzhao2627/eapp3/tr ... plicitapp3)
(Using Android 16, API 36)
My overlay in a service class looks like this:
von notificationHelper.class
(Using Android 16, API 36)
My overlay in a service class looks like this:
Code: Select all
@RequiresApi(api = Build.VERSION_CODES.O)
private void setupOverlay() {
LayoutInflater lf = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
// this is the overlay view
View view = lf.inflate(R.layout.activity_overlay, null);
screenMirror = view.findViewById(R.id.screenMirror);
mSurface = screenMirror.getHolder().getSurface();
screenMirror.getHolder().addCallback(new SurfaceHolder.Callback() {
@Override
public void surfaceCreated(SurfaceHolder holder) {
mSurface = holder.getSurface();
if (mMediaProjection != null) startScreenCapture();
}
@Override public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {}
@Override public void surfaceDestroyed(SurfaceHolder holder) {}
});
windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
WindowManager.LayoutParams params = new WindowManager.LayoutParams(
900,
500,
WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY,
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE,
PixelFormat.OPAQUE
);
params.gravity = Gravity.TOP | Gravity.LEFT;
params.x = 100;
params.y = 100;
// init the imageview
windowManager.addView(view, params);
}
< /code>
In diesem obigen Code wird die Oberflächenansicht oben auf dem Bildschirm angezeigt. Dann habe ich die folgende Funktion startScreencapture () erstellt: < /p>
public void startScreenCapture() {
MediaProjection.Callback callback = new MediaProjection.Callback() {
@Override
public void onStop() {
super.onStop();
stopScreenCapture();
}
};
mMediaProjection.registerCallback(callback, null);
mVirtualDisplay = mMediaProjection.createVirtualDisplay(
"ScreenCapture",
900,
500,
getResources().getDisplayMetrics().densityDpi,
DisplayManager.VIRTUAL_DISPLAY_FLAG_AUTO_MIRROR,
mSurface,
null,
null
);
}
< /code>
Daher sollte der Code die Anzeige in mein MSurface projizieren. Es zeigt jedoch nur einen leeren Bildschirm und es gibt keine Fehlerprotokolle, daher kann ich die wahrscheinliche Ursache nicht genau bestimmen. Surfaceview zu TextureView gibt es jedoch keine visuellen Änderungen. />android.permission.SYSTEM_ALERT_WINDOW
Regarding the notification channels, here are the code snippet:
[b]From the OverlayService.class[/b]
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
NotificationHelper.createNotificationChannel(getApplicationContext());
notification = NotificationHelper.createNotification(getApplicationContext());
startForeground(SERVICE_ID, notification, ServiceInfo.FOREGROUND_SERVICE_TYPE_MEDIA_PROJECTION);
}
int rc = intent.getIntExtra("resultCode", -1);
Intent d = intent.getParcelableExtra("data");
if (rc != -1 && d != null) {
mMediaProjection = mediaProjectionManager.getMediaProjection(rc, d);
if (mMediaProjection != null && mSurface != null) {
startScreenCapture();
}
}
return START_STICKY;
}
Code: Select all
private static final String CHANNEL_ID = "screen_recording_channel";
public static Notification createNotification(Context context) {
Intent intent = new Intent(context, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(
context,
0,
intent,
PendingIntent.FLAG_IMMUTABLE
);
return new NotificationCompat.Builder(context, CHANNEL_ID)
.setContentTitle("Screen recording")
.setContentText("Recording in progress...")
.setSmallIcon(R.drawable.ic_launcher_foreground)
.setContentIntent(pendingIntent)
.build();
}
public static void createNotificationChannel(Context context) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel serviceChannel = new NotificationChannel(
CHANNEL_ID,
"Screen Recording Channel",
NotificationManager.IMPORTANCE_DEFAULT
);
NotificationManager notificationManager =
(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
if (notificationManager != null) {
notificationManager.createNotificationChannel(serviceChannel);
}
}
}