1


I develop a StepCounter application with Xamarin.Android.

What do I have for now:

1) In MainActivity in OnCreate() I call StartService(new Intent(this, typeof(MyService)));
Then in OnStart() I call BindService(serviceIntent, MyServiceConnection, Bind.AutoCreate);

2) That gives me a nice service which is 1) foreground 2) both StartService and BindService 3) returns StartCommandResult.Sticky in OnStartCommand()

3) In MyService I register MainActivity as a listener for every detected step. I use Interface as suggested here and the UI in MainActivity updates in real time, everything works perfect.

But there is a problem.
When I swipe MainActivity from recent tasks my app supposed to shut down/finish/going off and so it does.
But MyService is STICKY so it starts again and the user can see a notification in status bar. That's exactly what I want. Sticky undying service.
And than first scenario: user presses the notification -> that creates an activity -> OnStart() provides binding to MyService again -> works ok.
Second scenario: user does not press the notification -> no activity has been created (it's the only service) -> user makes some steps -> application fails.

I don't know how to handle this second kind of behavior. Maybe that is because I register MainActivity as a listener? And if I destroy it there is nothing to update My service? In OnStop() I call UnbindService(MyService); and I expect that MyService can work even without activity. But obviously, I do something wrong.

Any help is appreciated.

UPD: Find the solution, see my answer below.

  • Hi, do you want to create a service which will not die even though the app has been killed? – Robbit Jan 30 '18 at 09:33
  • Joe, hi! Well, I've already have it, my service is undestroyable. But when my activity is removed, service still works and it supposed to update the activity which had been destroyed. I was told to check listener for 'null' and I think that would help me. –  Jan 30 '18 at 10:23
  • So, have you solved your problem? – Robbit Jan 30 '18 at 10:24
  • I'll update my question after testing the idea about listener and null. –  Jan 30 '18 at 10:26

2 Answers2

1

Thanks for help everyone!
The solution was quite simple. In MyService I register MainActivity as a listener using interface. So it's not surprising that destroying activity causes failure - there is nothing to update anymore. Solution is check for null before calling this.listener.UpdateUI();

  • Hi, what is your minSdkVersion and targetSdkVersion? Because the STICKY not work on my phone, I need use other way to keep the service alive. And what phone are you using? Thanks! – Robbit Jan 31 '18 at 01:44
  • Hi, Joe! My phone is Digma VOX S502F, minSdkVersion is Android 5.0 and targetSdkVersion is latest Android 8.0 Oreo. Speaking about STICKY. I looked through [link](https://github.com/MikeCodesDotNet/My-StepCounter) StepCounter by James Montemagno and see what is written in his Service: `Workaround as on Android 4.4.2 START_STICKY has currently no effect -> restart service every 60 mins alarmManager.Set(AlarmType.Rtc, Java.Lang.JavaSystem .CurrentTimeMillis() + 1000 * 60 * 60, stepIntent);` –  Jan 31 '18 at 08:36
  • My service is foreground also, maybe that is why STICKY does work. –  Jan 31 '18 at 08:54
  • Ok, I think it is the device's problem, START_STICKY can't work on my phone. – Robbit Jan 31 '18 at 08:56
  • Well people who are testing my application say it doesn't restarts at all, but on my phone it does, so the device itself can cause some problems, I think. –  Jan 31 '18 at 09:00
  • That's why I ask you "do you want to create a service which will not die even though the app has been killed?".I have tested it, in some devices, START_STICKY won't work, because the manufacturer banned it. – Robbit Jan 31 '18 at 09:05
  • Android is fragmented, so sometimes you have to integrate a lot of things to accomplish one thing. – Robbit Jan 31 '18 at 09:07
  • True, and now I have another problem in my app which is about memory leak while step counting. And that causes the restart of my service. Hope, I'll can fix it. –  Jan 31 '18 at 09:11
0

I understood your situation and somehow also got the idea that you want to achieve.

In the service class the return type you must be using is START_STICKY and I suggest you to use START_NOT_STICKY.

Both have their own significance and use. Further how to use the return type you can go through this answer

Pranay Soni
  • 403
  • 3
  • 12
  • Pranay, thanks for your comment, but that's not what I need. You see, STICKY is exactly what I want, cause if somehow my app would be killed I want it started again and continued to count steps. But I'll read your link more thoroughly, may be I'll find the answer. –  Jan 29 '18 at 10:05