Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9234601
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T06:49:42+00:00 2026-06-18T06:49:42+00:00

I have a service which is checking for new task all the time. If

  • 0

I have a service which is checking for new task all the time. If there is new task, I want to refresh the activity UI to show that info.
I did find https://github.com/commonsguy/cw-andtutorials/tree/master/18-LocalService/ this example. Is that a good approch ? Any other examples?

Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T06:49:43+00:00Added an answer on June 18, 2026 at 6:49 am

    See below for my original answer – that pattern has worked well, but recently I’ve started using a different approach to Service/Activity communication:

    • Use a bound service which enables the Activity to get a direct
      reference to the Service, thus allowing direct calls on it, rather
      than using Intents.
    • Use RxJava to execute asynchronous operations.

    • If the Service needs to continue background operations even when no
      Activity is running, also start the service from the Application
      class so that it does not get stopped when unbound.

    The advantages I have found in this approach compared to the startService()/LocalBroadcast technique are

    • No need for data objects to implement Parcelable – this is particularly important to me as I am now sharing code between Android and iOS (using RoboVM)
    • RxJava provides canned (and cross-platform) scheduling, and easy composition of sequential asynchronous operations.
    • This should be more efficient than using a LocalBroadcast, though the overhead of using RxJava may outweigh that.

    Some example code. First the service:

    public class AndroidBmService extends Service implements BmService {
    
        private static final int PRESSURE_RATE = 500000;   // microseconds between pressure updates
        private SensorManager sensorManager;
        private SensorEventListener pressureListener;
        private ObservableEmitter<Float> pressureObserver;
        private Observable<Float> pressureObservable;
    
        public class LocalBinder extends Binder {
            public AndroidBmService getService() {
                return AndroidBmService.this;
            }
        }
    
        private IBinder binder = new LocalBinder();
    
        @Nullable
        @Override
        public IBinder onBind(Intent intent) {
            logMsg("Service bound");
            return binder;
        }
    
        @Override
        public int onStartCommand(Intent intent, int flags, int startId) {
            return START_NOT_STICKY;
        }
    
        @Override
        public void onCreate() {
            super.onCreate();
    
            sensorManager = (SensorManager)getSystemService(SENSOR_SERVICE);
            Sensor pressureSensor = sensorManager.getDefaultSensor(Sensor.TYPE_PRESSURE);
            if(pressureSensor != null)
                sensorManager.registerListener(pressureListener = new SensorEventListener() {
                    @Override
                    public void onSensorChanged(SensorEvent event) {
                        if(pressureObserver != null) {
                            float lastPressure = event.values[0];
                            float lastPressureAltitude = (float)((1 - Math.pow(lastPressure / 1013.25, 0.190284)) * 145366.45);
                            pressureObserver.onNext(lastPressureAltitude);
                        }
                    }
    
                    @Override
                    public void onAccuracyChanged(Sensor sensor, int accuracy) {
    
                    }
                }, pressureSensor, PRESSURE_RATE);
        }
    
        @Override
        public Observable<Float> observePressure() {
            if(pressureObservable == null) {
                pressureObservable = Observable.create(emitter -> pressureObserver = emitter);
                pressureObservable = pressureObservable.share();
            }
             return pressureObservable;
        }
    
        @Override
        public void onDestroy() {
            if(pressureListener != null)
                sensorManager.unregisterListener(pressureListener);
        }
    } 
    

    And an Activity that binds to the service and receives pressure altitude updates:

    public class TestActivity extends AppCompatActivity {
    
        private ContentTestBinding binding;
        private ServiceConnection serviceConnection;
        private AndroidBmService service;
        private Disposable disposable;
    
        @Override
        protected void onDestroy() {
            if(disposable != null)
                disposable.dispose();
            unbindService(serviceConnection);
            super.onDestroy();
        }
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            binding = DataBindingUtil.setContentView(this, R.layout.content_test);
            serviceConnection = new ServiceConnection() {
                @Override
                public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
                    logMsg("BlueMAX service bound");
                    service = ((AndroidBmService.LocalBinder)iBinder).getService();
                    disposable = service.observePressure()
                        .observeOn(AndroidSchedulers.mainThread())
                        .subscribe(altitude ->
                            binding.altitude.setText(
                                String.format(Locale.US,
                                    "Pressure Altitude %d feet",
                                    altitude.intValue())));
                }
    
                @Override
                public void onServiceDisconnected(ComponentName componentName) {
                    logMsg("Service disconnected");
                }
            };
            bindService(new Intent(
                this, AndroidBmService.class),
                serviceConnection, BIND_AUTO_CREATE);
        }
    }
    

    The layout for this Activity is:

    <?xml version="1.0" encoding="utf-8"?>
    <layout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        >
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            tools:context="com.controlj.mfgtest.TestActivity">
    
            <TextView
                tools:text="Pressure"
                android:id="@+id/altitude"
                android:gravity="center_horizontal"
                android:layout_gravity="center_vertical"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"/>
    
        </LinearLayout>
    </layout>
    

    If the service needs to run in the background without a bound Activity it can be started from the Application class as well in OnCreate() using Context#startService().


    My Original Answer (from 2013):

    In your service: (using COPA as service in example below).

    Use a LocalBroadCastManager. In your service’s onCreate, set up the broadcaster:

    broadcaster = LocalBroadcastManager.getInstance(this);
    

    When you want to notify the UI of something:

    static final public String COPA_RESULT = "com.controlj.copame.backend.COPAService.REQUEST_PROCESSED";
    
    static final public String COPA_MESSAGE = "com.controlj.copame.backend.COPAService.COPA_MSG";
    
    public void sendResult(String message) {
        Intent intent = new Intent(COPA_RESULT);
        if(message != null)
            intent.putExtra(COPA_MESSAGE, message);
        broadcaster.sendBroadcast(intent);
    }
    

    In your Activity:

    Create a listener on onCreate:

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        super.setContentView(R.layout.copa);
        receiver = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                String s = intent.getStringExtra(COPAService.COPA_MESSAGE);
                // do something here.
            }
        };
    }
    

    and register it in onStart:

    @Override
    protected void onStart() {
        super.onStart();
        LocalBroadcastManager.getInstance(this).registerReceiver((receiver), 
            new IntentFilter(COPAService.COPA_RESULT)
        );
    }
    
    @Override
    protected void onStop() {
        LocalBroadcastManager.getInstance(this).unregisterReceiver(receiver);
        super.onStop();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have web service which i want to use to upload image to the
In my onCreate method in main activity i have some code which is checking
We have built a Windows Service that is running on client's machines, which occasionally
have a problem with a system. I have running service, which is constantly checking
I have a service which has a method that downloads an image from an
So, Here is my scenario: I have two activity and one service --> all
I have a service which I know is already running, how does my activity
I have a service which is running in a system in several instances. I
I have web service which return json string like : d={main0ID:abc.es/main,main1ID:ah/main} I wanna append
I have web-service which give list of property at a particular area My problem

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.