Android10适配-限制对屏幕内容的访问

AndroidQ版本官方限制了对屏幕内容的访问。

为了保护用户的屏幕内容,Android 10 更改了 READ_FRAME_BUFFER、CAPTURE_VIDEO_OUTPUT 和 CAPTURE_SECURE_VIDEO_OUTPUT 权限的作用域,从而禁止以静默方式访问设备的屏幕内容。从 Android 10 开始,这些权限只能通过签名访问。
需要访问设备屏幕内容的应用应使用 MediaProjection API,此 API 会显示提示,要求用户同意访问。

如果还使用之前的逻辑访问屏幕内容的话,会报如下错误:

java.lang.SecurityException: Media projections require a foreground service of type ServiceInfo.FOREGROUND_SERVICE_TYPE_MEDIA_PROJECTION

那么如何获取屏幕内容呢?你需要以下几步。

1、请求用户授权

this.projectionManager = (MediaProjectionManager)activity.getSystemService(Context.MEDIA_PROJECTION_SERVICE);
activity.startActivityForResult(this.projectionManager.createScreenCaptureIntent(), 1000);

执行上面的代码,会弹出系统的提示用户弹框,选择“立即开始”或者“取消”后,会在onActivityResult中进行回调。

2、新建一个CaptureScreenService

@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
public class CaptureScreenService extends Service {
    private int mResultCode;
    private Intent mResultData;
    private MediaProjectionManager projectionManager;
    private MediaProjection mMediaProjection;

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        createNotificationChannel();
        mResultCode = intent.getIntExtra("code", -1);
        mResultData = intent.getParcelableExtra("data");

        this.projectionManager = (MediaProjectionManager)getSystemService(Context.MEDIA_PROJECTION_SERVICE);
        mMediaProjection = projectionManager.getMediaProjection(mResultCode, Objects.requireNonNull(mResultData));

        ScreenCaptureManager.getInstance().start(mMediaProjection);
        return super.onStartCommand(intent, flags, startId);
    }

    private void createNotificationChannel() {
        Notification.Builder builder = new Notification.Builder(this.getApplicationContext()); //获取一个Notification构造器
        Intent nfIntent = new Intent(this, MainActivity.class); //点击后跳转的界面,可以设置跳转数据

        builder.setContentIntent(PendingIntent.getActivity(this, 0, nfIntent, 0)) // 设置PendingIntent
                .setLargeIcon(BitmapFactory.decodeResource(this.getResources(), R.mipmap.ic_launcher)) // 设置下拉列表中的图标(大图标)
                //.setContentTitle("SMI InstantView") // 设置下拉列表里的标题
                .setSmallIcon(R.mipmap.ic_launcher) // 设置状态栏内的小图标
                .setContentText("is running......") // 设置上下文内容
                .setWhen(System.currentTimeMillis()); // 设置该通知发生的时间

        /*以下是对Android 8.0的适配*/
        //普通notification适配
        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            builder.setChannelId("notification_id");
        }
        //前台服务notification适配
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationManager notificationManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
            NotificationChannel channel = new NotificationChannel("notification_id", "notification_name", NotificationManager.IMPORTANCE_LOW);
            notificationManager.createNotificationChannel(channel);
        }

        Notification notification = builder.build(); // 获取构建好的Notification
        notification.defaults = Notification.DEFAULT_SOUND; //设置为默认的声音
        startForeground(110, notification);

    }

    @Override
    public void onDestroy() {
    	super.onDestroy();
        stopForeground(true);
    }
}

3、注册CaptureScreenService,并添加foregroundServiceType属性。

<service android:name=".CaptureScreenService"
            android:enabled="true"
            android:foregroundServiceType="mediaProjection"/>

4、在activity的onActivityResult中启动前台服务

	@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(resultCode == RESULT_OK && requestCode == 1000) {
        	//判断系统版本选择不同处理方法
            if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
            	//启动前台服务
                service = new Intent(this, CaptureScreenService.class);
                service.putExtra("code", resultCode);
                service.putExtra("data", data);
                startForegroundService(service);
            }else {
                ScreenCaptureManager.getInstance().start(resultCode, data);
            }
        }
    }

5、在CaptureScreenService的onStartCommand执行访问屏幕内容的逻辑。

	@Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        createNotificationChannel();
        mResultCode = intent.getIntExtra("code", -1);
        mResultData = intent.getParcelableExtra("data");

        this.projectionManager = (MediaProjectionManager)getSystemService(Context.MEDIA_PROJECTION_SERVICE);
        mMediaProjection = projectionManager.getMediaProjection(mResultCode, Objects.requireNonNull(mResultData));

        ScreenCaptureManager.getInstance().start(mMediaProjection);
        return super.onStartCommand(intent, flags, startId);
    }

需要在前台服务启动后5s内调用startForeground方法,否则也会报错。
所以先执行createNotificationChannel(),启动startForeground。
获取屏幕内容的逻辑封装在ScreenCaptureManager内,如下:

   public void start(MediaProjection mediaProjection) {
        this.mediaProjection = mediaProjection;
        if (this.projectionManager != null) {
            this.initVirtualDisplay(this.activity);
            this.mediaProjection.registerCallback(new ScreenCaptureManager.MediaProjectionStopCallback(), (Handler)null);
        }
    }
    
    public void initVirtualDisplay(Activity activity) {
        DisplayMetrics metrics = activity.getResources().getDisplayMetrics();
        int density = metrics.densityDpi;
        Point size = new Point();
        activity.getWindowManager().getDefaultDisplay().getRealSize(size);
        int width = size.x;
        int height = size.y;
        this.imageReader = ImageReader.newInstance(width, height, 1, 1);
        this.imageReader.setOnImageAvailableListener(new ScreenCaptureManager.ImageAvailableListener(), (Handler)null);
        String virtualDisplayName = "Screenshot";
        int flags = 9;
        this.virtualDisplay = this.mediaProjection.createVirtualDisplay(virtualDisplayName, width, height, density, flags, this.imageReader.getSurface(), (VirtualDisplay.Callback)null, (Handler)null);
    }

在ScreenCaptureManager.ImageAvailableListener()的onImageAvailable(ImageReader reader)就可以拿到屏幕的bitmap信息了。

6、记得关闭服务。

	//这是CaptureScreenService的onDestroy()方法
 	@Override
    public void onDestroy() {
    	super.onDestroy();
        stopForeground(true);
    }

stopForeground中的参数表示:是否移除之前的通知。

	//这是activity的onDestroy()
	@Override
    protected void onDestroy() {
        super.onDestroy();
        //停止获取屏幕内容
        ScreenCaptureManager.getInstance().stop();
        //关闭服务
        Intent service = new Intent(this, CaptureScreenService.class);
        stopService(service);
    }

7、记得加上FOREGROUND_SERVICE权限。

<uses-permission android:name="android.permission.FOREGROUND_SERVICE"/>

8、compileSdkVersion设置为29,否则manifest中没有foregroundServiceType属性。

至此你就可以愉快的使用屏幕内容了!

demo地址:限制对屏幕内容的访问。
参考文章:
MediaProjections in Android Q
Android Foreground Service (前台服务)
官方文档

你可能感兴趣的:(Android适配,访问屏幕,适配,屏幕,前台服务,android)