3

I am totally new in Xamarin. I am trying to run the service when the application gets closed. But as I closed the app from the recent item service getting stop and getting message projectname.android has stopped. I want service should run as in whatsapp after closed the app then also service run. Anyone know the answer please help.

Here is my service class Code. This code sent the notification each 5 sec. using local notification plugin library. but this code running fine in android 5 version

class AndroidService : Service
{
    private Timer _check_Timer_Data;

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

    [return: GeneratedEnum]
    public override StartCommandResult OnStartCommand(Intent intent, [GeneratedEnum] StartCommandFlags flags, int startId)
    {
        base.OnStartCommand(intent, flags, startId);

        var t = new Thread(() =>
        {
            _check_Timer_Data = new Timer((o) =>
            {
                Random rdno = new Random();
                int id = rdno.Next(1000);
                string dt = DateTime.Now.ToString("h:mm:ss tt");
                CrossLocalNotifications.Current.Show("G7CR", "G7CR Notification-" + dt, id);
            }, null, 0, 5000);
        }
         );
        t.Start();

        return StartCommandResult.Sticky;
    }

    public override void OnDestroy()
    {

        base.OnDestroy();
        _check_Timer_Data.Dispose();

    }
    public override void OnCreate()
    {
        base.OnCreate();
    }


}
Brandon Minnick
  • 13,342
  • 15
  • 65
  • 123
mohd ashif
  • 41
  • 2
  • 1
    You probably need a [Job](https://developer.android.com/reference/android/app/job/JobScheduler) instead for O+ check this out https://stackoverflow.com/a/28914451/7462031 – FreakyAli Mar 14 '19 at 15:29
  • Hi @G.hakim, I am looking code in xamarin android – mohd ashif Mar 15 '19 at 06:20
  • If you would have bothered reading the whole answer you would find this https://github.com/Dooks123/Android-Job-XamarinBinding – FreakyAli Mar 15 '19 at 06:39
  • Hi @G.hakim, I am new in xamarin and never have been written code in android too. I belong to only give some more hint to achieve the solution – mohd ashif Mar 15 '19 at 12:45
  • Everything is there in the github description my friend read!!! – FreakyAli Mar 15 '19 at 13:45

0 Answers0