如何创建一个应用程序服务,即使该应用程序在Xamarin(Android)中关闭,该应用程序服务也始终在后台运行?

时间:2019-03-19 06:56:17

标签: xamarin xamarin.android

我正在使用Visual Studio在Xamarin中创建一个应用程序,其中即使该应用程序已关闭,该应用程序服务也应始终在后台运行,并且该服务应间隔发送通知。 如果该应用已被系统关闭(在占用更多内存空间的情况下),则应通过在“设置”中运行服务列表下将状态显示为“ 0进程和0服务正在重启...”来重启服务。

有人可以帮助我如何通过参考示例项目来实现这一目标。

在BackgroundService.cs

[Service]
public class BackgroundService : Service
{
    const int SERVICE_RUNNING_NOTIFICATION_ID = 123;
    const string NOTIFICATION_CHANNEL_ID = "com.company.app.channel";
    public override StartCommandResult OnStartCommand(Intent intent, StartCommandFlags flags, int startId)
    {
        Log.Debug("service", "Service Started");
        // Check if device is running Android 8.0 or higher and call StartForeground() if so
        if (Build.VERSION.SdkInt >= BuildVersionCodes.O)
        {
            var notification = new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID)
                               .SetContentTitle(Resources.GetString(Resource.String.app_name))
                               .SetContentText(Resources.GetString(Resource.String.notification_text))
                               .SetSmallIcon(Resource.Drawable.notification_icon_background)
                               .SetOngoing(true)
                               .Build();

            var notificationManager =
                GetSystemService(NotificationService) as NotificationManager;

            var chan = new NotificationChannel(NOTIFICATION_CHANNEL_ID, "On-going Notification", NotificationImportance.Min);

            notificationManager.CreateNotificationChannel(chan);

            StartForeground(SERVICE_RUNNING_NOTIFICATION_ID, notification);
        }
        return StartCommandResult.NotSticky;

    }

    public override IBinder OnBind(Intent intent)
    {
        return null;
    }

    public override void OnTaskRemoved(Intent rootIntent)
    {
        Intent restartServiceIntent = new Intent(this, this.Class);
        restartServiceIntent.SetPackage(this.PackageName);
        Log.Debug("service", "Service Restarted");

        PendingIntent restartServicePendingIntent = PendingIntent.GetService(this, 1, restartServiceIntent, PendingIntentFlags.OneShot);
        AlarmManager alarmService = (AlarmManager)this.GetSystemService(Context.AlarmService);
        alarmService.Set(
            AlarmType.ElapsedRealtime,
           SystemClock.ElapsedRealtime() + 1000,
            restartServicePendingIntent);
        base.OnTaskRemoved(rootIntent);
    }

}

更新了MainActivity.cs

public class MainActivity : AppCompatActivity
{
    String alarm = Android.Content.Context.AlarmService;
    const string NOTIFICATION_CHANNEL_ID = "com.companyname.ServiceSample3";
    protected override void OnCreate(Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);
        SetContentView(Resource.Layout.abc_activity_chooser_view);

        Android.Support.V7.Widget.Toolbar toolbar = FindViewById<Android.Support.V7.Widget.Toolbar>(Resource.Id.toolbar);
        SetSupportActionBar(toolbar);

        //var alarmIntent = new Intent(this, typeof(AlarmReceiver));
        //alarmIntent.PutExtra("title", "Hello");
        //alarmIntent.PutExtra("message", "World!");

        Intent intent = new Intent(NOTIFICATION_CHANNEL_ID);
        intent.SetClass(this, typeof(AlarmReceiver));

        var pending = PendingIntent.GetBroadcast(this, 0, intent, PendingIntentFlags.UpdateCurrent);

        var alarmManager = GetSystemService(AlarmService).JavaCast<AlarmManager>();
        alarmManager.SetRepeating(AlarmType.ElapsedRealtime, DateTime.Now.Millisecond, 5 * 100, pending);


    }
}

谢谢。

1 个答案:

答案 0 :(得分:2)

请参考以下示例。您可以使用警报管理器重新启动服务。

请按照以下步骤操作。

第1步:       创建BroadcastReceiver

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;

