当前位置: 首页 > 知识库问答 >
问题:

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

上官鸿朗
2023-03-14
[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);
    }

}

Updtaed 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个答案

陶博涉
2023-03-14

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

请按照步骤操作。

步骤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);

        }
    }
}
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);


          }
   }
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);
        }
    }
}
 类似资料:
  • 我想在android应用程序中做后台服务,即使当应用程序处于关闭、杀死或后台状态时,它也应该始终保持运行,我目前有后台服务,当应用程序最小化时,它工作得很好,但当我关闭应用程序时,杀死应用程序时,它不工作。如果应用程序死亡,套接字是关闭的,即使我使用了android隔离进程true... 我想让套接字在任何情况下都可用

  • 我想让我的Flutter应用程序总是在后台运行。在android中,我们必须创建一个始终在后台运行的服务。我在Flutter文档中找不到关于服务的东西。 有可能用Flutter做这种事情吗?

  • 我一直在使用Phonegap开发一个Android应用程序,现在我想让它在应用程序关闭时仍然可以在应用程序中执行java/js代码。所以我知道我需要创建一个服务。如果我在phonegap上创建服务插件,我还能执行javascript代码还是只执行java? 有人做过这样的事情吗?我发现了这个讨论,但似乎没有用:http://groups.google.com/group/phonegap/brow

  • 问题内容: 我正在尝试在后台运行服务。我的应用程序要做的是,当用户选中复选框,然后服务启动,而未选中时,服务停止。哪个工作正常。但是问题是,当我从任务管理器中关闭应用程序时,它也停止了服务。我想要的是即使从任务管理器关闭后也要保持服务运行。然后,停止该服务的唯一方法是由用户自己打开框。 我该如何实现? 这是我的代码 我的主要活动 我的服务等级 更新资料 我们如何使该服务像gtalk服务一样运行?

  • 问题内容: 编辑:我现在确定问题与保存所有其他命令的循环有关, 因为我已将其注释掉,并且在部署应用程序时没有附加的异常。我不确定它有多重要,但是我的实现看起来像这样: 因此,现在该线程在部署应用程序时运行,但是由于注释了循环,因此它没有实际意义。 当我的应用程序加载时,我需要在后台运行一个线程,并不断(无超时)检查某个对象队列。当然,一旦有了对象,它就会“照顾它们”,然后继续检查队列。 目前,我正

  • 我正在编写一个带有ExecutorService的单例类的SDK。它看起来像这样: 此SDK类用于在整个应用程序中运行任务/可运行程序,doSomething()函数用于在单个线程中排队并运行所有可运行程序。 但有一件事我搞不清楚,那就是什么时候给ExecutorService打电话。shutdown()方法。如果我这样称呼它: 它会破坏使用一个Thread的目的,因为如果在第二次调用doThin