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 (前臺服務)
官方文檔

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章