我有一个运行良好多年的视频流应用程序,但我最近升级到
targetSdkVersion 28
我现在看到以下错误...
java.lang.SecurityException: Media projections require a foreground service of type ServiceInfo.FOREGROUND_SERVICE_TYPE_MEDIA_PROJECTION
at android.os.Parcel.createExceptionOrNull(Parcel.java:2373)
at android.os.Parcel.createException(Parcel.java:2357)
at android.os.Parcel.readException(Parcel.java:2340)
at android.os.Parcel.readException(Parcel.java:2282)
at android.media.projection.IMediaProjection$Stub$Proxy.start(IMediaProjection.java:231)
at android.media.projection.MediaProjection.<init>(MediaProjection.java:59)
at android.media.projection.MediaProjectionManager.getMediaProjection(MediaProjectionManager.java:119)
at screencast.StreamerService.startScreenCast(StreamerService.java:401)
at screencast.ScreencastBusinessLogic.loginToFacebook(ScreencastBusinessLogic.java:191)
at screencast.ScreencastBusinessLogic.access$200(ScreencastBusinessLogic.java:52)
at screencast.ScreencastBusinessLogic$4.onCompleted(ScreencastBusinessLogic.java:449)
at com.facebook.GraphRequest$5.run(GraphRequest.java:1398)
at android.os.Handler.handleCallback(Handler.java:938)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:223)
at android.app.ActivityThread.main(ActivityThread.java:7660)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
Caused by: android.os.RemoteException: Remote stack trace:
at com.android.server.media.projection.MediaProjectionManagerService$MediaProjection.start(MediaProjectionManagerService.java:479)
at android.media.projection.IMediaProjection$Stub.onTransact(IMediaProjection.java:135)
at android.os.Binder.execTransactInternal(Binder.java:1154)
at android.os.Binder.execTransact(Binder.java:1123)
我遵循了媒体预测中的建议,要求在 Android Pie 和 Q 中使用 ServiceInfo.FOREGROUND_SERVICE TYPE_MEDIA_PROJECTION 类型的前台服务,但没有任何运气。
我的清单包括
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
<application
...
<service
android:name="screencast.StreamerService"
android:foregroundServiceType="mediaProjection"
android:enabled="true"
android:exported="true"
tools:targetApi="q"
/>
</application>
该服务是这样调用的......
package screencast;
final public class StreamerService extends Service implements Streamer.Listener {
...
我能够成功请求权限(onActivityResult
像这样获得-1的结果代码......
public void requestMediaProjection() {
//Log.v(TAG, "requestMediaProjection");
final MediaProjectionManager manager = (MediaProjectionManager) context.getSystemService(Context.MEDIA_PROJECTION_SERVICE);
context.startActivityForResult(
manager.createScreenCaptureIntent(),
REQUEST_MEDIA_PROJECTION);
}
int resultCode;
Intent data;
public void onActivityResult(int requestCode, int resultCode, Intent data) {
this.resultCode = resultCode;
this.data = data;
if (requestCode != REQUEST_MEDIA_PROJECTION) {
return;
}
if (resultCode != Activity.RESULT_OK) {
return;
}
enterStreamDescription();
}
稍后当我调用 manager.getMediaProjection 时,我在这里遇到了崩溃...
public void startScreenCast(int resultCode,
Intent data,
int screenDensity,
List<Connection> connections,
AudioConfig audioConfig,
VideoConfig videoConfig,
Streamer.MODE mode,
File mp4,
MediaProjectionManager manager
) {
...
mMediaProjection = manager.getMediaProjection(resultCode, data);
}
非常感谢任何建议。谢谢。