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

问题描述 投票:-1回答:1

我正在使用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);


    }
}

提前致谢。

xamarin xamarin.android
1个回答
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);
        }
    }
}

如果关闭应用程序并重启设备,这也适用。

© www.soinside.com 2019 - 2024. All rights reserved.