如果依赖版本是Android Q之后的版本,系统录制屏幕的方法需要有所变动:
1、需要将onActivityResult中返回的resultcode和data传给service,然后在service中获取MediaProjcection
MediaProjection mediaProjection = ScreenRecorderCtrl.getInstance().getProjectionManager().getMediaProjection(resultcode, data)
2、Mainfest对应service中添加如下属性
android:enabled="true"
android:foregroundServiceType="mediaProjection"
3、Service中onStartCommand中需要设置通知,该方法要在MediaProjection初始化之前
/**
* 添加一个状态栏通知
*/
private void addNotification(){
NotificationManager manager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
if(Build.VERSION.SDK_INT >= 26)
{
//当sdk版本大于26
String id = "screenrecord";
String description = "143";
int importance = NotificationManager.IMPORTANCE_HIGH;
NotificationChannel channel = new NotificationChannel(id, description, importance);
manager.createNotificationChannel(channel);
Notification notification = new Notification.Builder(this, id)
.setCategory(Notification.CATEGORY_MESSAGE)
.setSmallIcon(R.drawable.recoding)
.setContentTitle("录屏服务 ")
.setContentText("进行中")
.setAutoCancel(false)
.build();
startForeground(1,notification);
}
else
{
Notification notification = new Notification.Builder(this)
.setContentTitle("录屏服务 ")
.setContentText("进行中")
.setSmallIcon(R.drawable.recoding)
.build();
startForeground(1, notification);
}
}
4、Service启动的时候
Intent service = new Intent(this, ScreenRecorderService.class);
service.putExtra("code", resultcode);//onactivityresult返回值
service.putExtra("data", data);//onactivityresult 返回值
if (android.os.Build.VERSION.SDK_INT <=26) {
startService(service);
}else {
startForegroundService(service);
}
