Skip to content Skip to sidebar Skip to footer

Android Service Not Running In Background?

The service runs when I press home button or back button but the service is not running in background when app is closed. Moreover the service runs in background in some(LG Nexus 5

Solution 1:

You should use a foreground service if you want keep alive your service.

To make it you must run the service in foreground

privatevoidrunForeground(){
//... Pending intent if you want attach it to the notification


Notification notification=new NotificationCompat.Builder(this)
                            .setSmallIcon(R.drawable.ic_launcher)
                            .setContentText(getString(R.string.string))
                            .setContentIntent(pendingIntent).build();

startForeground(NOTIFICATION_ID, notification);

}

NOTIFICATION_ID is a number for identify it, return sticky the service.

Solution 2:

I was running the background task in a separate thread, while it should be in the main thread, that was the reason app was not running in background in some Phones.

classTimeDisplayTimerTaskextendsTimerTask {
@Overridepublicvoidrun() {       
            // Send message in backgroundsendSMS(number,msg);
             }
}

Post a Comment for "Android Service Not Running In Background?"