媒体投影需要Android Pie和Q中的ServiceInfo.FOREGROUND_SERVICE TYPE_MEDIA_PROJECTION类型的前台服务。>

问题描述 投票:0回答:1

[任何人都知道为什么会发生此错误,媒体投影需要在getMediaProjection()上使用ServiceInfo.FOREGROUND_SERVICE_TYPE_MEDIA_PROJECTION类型的前台服务,即使我在调用startforeground()方法之后也调用了getMediaProjection()方法。!

   private void startForeground() {

    Intent activityIntent = new Intent(this, MainActivity.class);
    activityIntent.setAction("stop");
    PendingIntent contentIntent = PendingIntent.getActivity(this, 0, activityIntent, PendingIntent.FLAG_UPDATE_CURRENT);

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {

        String channelId = "001";
        String channelName = "myChannel";
        NotificationChannel channel = new NotificationChannel(channelId, channelName, NotificationManager.IMPORTANCE_NONE);
        channel.setLightColor(Color.BLUE);
        channel.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE);

        NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

        if (manager != null) {
            manager.createNotificationChannel(channel);
            Notification notification = new Notification.
                    Builder(getApplicationContext(), channelId)
                    .setOngoing(true)
                    .setSmallIcon(R.mipmap.ic_launcher)
                    .setCategory(Notification.CATEGORY_SERVICE)
                    .setContentTitle(getString(R.string.ClickToCancel))
                    .setContentIntent(contentIntent)
                    .build();
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
                startForeground(0, notification, ServiceInfo.FOREGROUND_SERVICE_TYPE_MEDIA_PROJECTION);
            } else {
                startForeground(0, notification);
            }
        }
    } else {
        startForeground(0, new Notification());
    }

}

并且onStartCommand是:

public int onStartCommand(Intent intent, int flags, int startId) {
    Log.i(TAG, "Service onStartCommand() is called");

    startForeground();

    mResultCode = intent.getIntExtra("code", -1);
    mResultData = intent.getParcelableExtra("data");
    mScreenWidth = intent.getIntExtra("width", 720);
    mScreenHeight = intent.getIntExtra("height", 1280);
    mScreenDensity = intent.getIntExtra("density", 1);
    isVideoSd = intent.getBooleanExtra("quality", true);
    isAudio = intent.getBooleanExtra("audio", true);


    mMediaProjection = createMediaProjection();
    mMediaRecorder = createMediaRecorder();
    mVirtualDisplay = createVirtualDisplay();
    mMediaRecorder.start();

    return Service.START_STICKY;
}

和createMediaProjection()方法为:

 private MediaProjection createMediaProjection() {
    Log.i(TAG, "Create MediaProjection");
    return ((MediaProjectionManager) Objects.requireNonNull(getSystemService(Context.MEDIA_PROJECTION_SERVICE))).
            getMediaProjection(mResultCode, mResultData);
}

[任何人都知道为什么会发生此错误,媒体投影需要在getMediaProjection()上调用ServiceInfo.FOREGROUND_SERVICE_TYPE_MEDIA_PROJECTION类型的前台服务,即使我调用...

java android foreground-service android-mediaprojection pie
1个回答
0
投票

android:foregroundServiceType="mediaProjection"添加到服务清单中的<service>元素。

© www.soinside.com 2019 - 2024. All rights reserved.