0

I am trying to explore the world of Services in Android and I just wrote a small example where the activity has a button that triggers a notification.

The steps I do are:

  • Creating Intent intent = new Intent(context, MyService.class);
  • Inserting this in a PendingIntent and sending it to the NotificationManager

Then, once I click on the notification, the service (MyService) is started and launches a music soundtrack.

Using logging I saw that the click of the button and the actions of MyService both happen on the main thread, and I would like to know how can I make the service run in a separate background thread

p.s. MyService is extending Service and not IntentService because this last one terminates once executed

kioli
  • 635
  • 1
  • 10
  • 26

2 Answers2

0

onStartCommand() is called on the main thread in your Service. If you want to launch a background Thread to do the work, you can just create your own Thread and start it inside onStartCommand(). Something like this:

Thread t = new Thread(new Runnable() {
    public void run() { 
        // Put whatever code you want to run in background thread here
    }
}).start();

Make sure that you have code that will shutdown running background threads if necessary when your Service needs to stop.

David Wasser
  • 93,459
  • 16
  • 209
  • 274
  • Thank you David, I will try that :) I thought that it was the whole Service that needed to be launched in a separate Thread – kioli Feb 29 '16 at 22:03
0

Have you ever work with RXjava?

1-get the librery: Write these two lines in the build.gradle:

compile 'io.reactivex.rxjava2:rxandroid:2.0.1'

compile 'io.reactivex.rxjava2:rxjava:2.1.2'

2-Write these lines in onStartCommand:

    io.reactivex.Observable.just(s).subscribeOn(AndroidSchedulers.mainThread())
                                                    .observeOn(Schedulers.io())
                                                    .subscribe(new io.reactivex.Observer<String>() {
                                                        @Override
                                                        public void onSubscribe(@NonNull Disposable d) {

                                                        }

                                                        @Override
                                                        public void onNext(@NonNull String s) {

                                                            //Write what you want here!! ;)

                                                        }

                                                        @Override
                                                        public void onError(@NonNull Throwable e) {

                                                        }

                                                        @Override
                                                        public void onComplete() {

                                                        }
                                                    });

and inside onNext write what ever you want, and it will execute on background thread

I hope it will be helpful

Community
  • 1
  • 1