targetSdkVersion 29 时出现 getMediaProjection 错误 [英] Error getMediaProjection when targetSdkVersion 29
本文介绍了targetSdkVersion 29 时出现 getMediaProjection 错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在构建一个屏幕录制应用程序.但它只适用于targetSdkVersion 28,当传递google请求的targetSdkVersion 29时,错误
I am building a screen recording application. But it only works on targetSdkVersion 28, when passing targetSdkVersion 29 requested by google, the error
代码
@SuppressLint("MissingSuperCall")
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode != REQUEST_CODE) {
Toast.makeText(this, "Error!", Toast.LENGTH_SHORT).show();
return;
}
if (resultCode != RESULT_OK) {
Toast.makeText(this,
"Screen Cast Permission Denied", Toast.LENGTH_SHORT).show();
return;
}
mMediaProjectionCallback = new MediaProjectionCallback();
mMediaProjection = mProjectionManager.getMediaProjection(resultCode, data);
mMediaProjection.registerCallback(mMediaProjectionCallback, null);
mVirtualDisplay = createVirtualDisplay();
mMediaRecorder.start();
}
错误被抛出
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.megamestudio.screen_record_and_booster, PID: 21862
java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1000, result=-1, data=Intent { (has extras) }} to activity {com.megamestudio.screen_record_and_booster/com.megamestudio.screen_record_and_booster.MainActivity}: java.lang.SecurityException: Media projections require a foreground service of type ServiceInfo.FOREGROUND_SERVICE_TYPE_MEDIA_PROJECTION
at android.app.ActivityThread.deliverResults(ActivityThread.java:4927)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:4968)
at android.app.servertransaction.ActivityResultItem.execute(ActivityResultItem.java:51)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2043)
at android.os.Handler.dispatchMessage(Handler.java:107)
at android.os.Looper.loop(Looper.java:224)
at android.app.ActivityThread.main(ActivityThread.java:7548)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:539)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:950)
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:2074)
at android.os.Parcel.readException(Parcel.java:2042)
at android.os.Parcel.readException(Parcel.java:1990)
at android.media.projection.IMediaProjection$Stub$Proxy.start(IMediaProjection.java:231)
at android.media.projection.MediaProjection.<init>(MediaProjection.java:58)
at android.media.projection.MediaProjectionManager.getMediaProjection(MediaProjectionManager.java:104)
at com.megamestudio.screen_record_and_booster.MainActivity.onActivityResult(MainActivity.java:426)
请帮帮我,谢谢
推荐答案
这是经过一段时间研究后的解决方案.首先,请遵循其他答案.
This is the solution after some time of research. First of all, follow the other answers.
- 清单权限
- 在前台启动服务
这是我的答案
- 在服务onStartCommand()上,我为android Q实现了这个方法.(小于Q,没有这个代码也能工作):startForeground(id, notification, serviceType)
public int onStartCommand(Intent intent, int flags, int startId) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
Notification notification = ScreenRecorderHelper.GetMediaProjectionNotification(AppShared.gContext);
startForeground(startId,
notification,
ServiceInfo.FOREGROUND_SERVICE_TYPE_MEDIA_PROJECTION);
}
handleCommand(intent);
// We want this service to continue running until it is explicitly
// stopped, so return sticky.
return START_STICKY;
}
我希望这能帮助很多像我一样的人......
I hope this helps many others like me...
这篇关于targetSdkVersion 29 时出现 getMediaProjection 错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文