6

I'm having a nightmare trying to create a simple background service on android that runs permanently.

This service will be doing some background tasks like pool users social media and show notification so it requires only one user interaction (login) after that should be able to run forever until the end of the days....

but it is not happening

this is my manifest:

<service
        android:name=".service.MyService"
        android:description="@string/serviceDescription"
        android:directBootAware="false"
        android:enabled="true"
        android:exported="false"
        android:icon="@drawable/ic_logo"
        android:label="@string/serviceLabel"/>

I start service on Application onCreate()

    if (MyService.getInstance() == null) {
        Intent intent = new Intent(this, MyService.class);
        startService(intent);
    }

service:

public void onCreate() {
        super.onCreate();
        this.workDone = 0;

        this.workerMap = new HashMap<User.UserData, Worker>(3);
        this.listeners = new HashMap<Worker, Worker.WorkerListener>(3);
        this.usernames = new HashMap<APIFacade, List<String>>(3);

        this.threadPool = Executors.newFixedThreadPool(3);
        INSTANCE = this;

    }

ABSOLUTELLY NOWHERE IN MY CODE I CALL STOPSERVICE NOR STOPSELF

so why i'm getting nullpointerexception in some random situations?

someone even told me to do the bellow code to force android to "restart" service

  @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        return START_STICKY;
    }

can someone please help me... i'm getting really stressed with this problem

my app has over 1000 daily users and around 10% of them "find a way" to make nullpointerexception when using the service

Rafael Lima
  • 3,079
  • 3
  • 41
  • 105
  • 1
    Possible duplicate of [Android - implementing startForeground for a service?](https://stackoverflow.com/questions/6397754/android-implementing-startforeground-for-a-service) – TheWanderer Dec 13 '18 at 21:10
  • Don't create a permanent service that keeps polling. Use push notifications. Saves CPU cycles, data traffic, battery etc. Better experience for everyone. – J.A.P Dec 13 '18 at 21:33
  • Possible duplicate of [Permanent services on android oreo](https://stackoverflow.com/questions/47270691/permanent-services-on-android-oreo) – nandsito Dec 13 '18 at 21:40
  • @J.A.P, if the api does allow me to push i would... since its third party api and i cant change how backend behaves i have to poll – Rafael Lima Dec 13 '18 at 21:44
  • @RafaelLima I know, but you can have your own server handle the notifications. I.e middle-hand. – J.A.P Dec 13 '18 at 21:51
  • I dont get why people like to downvote... is a autentic question, is well writen and as it was correclty pointed out. is a feature of newer versions of android Nobody knows of every aspect of all android versions, and until some people pointed out i didn't notice that the errors come from 8.0 < version – Rafael Lima Dec 13 '18 at 22:05

2 Answers2

5

A lot of the internals of background services has changed in Oreo (or was it Nougat?). I'd get familiar with the Android documentation:

https://developer.android.com/about/versions/oreo/background

and

https://developer.android.com/training/best-background

You might want to consider the JobScheduler framework.

Cody Caughlan
  • 32,456
  • 5
  • 63
  • 68
  • it says: "A foreground service performs some operation that is noticeable to the user. ... Foreground services must display a Notification. " but it doesn't say how to create them... do i need to put something in my intent? manifest? or just the fack that my app has a visible notification makes all my services "foreground" ??? – Rafael Lima Dec 13 '18 at 21:38
  • 1
    I've one last question... do i need to do `startForeground()` in the `onCreate()` of my service or can i do somewhere else in the code – Rafael Lima Dec 13 '18 at 22:19
1

The answer is: You can't! Current Android 100% blocks developers from doing permanent services.
It used to be somewhat possible (but not totally reliable) to do it up to KitKat.

The closest you can get from a permanent service is to use a Foreground Service using the startForeground() method from inside the service and startServiceForeground() method (on API 26 and up) when starting the service.
But even then, the service will still get killed at some point.

The alternatives are:

  • Use the WorkManager to do "once in a while" tasks (sound like a viable option from what you describe)
  • User AlarmManager to do task at specific time
  • User FCM (push messages) to do tasks executed when server tells to.
Budius
  • 39,391
  • 16
  • 102
  • 144