Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
[Android]remove duplicate foreground notification of screen sharing.
  • Loading branch information
xgfd3 committed Nov 3, 2023
commit e0e0f3e1dab7a289d5d165ff668730a408b73862
10 changes: 0 additions & 10 deletions Android/APIExample/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -46,16 +46,6 @@
</intent-filter>
</activity>

<service android:name=".examples.advanced.SwitchCameraScreenShare$MediaProjectFgService"
android:enabled="true"
android:exported="false"
android:foregroundServiceType="mediaProjection" />

<service android:name=".examples.advanced.ScreenSharing$MediaProjectFgService"
android:enabled="true"
android:exported="false"
android:foregroundServiceType="mediaProjection" />

<activity
android:name=".SettingActivity"
android:label="@string/setting"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,8 @@
import static io.agora.rtc2.video.VideoEncoderConfiguration.STANDARD_BITRATE;
import static io.agora.rtc2.video.VideoEncoderConfiguration.VD_640x360;

import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.os.IBinder;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.LayoutInflater;
Expand All @@ -34,8 +25,6 @@

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;
import androidx.core.app.NotificationCompat;

import com.yanzhenjie.permission.AndPermission;
import com.yanzhenjie.permission.runtime.Permission;
Expand Down Expand Up @@ -83,7 +72,6 @@ public class ScreenSharing extends BaseFragment implements View.OnClickListener,
private RtcEngineEx engine;
private final ScreenCaptureParameters screenCaptureParameters = new ScreenCaptureParameters();

private Intent fgServiceIntent;
private Spinner screenScenarioType;

@Nullable
Expand Down Expand Up @@ -122,9 +110,6 @@ public void onActivityCreated(@Nullable Bundle savedInstanceState) {
if (context == null) {
return;
}
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
fgServiceIntent = new Intent(getActivity(), ScreenSharing.MediaProjectFgService.class);
}
try {
RtcEngineConfig config = new RtcEngineConfig();
/**
Expand Down Expand Up @@ -176,9 +161,6 @@ public void onActivityCreated(@Nullable Bundle savedInstanceState) {

@Override
public void onDestroy() {
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
getActivity().stopService(fgServiceIntent);
}
/**leaveChannel and Destroy the RtcEngine instance*/
if (engine != null) {
engine.leaveChannel();
Expand Down Expand Up @@ -285,10 +267,6 @@ private void joinChannel(String channelId) {
/**Set up to play remote sound with receiver*/
engine.setDefaultAudioRoutetoSpeakerphone(true);

if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
getActivity().startForegroundService(fgServiceIntent);
}

DisplayMetrics metrics = new DisplayMetrics();
getActivity().getWindowManager().getDefaultDisplay().getRealMetrics(metrics);
screenCaptureParameters.captureVideo = true;
Expand Down Expand Up @@ -490,9 +468,6 @@ private void leaveChannel() {
fl_remote.removeAllViews();
remoteUid = myUid = -1;

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
getActivity().stopService(fgServiceIntent);
}
engine.leaveChannel();
engine.stopScreenCapture();
engine.stopPreview();
Expand Down Expand Up @@ -531,58 +506,4 @@ public void onNothingSelected(AdapterView<?> parent) {

}

public static class MediaProjectFgService extends Service {
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}

@Override
public void onCreate() {
super.onCreate();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
createNotificationChannel();
}
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
return START_NOT_STICKY;
}

@Override
public void onDestroy() {
super.onDestroy();
stopForeground(true);
}

@RequiresApi(api = Build.VERSION_CODES.O)
private void createNotificationChannel() {
CharSequence name = getString(R.string.app_name);
String description = "Notice that we are trying to capture the screen!!";
int importance = NotificationManager.IMPORTANCE_HIGH;
String channelId = "agora_channel_mediaproject";
NotificationChannel channel = new NotificationChannel(channelId, name, importance);
channel.setDescription(description);
channel.enableLights(true);
channel.setLightColor(Color.RED);
channel.enableVibration(true);
channel.setVibrationPattern(
new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});
NotificationManager notificationManager = (NotificationManager)
getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.createNotificationChannel(channel);
int notifyId = 1;
// Create a notification and set the notification channel.
Notification notification = new NotificationCompat.Builder(this, channelId)
.setContentText(name + "正在录制屏幕内容...")
.setSmallIcon(R.mipmap.ic_launcher)
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher))
.setChannelId(channelId)
.setWhen(System.currentTimeMillis())
.build();
startForeground(notifyId, notification);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,9 @@
import static io.agora.rtc2.video.VideoEncoderConfiguration.STANDARD_BITRATE;
import static io.agora.rtc2.video.VideoEncoderConfiguration.VD_640x360;

