xamarin c# android后台服务代码


自己基于xamarin做了一个有趣的小工具,具体就是截屏,然后图片识别,点击屏幕

这是自己做的后台服务(这个是截图的后台服务),安卓anroid 10开始截屏需要后台服务

 

	<application ...
		<service android:name="com.companyname.ScreenTranslatorService" android:foregroundServiceType="mediaProjection" android:enabled="true" android:exported="false" />
	</application>

 

//[Service(Enabled = true, Name = "com.companyname.ScreenTranslatorService ", ForegroundServiceType = Android.Content.PM.ForegroundService.TypeMediaProjection)]
    [Register("com.companyname.ScreenTranslatorService")]
    public class ScreenTranslatorService : Service
    {
        private static ScreenTranslatorService _instance;
        private static int ONGOING_NOTIFICATION_ID = 99690;
        private bool dismissNotify = false;
        private NotificationManager notificationManager;
        public override IBinder OnBind(Intent intent)
        {
            return null;
        }
        public override void OnCreate()
        {
            base.OnCreate();

            if (_instance == null)
            {
                _instance = this;
            }
            notificationManager = (NotificationManager)GetSystemService(Context.NotificationService);
            startForeground();
        }

        public static void start(Context context)
        {
            if(_instance == null)
                ContextCompat.StartForegroundService(context, new Intent(context, typeof(ScreenTranslatorService)));
        }
        public override void OnDestroy()
        {
            base.OnDestroy();
            stopForeground();

        }
        private Notification getForegroundNotification()
        {
            String channelId = PackageName + "_v1";

            if (Build.VERSION.SdkInt >= Android.OS.BuildVersionCodes.O)
            {
                IList<NotificationChannel> channels = notificationManager.NotificationChannels;
                foreach (NotificationChannel channel in channels)
                {
                    String id = channel.Id;
                    if (NotificationChannel.DefaultChannelId  == id)
                    {
                        continue;
                    }
                    if (id != channelId)
                    {
                        notificationManager.DeleteNotificationChannel(id);
                    }
                }
                if (notificationManager.GetNotificationChannel(channelId) == null)
                {
                    NotificationChannel channel = new NotificationChannel(channelId,
                           "Foreground notification",
                           Android.App.NotificationImportance.Low);
                    channel.SetShowBadge(false);
                    notificationManager.CreateNotificationChannel(channel);
                }
            }
            NotificationCompat.Builder builder =
                    new NotificationCompat.Builder(this, channelId);
            //builder.setColor(ContextCompat.getColor(this, R.color.appIconColor));
            //builder.setSmallIcon(R.drawable.ic_for_notify);
            //builder.setLargeIcon(BitmapFactory.decodeResource(getResources(),
            //      R.drawable.ic_launcher_shadow));
            builder.SetTicker(this.ApplicationInfo.Name);
            builder.SetContentTitle(this.ApplicationInfo.Name);
            //boolean toShow = !_isFloatingViewShowing();
            //   if (!toShow) {
            //      builder.setContentText(getString(R.string.notification_contentText_hide));
            //    } else {
            //        builder.setContentText(getString(R.string.notification_contentText_show));
            //    }
            //   Intent notificationIntent = MainActivity.getShowingStateSwitchIntent(this, toShow);
            //    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
            //     builder.setContentIntent(pendingIntent);
            builder.SetAutoCancel(false);
            return builder.Build();
        }
        public override StartCommandResult OnStartCommand(Intent? intent, [GeneratedEnum] StartCommandFlags flags, int startId)
        {
            base.OnStartCommand(intent, flags, startId);

            if (_instance == null)
            {
                _instance = this;
            }

            return StartCommandResult.Sticky;
        }
        private void startForeground()
        {
            StartForeground(ONGOING_NOTIFICATION_ID, getForegroundNotification());
        }
        private void stopForeground()
        {
           StopForeground(dismissNotify);
        }
    }