问题描述
任何人都知道为什么会发生此错误,媒体投影需要在getMediaProjection()上使用类型为ServiceInfo.FOREGROUND_SERVICE_TYPE_MEDIA_PROJECTION的前台服务,即使我在调用startforeground()方法之后也调用了getMediaProjection()方法.
堆栈跟踪为:
进程:com.al1.screenrecorder,PID:24641java.lang.RuntimeException:无法启动Income {cmp = com.al1.screenrecorder/.service.DisplayRecorderService(有其他功能)}的服务com.al1.screenrecorder.service.DisplayRecorderService@9ef2101:java.lang.SecurityException:媒体投影需要ServiceInfo.FOREGROUND_SERVICE_TYPE_MEDIA_PROJECTION类型的前台服务在android.app.ActivityThread.handleServiceArgs(ActivityThread.java:4661)在android.app.ActivityThread.access $ 2900(ActivityThread.java:292)在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:2246)在android.os.Handler.dispatchMessage(Handler.java:107)在android.os.Looper.loop(Looper.java:213)在android.app.ActivityThread.main(ActivityThread.java:8147)在java.lang.reflect.Method.invoke(本机方法)在com.android.internal.os.RuntimeInit $ MethodAndArgsCaller.run(RuntimeInit.java:513)在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1101)由以下原因引起:java.lang.SecurityException:媒体投影需要ServiceInfo.FOREGROUND_SERVICE_TYPE_MEDIA_PROJECTION类型的前台服务在android.os.Parcel.createException(Parcel.java:2071)在android.os.Parcel.readException(Parcel.java:2039)在android.os.Parcel.readException(Parcel.java:1987)在android.media.projection.IMediaProjection $ Stub $ Proxy.start(IMediaProjection.java:231)在android.media.projection.MediaProjection.< init>(MediaProjection.java:75)在android.media.projection.MediaProjectionManager.getMediaProjection(MediaProjectionManager.java:104)在com.al1.screenrecorder.service.DisplayRecorderService.createMediaProjection(DisplayRecorderService.java:118)在com.al1.screenrecorder.service.DisplayRecorderService.onStartCommand(DisplayRecorderService.java:107)在android.app.ActivityThread.handleServiceArgs(ActivityThread.java:4641)在android.app.ActivityThread.access $ 2900(ActivityThread.java:292)在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:2246)在android.os.Handler.dispatchMessage(Handler.java:107)在android.os.Looper.loop(Looper.java:213)在android.app.ActivityThread.main(ActivityThread.java:8147)在java.lang.reflect.Method.invoke(本机方法)在com.android.internal.os.RuntimeInit $ MethodAndArgsCaller.run(RuntimeInit.java:513)在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1101)引起原因:android.os.RemoteException:远程堆栈跟踪:在com.android.server.media.projection.MediaProjectionManagerService $ MediaProjection.start(MediaProjectionManagerService.java:498)在android.media.projection.IMediaProjection $ Stub.onTransact(IMediaProjection.java:135)在android.os.Binder.execTransactInternal(Binder.java:1028)在android.os.Binder.execTransact(Binder.java:1001)
强文本私人无效startForeground(){
Intent activityIntent =新的Intent(this,MainActivity.class);activityIntent.setAction("stop");PendingIntent contentIntent = PendingIntent.getActivity(this,0,activityIntent,PendingIntent.FLAG_UPDATE_CURRENT);如果(Build.VERSION.SDK_INT> = Build.VERSION_CODES.O){字符串channelId ="001";字符串channelName ="myChannel";NotificationChannel channel =新的NotificationChannel(channelId,channelName,NotificationManager.IMPORTANCE_NONE);channel.setLightColor(Color.BLUE);channel.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE);NotificationManager管理器=(NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);if(manager!= null){manager.createNotificationChannel(channel);通知通知=新通知.生成器(getApplicationContext(),channelId).setOngoing(真).setSmallIcon(R.mipmap.ic_launcher).setCategory(Notification.CATEGORY_SERVICE).setContentTitle(getString(R.string.ClickToCancel)).setContentIntent(contentIntent).建造();如果(Build.VERSION.SDK_INT> = Build.VERSION_CODES.Q){startForeground(0,通知,ServiceInfo.FOREGROUND_SERVICE_TYPE_MEDIA_PROJECTION);} 别的 {startForeground(0,通知);}}} 别的 {startForeground(0,new Notification());}}
,onStartCommand是:
public int onStartCommand(意图意图,整数标志,整数startId){Log.i(TAG,已调用Service onStartCommand()");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();返回服务.START_STICKY;}
,并且在getMediaProjection的此方法中例外:
私人MediaProjection createMediaProjection(){Log.i(TAG,创建MediaProjection");返回((MediaProjectionManager)Objects.requireNonNull(getSystemService(Context.MEDIA_PROJECTION_SERVICE))).getMediaProjection(mResultCode,mResultData);}
向清单中的< service>
元素中添加 android:foregroundServiceType ="mediaProjection"
您的服务.
any body knows why this error occur, Media projections require a foreground service of type ServiceInfo.FOREGROUND_SERVICE_TYPE_MEDIA_PROJECTION on getMediaProjection() even i call getMediaProjection() method after calling startforeground() method..!
Stack Trace is :
Process: com.al1.screenrecorder, PID: 24641
java.lang.RuntimeException: Unable to start service com.al1.screenrecorder.service.DisplayRecorderService@9ef2101 with Intent { cmp=com.al1.screenrecorder/.service.DisplayRecorderService (has extras) }: java.lang.SecurityException: Media projections require a foreground service of type ServiceInfo.FOREGROUND_SERVICE_TYPE_MEDIA_PROJECTION
at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:4661)
at android.app.ActivityThread.access$2900(ActivityThread.java:292)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2246)
at android.os.Handler.dispatchMessage(Handler.java:107)
at android.os.Looper.loop(Looper.java:213)
at android.app.ActivityThread.main(ActivityThread.java:8147)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:513)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1101)
Caused by: java.lang.SecurityException: Media projections require a foreground service of type ServiceInfo.FOREGROUND_SERVICE_TYPE_MEDIA_PROJECTION
at android.os.Parcel.createException(Parcel.java:2071)
at android.os.Parcel.readException(Parcel.java:2039)
at android.os.Parcel.readException(Parcel.java:1987)
at android.media.projection.IMediaProjection$Stub$Proxy.start(IMediaProjection.java:231)
at android.media.projection.MediaProjection.<init>(MediaProjection.java:75)
at android.media.projection.MediaProjectionManager.getMediaProjection(MediaProjectionManager.java:104)
at com.al1.screenrecorder.service.DisplayRecorderService.createMediaProjection(DisplayRecorderService.java:118)
at com.al1.screenrecorder.service.DisplayRecorderService.onStartCommand(DisplayRecorderService.java:107)
at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:4641)
at android.app.ActivityThread.access$2900(ActivityThread.java:292)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2246)
at android.os.Handler.dispatchMessage(Handler.java:107)
at android.os.Looper.loop(Looper.java:213)
at android.app.ActivityThread.main(ActivityThread.java:8147)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:513)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1101)
Caused by: android.os.RemoteException: Remote stack trace:
at com.android.server.media.projection.MediaProjectionManagerService$MediaProjection.start(MediaProjectionManagerService.java:498)
at android.media.projection.IMediaProjection$Stub.onTransact(IMediaProjection.java:135)
at android.os.Binder.execTransactInternal(Binder.java:1028)
at android.os.Binder.execTransact(Binder.java:1001)
strong text 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());
}
}
and onStartCommand is :
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;
}
and exception is in this method on getMediaProjection:
private MediaProjection createMediaProjection() {
Log.i(TAG, "Create MediaProjection");
return ((MediaProjectionManager) Objects.requireNonNull(getSystemService(Context.MEDIA_PROJECTION_SERVICE))).
getMediaProjection(mResultCode, mResultData);
}
Add android:foregroundServiceType="mediaProjection"
to the <service>
element in the manifest for your service.
这篇关于媒体投影需要Android Pie和Q中的ServiceInfo.FOREGROUND_SERVICE TYPE_MEDIA_PROJECTION类型的前台服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!