Search code examples
androidandroid-service-binding

update listview in my activity from service android


I having 2 classes ,

1.Activity class

2.Service class

I need to update my list view in my activity,when service got any updates. Actually i trying like an chat application , My services always checking my db and if it got any new string , i need to update in my activity without rebuild the again only i need to refresh the list view. i found it will be manipulated using iBinder , But i don't how to use it. Can any one suggest me with some examples of code .

referred pages


Solution

  • You should use a Bound Service. I did the something similar in my application. Where upon clicking refresh, I invoke a service which gets data in background and updates the UI.

    Check out my service here: https://github.com/madhur/GAnalytics/blob/develop/src/in/co/madhur/ganalyticsdashclock/AnalyticsDataService.java

    @Override
    protected void onPostExecute(AnalyticsAccountResult result) {
        super.onPostExecute(result);
        App.getEventBus().post(result);
    }
    

    Activity: https://github.com/madhur/GAnalytics/blob/develop/src/in/co/madhur/ganalyticsdashclock/MainActivity.java

    @Subscribe
    public void UpdateUI(AnalyticsAccountResult result) {
        ProgressBar progressbar = (ProgressBar) findViewById(R.id.pbHeaderProgress);
        LinearLayout spinnerLayout = (LinearLayout) findViewById(R.id.spinnerslayout);
        TextView statusMessage = (TextView) findViewById(R.id.statusMessage);
    
        switch (result.getStatus()) {
            case STARTING:
            statusMessage.setVisibility(View.GONE);
            progressbar.setVisibility(View.VISIBLE);
            spinnerLayout.setVisibility(View.GONE);
            break;
    
            case FAILURE:
            statusMessage.setVisibility(View.VISIBLE);
            progressbar.setVisibility(View.GONE);
            spinnerLayout.setVisibility(View.GONE);
            statusMessage.setText(result.getErrorMessage());
            break;
    
            case SUCCESS:
            statusMessage.setVisibility(View.GONE);
            progressbar.setVisibility(View.GONE);
            spinnerLayout.setVisibility(View.VISIBLE);
    
            if (result.getItems() != null)
            {
                this.acProfiles = result.getItems();
                MyAdapter myAdapter = new MyAdapter(acProfiles, this);
                listView.setAdapter(myAdapter);
                UpdateSelectionPreferences();
                if (result.isPersist() && acProfiles.size() > 0)
                {
                    if (App.LOCAL_LOGV)
                    Log.v(App.TAG, "saving configdata");        
                    try
                    {
                        appPreferences.saveConfigData(acProfiles, credential.getSelectedAccountName());
                    }
                    catch (JsonProcessingException e)
                    {
                        Log.e(App.TAG, e.getMessage());
                    }
                }
            }
            break;
        }
    }
    

    It would also helpful to use Otto library:
    http://square.github.io/otto/