是在使用系统录屏服务时在29版本以上需要开启一个前台通知
在开启录屏之前,在service里调用如下代码,总之在getMediaProjection之前调用就行
public void createNotification() {
if (Build.VERSION.SDK_INT >= 26) {
Intent notificationIntent = new Intent(this, FloatWindowsService.class);
PendingIntent pendingIntent;
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.S) {
pendingIntent = PendingIntent.getActivity(this, 123, notificationIntent, PendingIntent.FLAG_IMMUTABLE);
} else {
pendingIntent = PendingIntent.getActivity(this, 123, notificationIntent, PendingIntent.FLAG_ONE_SHOT);
}
Notification.Builder notificationBuilder = new Notification.Builder(this, NOTIFICATION_CHANNEL_ID)
.setLargeIcon(ImageUtils.getBitmap(R.mipmap.ic_launcher))
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle("化无助手")
.setContentText("持续运行以识别屏幕内容")
.setTicker("test")
.setContentIntent(pendingIntent);
Notification notification = notificationBuilder.build();
NotificationChannel channel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, NOTIFICATION_CHANNEL_NAME, NotificationManager.IMPORTANCE_DEFAULT);
channel.setDescription(NOTIFICATION_CHANNEL_DESC);
NotificationManager notificationManager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.createNotificationChannel(channel);
this.startForeground(NOTIFICATION_ID, notification);
}
}