Refresh list in my activity using android service

I have 2 classes,

Class 1.Activity

Class 2.Service

I need to update the list view in my activity when the service received any updates. In fact, I try as a chat application, My services always check my db, and if it has any new line, I need to update in my activity without rebuilding again, only I need to update the list view. I found that iBinder will work with it, but I do not use it. Can anyone suggest me some code examples.

referring pages

+6
source share
3 answers

You must use the Bound Service. In my application, I did something similar. Where, after clicking update, I call a service that receives data in the background and updates the user interface.

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 be useful to use the Otto library:
http://square.imtqy.com/otto/

+4
source

Suppose you have an activity class named MainActivity in which you initialized a ListView using an adapter named listviewAdapter. Put this code in MainActivity:

  public static Handler UIHandler; static { UIHandler = new Handler(Looper.getMainLooper()); } public static void runOnUI(Runnable runnable) { UIHandler.post(runnable); } 

When you have made changes to your list data in your service class, write this code to apply the new data to the ListView:

 MainActivity.runOnUI(new Runnable() { public void run() { try { MainActivity.listviewAdapter.notifyDataSetChanged(); } catch (Exception e) { e.printStackTrace(); } } }); 
+3
source

Without additional information, I cannot provide useful code examples, but I think you can look for a ListAdapter. The ListAdapter takes a list and a data set (in your case there can be an array of strings) and combines 2. Whenever the data set changes (in your case, it will be when your service detects a new line and adds it to the array), you simply call ListAdapter.notifyDataSetChanged () and the list will be automatically updated with your new information. http://developer.android.com/reference/android/widget/ArrayAdapter.html for more information on the specific ListAdapter that you can use.

0
source

All Articles