ExternalVideoInputService.java
BASH
2021.03.06 17:55
public class ExternalVideoInputService extends Service
{
    private static final int NOTIFICATION_ID = 1;
    private static final String CHANNEL_ID = "ExternalVideo";
 
    private ExternalVideoInputManager mSourceManager;
    private IExternalVideoInputService mService;
 
    @Override
    public void onCreate()
    {
        super.onCreate();
        mSourceManager = new ExternalVideoInputManager(this.getApplicationContext());
        mService = new IExternalVideoInputService.Stub()
        {
            @Override
            public boolean setExternalVideoInput(int type, Intent intent) {
                return mSourceManager.setExternalVideoInput(type, intent);
            }
        };
    }
 
    @Nullable
    @Override
    public IBinder onBind(Intent intent)
    {
        startForeground();
        startSourceManager();
        return mService.asBinder();
    }
 
    private void startForeground()
    {
        createNotificationChannel();
 
        Intent notificationIntent = new Intent(getApplicationContext(),
                getApplicationContext().getClass());
        PendingIntent pendingIntent = PendingIntent.getActivity(
                this, 0, notificationIntent, 0);
 
        Notification.Builder builder = new Notification.Builder(this)
                .setContentTitle(CHANNEL_ID)
                .setContentIntent(pendingIntent);
 
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
        {
            createNotificationChannel();
            builder.setChannelId(CHANNEL_ID);
        }
 
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q)
        {
            startForeground(NOTIFICATION_ID, builder.build(),
                    ServiceInfo.FOREGROUND_SERVICE_TYPE_MEDIA_PROJECTION);
        }
        else
        {
            startForeground(NOTIFICATION_ID, builder.build());
        }
    }