0

i have implemented service. it will run 3 minutes once in background. in the service i am getting the value from web service and storing in database .

the user in the SAME UI , the data has to update . i am displaying data from database into listview.

Below is my service class :

public class ServiceClass extends Service {

private DbHelper mHelper;
private SQLiteDatabase dataBase;

}

@Override
public IBinder onBind(Intent intent) {
    return null;
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    getList();
    return START_STICKY;

}

in fragement i have a method called getDataFromDB()

how to call this method in service , and the UI get updated dynamically.

could any one help to me over come this prm

John
  • 1,407
  • 7
  • 26
  • 51
  • Try this, it may help you. http://stackoverflow.com/questions/10334901/how-to-get-results-from-an-intentservice-back-into-an-activity http://stackoverflow.com/questions/9088315/start-intentservice-from-activity-and-refresh-activity-when-intentservice-is-fin/9089086#9089086 – Parthi Apr 22 '15 at 13:04

1 Answers1

0

You can return your own instance in the on bind method. The activity has then access to the service. Use the activity or any class in the activity context as listener and "give" it to the service. So the service can call the listener when new data is present.

Don't forget to unbind the activity and the service when one of them gets destroyed.

RaphMclee
  • 1,623
  • 13
  • 16
  • i have tried with bind method , but the service should run even the app is closed. but its getting force close if i call the bind service – John Apr 22 '15 at 11:34
  • But this is another problem then. Create an other question. Or search it in SO, should already been have answered. – RaphMclee Apr 22 '15 at 11:36
  • what is SO? we can't update the UI using onStartCommand method in the service??? – John Apr 22 '15 at 11:38
  • SO is StackOverflow :-). So to update the UI the Activity needs to be running. You can use the Bind i descibed to do it. Once the activity is destroyed the UI is gone, no point to update it. – RaphMclee Apr 22 '15 at 11:57