namespace AlarmManagerApp
{
    [BroadcastReceiver(Enabled = true)]
    [IntentFilter(new[] { Android.Content.Intent.ActionBootCompleted })]
    class AlarmReceiver : BroadcastReceiver
    {
        Context context;
        public override void OnReceive(Context context, Intent intent)
        {
            this.context = context;
            Toast.MakeText(context,"Recieved",ToastLength.Long).Show();
            Intent background = new Intent(context, typeof(BackgroundService));
            context.StartService(background);

        }
    }
}

步骤2:在要调用服务的MainActivity中添加AlarmManager

namespace AlarmManagerApp
{
    [Activity(Label = "@string/app_name", Theme = "@style/AppTheme.NoActionBar", MainLauncher = true)]
    public class MainActivity : AppCompatActivity
    {
        String alarm = Context.AlarmService;
        protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);
            SetContentView(Resource.Layout.activity_main);

            Android.Support.V7.Widget.Toolbar toolbar = FindViewById<Android.Support.V7.Widget.Toolbar>(Resource.Id.toolbar);
            SetSupportActionBar(toolbar);

            var alarmIntent = new Intent(this, typeof(AlarmReceiver));
            alarmIntent.PutExtra("title", "Hello");
            alarmIntent.PutExtra("message", "World!");

            var pending = PendingIntent.GetBroadcast(this, 0, alarmIntent, PendingIntentFlags.UpdateCurrent);

            var alarmManager = GetSystemService(AlarmService).JavaCast<AlarmManager>();
            alarmManager.SetRepeating(AlarmType.ElapsedRealtime,DateTime.Now.Millisecond,  5 * 100, pending);


          }
   }

第3步:为您添加服务

namespace AlarmManagerApp
{
    [Service]
    public class BackgroundService : Service
    {
        public override void OnCreate()
        {
            base.OnCreate();

        }

        public override StartCommandResult OnStartCommand(Intent intent, StartCommandFlags flags, int startId)
        {
            Log.Debug("service","Service Started");
            //init the handler in oncreate
            System.Timers.Timer Timer1 = new System.Timers.Timer();
            Timer1.Start();
            Timer1.Interval = 3000;
            int a = 0;
            Timer1.Enabled = true;
            //Timer1.Elapsed += OnTimedEvent;
            Timer1.Elapsed += (object sender, System.Timers.ElapsedEventArgs e) =>
            {
                Timer1.Stop();
                Timer1.Start();
                a++;
               //Delete time since it will no longer be used.
                Timer1.Dispose();
            };
            Timer1.Start();

            return StartCommandResult.Sticky;
        }

        public override IBinder OnBind(Intent intent)
        {
            return null;
        }

        public override void OnTaskRemoved(Intent rootIntent)
        {
            Intent restartServiceIntent = new Intent(this, this.Class);
            restartServiceIntent.SetPackage(this.PackageName);
            Log.Debug("service", "Service Restarted");

            PendingIntent restartServicePendingIntent = PendingIntent.GetService(this, 1, restartServiceIntent, PendingIntentFlags.OneShot);
            AlarmManager alarmService = (AlarmManager)this.GetSystemService(Context.AlarmService);

            alarmService.SetRepeating(AlarmType.RtcWakeup, SystemClock.CurrentThreadTimeMillis(), 30 * 1000, restartServicePendingIntent);
            NotificationManager notificationManager =
                (NotificationManager)GetSystemService(NotificationService);

            Notification.Builder builder = new Notification.Builder(this);
            Intent notificationIntent = new Intent(this, typeof(MainActivity));
            PendingIntent contentIntent = PendingIntent.GetActivity(this, 0, notificationIntent, 0);

            //set
            builder.SetContentIntent(contentIntent);
            builder.SetSmallIcon(Resource.Mipmap.ic_launcher);
            builder.SetContentText("Contents");
            builder.SetContentTitle("title");
            builder.SetAutoCancel(true);
            builder.SetDefaults(NotificationDefaults.All);


            notificationManager.Notify(1, builder.Build());

            base.OnTaskRemoved(rootIntent);
        }
    }
}

如果应用已关闭且设备重新启动,这也将起作用。