import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.os.IBinder;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.LayoutInflater;
Expand All @@ -31,8 +23,6 @@

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;
import androidx.core.app.NotificationCompat;

import com.yanzhenjie.permission.AndPermission;
import com.yanzhenjie.permission.runtime.Permission;
Expand Down Expand Up @@ -78,7 +68,6 @@ public class SwitchCameraScreenShare extends BaseFragment implements View.OnClic
private boolean joined = false;
private int curRenderMode = RENDER_MODE_HIDDEN;
private ChannelMediaOptions options = new ChannelMediaOptions();
private Intent fgServiceIntent;
private RtcEngineEx engine;
private RtcConnection rtcConnection2 = new RtcConnection();

Expand Down Expand Up @@ -114,9 +103,6 @@ public void onActivityCreated(@Nullable Bundle savedInstanceState) {
if (context == null) {
return;
}
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
fgServiceIntent = new Intent(getActivity(), MediaProjectFgService.class);
}
try {
RtcEngineConfig config = new RtcEngineConfig();
/**
Expand Down Expand Up @@ -168,9 +154,6 @@ public void onActivityCreated(@Nullable Bundle savedInstanceState) {

@Override
public void onDestroy() {
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
getActivity().stopService(fgServiceIntent);
}
/**leaveChannel and Destroy the RtcEngine instance*/
if (engine != null) {
if(camera.isChecked()){
Expand All @@ -190,9 +173,6 @@ public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
if (compoundButton.getId() == R.id.screenShare) {
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.LOLLIPOP) {
if(b){
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
getActivity().startForegroundService(fgServiceIntent);
}
DisplayMetrics metrics = new DisplayMetrics();
getActivity().getWindowManager().getDefaultDisplay().getRealMetrics(metrics);
ScreenCaptureParameters parameters = new ScreenCaptureParameters();
Expand All @@ -211,9 +191,6 @@ public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
else{
// stop screen capture and update options
engine.stopScreenCapture();
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
getActivity().stopService(fgServiceIntent);
}
options.publishScreenCaptureVideo = false;
engine.updateChannelMediaOptions(options);
}
Expand Down Expand Up @@ -530,58 +507,4 @@ public void onUserOffline(int uid, int reason) {
}
};

public static class MediaProjectFgService extends Service {
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}

@Override
public void onCreate() {
super.onCreate();
if (Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.Q) {
createNotificationChannel();
}
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
return START_NOT_STICKY;
}

@Override
public void onDestroy() {
super.onDestroy();
stopForeground(true);
}

@RequiresApi(api = Build.VERSION_CODES.O)
private void createNotificationChannel() {
CharSequence name = getString(R.string.app_name);
String description = "Notice that we are trying to capture the screen!!";
int importance = NotificationManager.IMPORTANCE_HIGH;
String channelId = "agora_channel_mediaproject";
NotificationChannel channel = new NotificationChannel(channelId, name, importance);
channel.setDescription(description);
channel.enableLights(true);
channel.setLightColor(Color.RED);
channel.enableVibration(true);
channel.setVibrationPattern(
new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});
NotificationManager notificationManager = (NotificationManager)
getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.createNotificationChannel(channel);
int notifyId = 1;
// Create a notification and set the notification channel.
Notification notification = new NotificationCompat.Builder(this, channelId)
.setContentText(name + "正在录制屏幕内容...")
.setSmallIcon(R.mipmap.ic_launcher)
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher))
.setChannelId(channelId)
.setWhen(System.currentTimeMillis())
.build();
startForeground(notifyId, notification);
}
}
}