我正在使用 MediaProjection 截屏。这就是我正在做的事情。我使用服务创建了一个覆盖图标。单击覆盖图标时会截取屏幕截图。问题是每当应用程序被按下后退按钮或手动杀死时刷它 MediaProjection 对象丢失。有没有办法维护 MediaProjection 并避免每次应用程序被杀死时请求 MediaProjection。我已经看到了,但我仍然无法做到。
在我的 Mainactivity Onclick 中包含 startActivityForResult ,结果 onActivityResult 如下:
@Override
protected void onActivityResult(final int requestCode,final int resultCode,final Intent data)
{
if (requestCode == requestcode)
{
if (resultCode == RESULT_OK)
{
Singelton.setScreenshotPermission((Intent) data.clone());
Singelton.putmanger(mediaProjectionManager);
}else if (resultCode==RESULT_CANCELED) {
Singelton.setScreenshotPermission(null);
}
}
}
辛格尔顿类如下:
protected static void setScreenshotPermission(final Intent permissionIntent)
{
screenshotPermission = permissionIntent;
//screenshotPermission becomes null once the application is killed
}
public static MediaProjection getData()
{
return (mediaProjection);
}
public static void getScreenshotPermission()
{
if (screenshotPermission != null)
{
Log.d("screenshotpermisson", "screenshotPermission != null ");
if(mediaProjection!=null)
{
Log.d("mediaprojection", "mediaprojection != null ");
mediaProjection.stop();
mediaProjection = null;
}
mediaProjection = mediaProjectionManager.getMediaProjection(Activity.RESULT_OK, (Intent) screenshotPermission.clone());
}
else
{
//Here I need to request for media projection again without starting activity
}
}
我的覆盖图标句柄的服务类单击如下:
public void createDisplay()
{
if (mediaProjection == null)
{
Singelton.getScreenshotPermission();
mediaProjection = Singelton.getData();
}
mImageReader = ImageReader.newInstance(width, height, PixelFormat.RGBA_8888, 1);
vd = mediaProjection.createVirtualDisplay("screen-mirror", width, height, mDensity, DisplayManager.VIRTUAL_DISPLAY_FLAG_AUTO_MIRROR, mImageReader.getSurface(), null, null);
mImageReader.setOnImageAvailableListener(new ImageReader.OnImageAvailableListener(){..}
}
我是 android 新手,很难弄清楚这一点。任何帮助将不胜感激。