Running service on android 9.0 or higher continuously-ThrowExceptions

Exception or error:

My app is about backing up SMS/MMS messages.

So far my idea was to back up SMS/MMS to an XML file instantly when they arrive or are sent,
then periodically upload created XML file to a cloud storage, even if app is not running.

This approach is the only one I found that is reliable enough for me to not lose any conversations.

Functions to do all that are mostly complete but I have noticed that google does not allow service to be running continuously on a device.

So, my question is, is there a way to make service run in background continuously without getting killed after a while. I am not interested in foreground services since i do not wish my users to have a notification displayed at all times.

This is my first app basically and I am not even sure if my approach to back-up software is correct.

I would love to have some input how should I tackle this problem. All I care at this moment is that my app will back-up every conversation instantly on send or receive.

How to solve:

you can use android.permission.BIND_NOTIFICATION_LISTENER_SERVICE permission in your service
but you have to ask user for permission and also android.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS permission for “doze-mode” then use a Thread in the service for your BroadCastReceiver. this way guaranteed to make your service always running but it will cost battery life . i have tried and tested this for my app from api-level 19 to 28 and never stopped even in some devices like nokia with android O background running service disabled but with this trick you can keep it running. apps like Gameboosters or Cleaners also use this trick to keep there service alwyase running.


   <uses-permission android:name="android.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS" />
            <action android:name="android.service.notification.NotificationListenerService" />


@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR2)
public class NLService extends NotificationListenerService {

    private static final String TAG = NLService.class.getSimpleName();

    public void onCreate() {
        new Thread(new Runnable() {
            public void run() {
                while (true)
                    try {
                    } catch (Exception ignore) {


    public void onDestroy() {

    public void onNotificationPosted(StatusBarNotification sbn) {


    public void onNotificationRemoved(StatusBarNotification sbn) {


Request Permission


public void askForNotificationServicePermission() {

        context.startActivity(new Intent(ACTION_NOTIFICATION_LISTENER_SETTINGS));


public void askForDozeModePermission() {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        Intent intent = new Intent();
        String packageName = context.getPackageName();
            intent.setData(Uri.parse("package:" + packageName));


doSomeThing(this); is some method that you want to run for example your broudcastreceiver.

Leave a Reply

Your email address will not be published. Required fields are marked *