-1

When my application is in idle state i.e. no one is using the application or it is closed but a background service which is running continuously. I have issue that background service in my application is crashing. It just happened two times. It is not happening on regular basis. I don't have any stack trace on Crashlytics but only a crash report that shows the application is crashing on Runnable interface function run() (screen shot attached)

enter image description here

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        context = getApplicationContext();

        handler = new Handler();
        handler.removeCallbacks(restartThread);
        handler.post(restartThread);
        return START_STICKY;
    }

    private Runnable restartThread = new Runnable() {
        @Override
        public void run() {
            handler.postDelayed(restartThread, NOTIFY_INTERVAL);
        }
    };

Is this possible that OS is killing the background service due to low memory?

Waqas Ahmed
  • 153
  • 11

2 Answers2

0

This is just a suggestion, not a solution to your issue

Maybe its better to use JobService rather than using a Background Service. Their are so many restrictions on Background Services from Android Oreo and Above

Pavan Varma
  • 1,199
  • 1
  • 9
  • 21
  • Just btw, see my answer above. The recommended approach from Google is to use WorkManager. This wraps JobService and runs on any api level – Shmuel May 27 '18 at 22:13
0

Its very likely there are a host of problems with this implementation. Background services are blocked on modern versions of Android, and even on older versions running a service like that (with a Runnable loop) is very very error prone.

As of 2018 the best practice way to run a scheduled task is to use the WorkManager library from Android Architecture Components.

WorkManager chooses an appropriate way to schedule a background task--depending on the device API level and included dependencies, WorkManager might use JobScheduler, Firebase JobDispatcher, or AlarmManager. You don't need to write device logic to figure out what capabilities the device has and choose an appropriate API; instead, you can just hand your task off to WorkManager and let it choose the best option.

https://developer.android.com/topic/libraries/architecture/workmanager

You can define a task like this

public class CustomWorker extends Worker {
  ...
}

and then schedule it like this

new PeriodicWorkRequest.Builder photoWorkBuilder =
        new PeriodicWorkRequest.Builder(CustomWorker.class, 12,
                TimeUnit.HOURS);
// ...if you want, you can apply constraints to the builder here...

// Create the actual work object:
PeriodicWorkRequest work = customWorkerBuilder.build();
// Then enqueue the recurring task:
WorkManager.getInstance().enqueue(work);
Shmuel
  • 3,916
  • 2
  • 27
  • 45
  • Thank you so much @Shmuel for your suggestion. With this work manager can i run the code inside run() method after every second by defining it like a task? – Waqas Ahmed May 27 '18 at 20:33
  • Is the background service is not good for android versions like 4.4, 5.0, 6.0.1(API versions 19, 21, 23 etc.) ? I mean if you search any where about background tasks, the very first thing you will find is Service or Intent Service. – Waqas Ahmed May 27 '18 at 20:39
  • Background service is for running long lived tasks, such as file upload/ downloads and background music streaming. To run a periodic task the recommended approach (by Google) is to use Work Manager. If you give me more details as to specifically what you are trying to accomplish I can help more. How frequent are you trying to run this task, and what is it trying to accomplish? – Shmuel May 27 '18 at 22:11
  • well i want to make a client socket connection with the server socket. The application runs a client socket(simple socket) and look for a server socket(running somewhere else) to connect. if the client socket is connected with the server socket, then certain message passes from server to client and vice versa. the thing is that i need background service because i want to make client service continuously look for server socket if the application is open or not. – Waqas Ahmed May 28 '18 at 18:59
  • That won't work. You need to use Push Messaging (such as Firebase Cloud Messaging (FCM)) to tell that client that there is new data to sync. You can't keep a socket open. For many reasons, first background services are banned on Android O and higher. Second, you service can be killed even on older versions of android. Third, you'll drain the users battery pretty fast if you leave a network connection open. Probably other reasons also... Your question looks exactly like this one https://stackoverflow.com/questions/50552136/running-a-service-infinitely-in-android – Shmuel May 29 '18 at 00:44
  • Thanks for your answer. let me explain it to your further. I have a java application(as s desktop service) on windows machine running a server socket . I actually need to establish a connection and communication between the java desktop application and mobile application(client socket) over a specific port. It is some what like a socket communication over network. What happen is the client socket continuously listen on a particular port for server socket and when i run the desktop service a communication has establish between both sockets (between app and desktop service). – Waqas Ahmed May 29 '18 at 13:59
  • Now for the above situation tell me what should i do? and i need to do this communication process for like 3 devices(3 different desktop service and 3 different mobile apps on different mobile devices) at a time – Waqas Ahmed May 29 '18 at 15:23