How can I update information in an Android Activity from a background Service

前端 未结 5 1933
北海茫月
北海茫月 2020-11-29 17:06

I am trying to create a simple Android application that has a ActivityList of information, when the application starts, I plan to start a Service that will be constantly cal

相关标签:
5条回答
  • 2020-11-29 17:08

    You will have a background thread running that calculates the changes in the list. This thread now needs a possibility to notify the GUI that the list was updated.

    You can use some kind of ArrayAdapter to get the data into the ListView. The ArrayAdapter has a method called adpater.notifyDataSetChanged() every time you call this method the adapter will see that the corresponding data has changed and then notify the listview that it should update at the next possibility.

    0 讨论(0)
  • 2020-11-29 17:10

    How can I set up my Activity to be listening to the Service? Is this the best way to approach this problem?

    You have three major options, as I see it:

    1. Polling. The Activity periodically asks the Service for the latest data. IMHO, this option sucks, but it's certainly possible.

    2. Callbacks. Per jax's answer, the Activity registers a callback object ("observer") with the Service. The Service invokes a method on the callback when the data changes, which in turn updates the UI. You can see an example of using that with a Service here.

    3. Broadcast Intents. The Service broadcasts an Intent via sendBroadcast() on a data change. The Activity registers a BroadcastReceiver using registerReceiver(), and that BroadcastReceiver is notified of an incoming broadcast. This triggers the Activity to load the latest data from the Service, or possibly just to get the latest data out of extras in the broadcast Intent. You can see an example of using that technique with a Service here.

    0 讨论(0)
  • 2020-11-29 17:14

    This sound like a good candidate for the Observer Pattern. Basically your activity (The Observer) will register itself with the background service (The Observable) and you can push or pull data from your Activity. Your Observer in this case will be your Activity and the Observable will be your Service.

    If you know nothing about Design Patterns buy "Head First Design Patterns", it is easy to read and is full of great information.

    PS: I am reading it now.

    0 讨论(0)
  • 2020-11-29 17:17

    You need to use bindService() to bind the activity with running service and communicate with it.

    public class BindingActivity extends Activity {
    YourService mService;
    boolean mBound = false;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }
    
    @Override
    protected void onStart() {
        super.onStart();
        // Bind to Your Service
        Intent intent = new Intent(this, YourService.class);
        bindService(intent, mConnection, Context.BIND_AUTO_CREATE);
    }
    
    @Override
    protected void onStop() {
        super.onStop();
        // Unbind from the service
        if (mBound) {
            unbindService(mConnection);
            mBound = false;
        }
    }
    
    /** Called when a button is clicked (the button in the layout file attaches to
      * this method with the android:onClick attribute) */
    public void onButtonClick(View v) {
        if (mBound) {
            // Call a method from your Service.
            // However, if this call were something that might hang, then this request should
            // occur in a separate thread to avoid slowing down the activity performance.
            int num = mService.getRandomNumber();
            Toast.makeText(this, "number: " + num, Toast.LENGTH_SHORT).show();
        }
    }
    
    /** Defines callbacks for service binding, passed to bindService() */
    private ServiceConnection mConnection = new ServiceConnection() {
    
        @Override
        public void onServiceConnected(ComponentName className,
                IBinder service) {
            // We've bound to the running Service, cast the IBinder and get instance
            LocalBinder binder = (LocalBinder) service;
            mService = binder.getService();
            mBound = true;
        }
    
        @Override
        public void onServiceDisconnected(ComponentName arg0) {
            mBound = false;
        }
     };
    }
    

    and your service like:

    public class LocalService extends Service {
        // Binder given to clients
       private final IBinder mBinder = new LocalBinder();
       // Random number generator
       private final Random mGenerator = new Random();
    
    /**
     * Class used for the client Binder.  Because we know this service always
     * runs in the same process as its clients, we don't need to deal with IPC.
     */
    public class LocalBinder extends Binder {
        LocalService getService() {
            // Return this instance of LocalService so clients can call public methods
            return LocalService.this;
        }
    }
    
    @Override
    public IBinder onBind(Intent intent) {
        return mBinder;
    }
    
    /** method for clients */
    public int getRandomNumber() {
      return mGenerator.nextInt(100);
      }
    }
    
    0 讨论(0)
  • 2020-11-29 17:35

    I am really, really wondering why no one mentioned a simple approach using an EventBus by whatever library. This is of course if you are not using RX. My personal favorite is EventBus by GreenRobot. https://github.com/greenrobot/EventBus

    With just a couple of lines of code, and no interfaces. Fire an event, and listen for it wherever you want. It is decoupled, it is thread safe, and it will not crash your app.

    0 讨论(0)
提交回复
热议问题