Skip to content

Commit

Permalink
fix capture screen by androidQ.
Browse files Browse the repository at this point in the history
  • Loading branch information
jinanzhuan committed Jun 9, 2020
1 parent 38794f1 commit 48f01ce
Show file tree
Hide file tree
Showing 5 changed files with 117 additions and 1 deletion.
7 changes: 7 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,9 @@

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

<!--捕获屏幕所需权限-->
<uses-permission android:name="android.permission.FOREGROUND_SERVICE"/>

<application
android:name=".DemoApplication"
android:allowBackup="true"
Expand Down Expand Up @@ -561,6 +564,10 @@
</receiver>
<!-- Mi推送配置 end-->

<!--捕获屏幕service-->
<service android:name=".conference.SRForegroundService"
android:enabled="true"
android:foregroundServiceType="mediaProjection"/>
</application>

</manifest>
Original file line number Diff line number Diff line change
Expand Up @@ -730,7 +730,15 @@ protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
if (requestCode == ScreenCaptureManager.RECORD_REQUEST_CODE) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
ScreenCaptureManager.getInstance().start(resultCode, data);
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
Intent service = new Intent(this, SRForegroundService.class);
service.putExtra("code", resultCode);
service.putExtra("data", data);
startForegroundService(service);
}else {
ScreenCaptureManager.getInstance().start(resultCode, data);
}

}
} else if (requestCode == REQUEST_CODE_INVITE) {
final String[] members = data.getStringArrayExtra("members");
Expand Down Expand Up @@ -801,6 +809,7 @@ private void exitConference() {

if (ScreenCaptureManager.getInstance().state == ScreenCaptureManager.State.RUNNING) {
ScreenCaptureManager.getInstance().stop();
stopForegroundService();
}

// Stop to watch the phone call state.
Expand All @@ -823,6 +832,16 @@ public void onError(int error, String errorMsg) {
});
}

/**
* 停止服务
*/
private void stopForegroundService() {
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
Intent service = new Intent(this, SRForegroundService.class);
stopService(service);
}
}

private void startAudioTalkingMonitor() {
EMClient.getInstance().conferenceManager().startMonitorSpeaker(300);
}
Expand Down Expand Up @@ -909,6 +928,7 @@ private void unpublish(String publishId) {
if (!TextUtils.isEmpty(conference.getPubStreamId(EMConferenceStream.StreamType.DESKTOP))
&& publishId.equals(conference.getPubStreamId(EMConferenceStream.StreamType.DESKTOP))) {
ScreenCaptureManager.getInstance().stop();
stopForegroundService();
}
}
EMClient.getInstance().conferenceManager().unpublish(publishId, new EMValueCallBack<String>() {
Expand Down Expand Up @@ -1202,6 +1222,7 @@ public void run() {
DeskShareWindow.getInstance(getApplicationContext()).dismiss();
if (ScreenCaptureManager.getInstance().state == ScreenCaptureManager.State.RUNNING) {
ScreenCaptureManager.getInstance().stop();
stopForegroundService();
}
// 退出当前界面
finish();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package com.hyphenate.chatuidemo.conference;

import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.media.projection.MediaProjection;
import android.media.projection.MediaProjectionManager;
import android.os.Build;
import android.os.IBinder;
import android.support.annotation.Nullable;
import android.support.annotation.RequiresApi;

import com.hyphenate.chatuidemo.R;
import com.superrtc.mediamanager.ScreenCaptureManager;

import java.util.Objects;

@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
public class SRForegroundService extends Service {
private int resultCode;
private Intent resultData;
private MediaProjectionManager projectionManager;
private MediaProjection mMediaProjection;

@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
//启动前置服务
createNotificationChannel();
resultCode = intent.getIntExtra("code", -1);
resultData = intent.getParcelableExtra("data");

this.projectionManager = (MediaProjectionManager)getSystemService(Context.MEDIA_PROJECTION_SERVICE);
mMediaProjection = projectionManager.getMediaProjection(resultCode, Objects.requireNonNull(resultData));

ScreenCaptureManager.getInstance().start(resultCode, resultData, mMediaProjection);
return super.onStartCommand(intent, flags, startId);
}

private void createNotificationChannel() {
Notification.Builder builder = new Notification.Builder(this.getApplicationContext()); //获取一个Notification构造器
Intent nfIntent = new Intent(this, ConferenceActivity.class); //点击后跳转的界面,可以设置跳转数据

builder.setContentIntent(PendingIntent.getActivity(this, 0, nfIntent, 0)) // 设置PendingIntent
.setLargeIcon(BitmapFactory.decodeResource(this.getResources(), R.drawable.em_logo_uidemo)) // 设置下拉列表中的图标(大图标)
//.setContentTitle("SMI InstantView") // 设置下拉列表里的标题
.setSmallIcon(R.drawable.em_logo_uidemo) // 设置状态栏内的小图标
.setContentText(getString(R.string.share_screen_ongoing)) // 设置上下文内容
.setWhen(System.currentTimeMillis()); // 设置该通知发生的时间

/*以下是对Android 8.0的适配*/
//普通notification适配
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
builder.setChannelId("share_screen_id");
}
//前台服务notification适配
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationManager notificationManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
NotificationChannel channel = new NotificationChannel("share_screen_id", "shareScreen", NotificationManager.IMPORTANCE_LOW);
notificationManager.createNotificationChannel(channel);
}

Notification notification = builder.build(); // 获取构建好的Notification
notification.defaults = Notification.DEFAULT_SOUND; //设置为默认的声音
startForeground(110, notification);

}

@Override
public void onDestroy() {
super.onDestroy();
stopForeground(true);
}
}
2 changes: 2 additions & 0 deletions app/src/main/res/values-zh/em_strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -456,4 +456,6 @@

<string name="alert_request_video">%1$s发来视频请求</string>
<string name="alert_request_voice">%1$s发来语音请求</string>

<string name="share_screen_ongoing">共享屏幕运行中...</string>
</resources>
2 changes: 2 additions & 0 deletions app/src/main/res/values/em_strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -458,4 +458,6 @@

<string name="alert_request_video">%1$s sent a video request</string>
<string name="alert_request_voice">%1$s sent a voice request</string>

<string name="share_screen_ongoing">Shared screen running...</string>
</resources>

0 comments on commit 48f01ce

Please sign in to comment.