Android service separate from application

HI all,
I would like to create Android service that will be invoked by app but run separately from the app.
Also I would like it to survive restart and wake up on power plug on/ off.

I saw some hints that it could be done with AlarmManager (waking up in regular intervals), but some hints would be appreciated.

Can someone help with very base setup.

Thanx !

Posts

  • DinoNovak.3412DinoNovak.3412 USMember ✭✭
    edited April 2016

    HI,

    I managed to resolve this issue.

    So here is quick example service code with logging to file:
    `
    using System;
    using System.Threading.Tasks;
    using System.Linq;
    using Android.App;
    using Android.Util;
    using Android.Widget;
    using System.IO;

    namespace smBeaconPlay.Droid
    {
    [Service(Exported = true)]
    public class DiNoService : Service
    {

        public override void OnCreate()
        {
            base.OnCreate();
    
            Toast.MakeText(this, "Notifications Service - Created", ToastLength.Long).Show();
    
        }
    
        public override StartCommandResult OnStartCommand(Android.Content.Intent intent, StartCommandFlags flags, int startId)
        {
            Toast.MakeText(this, "Notifications Service - Started", ToastLength.Long).Show();
            RunUpdateLoop();
            return StartCommandResult.Sticky;
        }
    
    
        public override Android.OS.IBinder OnBind(Android.Content.Intent intent)
        {
            Toast.MakeText(this, "Notifications Service - Binded", ToastLength.Long).Show();
            return null;
        }
    
    
        public override void OnDestroy()
        {
            //Debug.WriteLine("Notifications Service destroyed");
            Toast.MakeText(this, "Notifications Service destroyed", ToastLength.Long).Show();
            base.OnDestroy();
        }
    
    
        private async void RunUpdateLoop()
        {
            int count = 1;
            while (true)
            {
                await Task.Delay(60000);
                count ++;
                String strMsg = count.ToString() + " ticks!";
    
                Toast.MakeText(this, strMsg, ToastLength.Long).Show();
                WriteToLocFile ("DiNoService ticked");
            }
        }
    
    
        private void WriteToLocFile(String eventString)
        {
            var path = Android.OS.Environment.ExternalStorageDirectory + Java.IO.File.Separator + "Download";
            string filename = Path.Combine(path, "myfile.txt");
    
            if (!Directory.Exists(path))
            {
                Directory.CreateDirectory(path);
            }
            using (var streamWriter = new StreamWriter(filename, true))
            {
                String fLine = DateTime.Now.ToString () + ": " + eventString;
                streamWriter.WriteLine(fLine);
                streamWriter.Close ();
            }
    
        }
    }
    

    }
    In the activity you need to have start/stop functions:
    public static void StartDiNoService()
    {
    AppContext.StartService(new Intent(AppContext, typeof(DiNoService)));

            if (Android.OS.Build.VERSION.SdkInt >= Android.OS.BuildVersionCodes.Kitkat)
            {
                PendingIntent pintent = PendingIntent.GetService(AppContext, 0, new Intent(AppContext, typeof(DiNoService)), 0);
                AlarmManager alarm = (AlarmManager)AppContext.GetSystemService(Context.AlarmService);
                alarm.Cancel(pintent);
            }
        }
    
        public static void StopDiNoService()
        {
            AppContext.StopService(new Intent(AppContext, typeof(DiNoService)));
    
            PendingIntent pintent = PendingIntent.GetService(AppContext, 0, new Intent(AppContext, typeof(DiNoService)), 0);
            AlarmManager alarm = (AlarmManager)AppContext.GetSystemService(Context.AlarmService);
            alarm.Cancel(pintent);
        }
    

    `
    and here is is simple just to call out StartDiNoService(); to start if from OnCreate()

    Just for someone to have a very simple example as it took me a while to understand it.

Sign In or Register to comment.