I am trying to implement a app which updates user location at every 15 seconds,stores data to database and show it to listview in my activity.
Location should always update even activity destroys,for that i have created LocationService class.

The problem is I am able to get updates and also able to store into database but i am unable to show these updates in listview at runtime means i want list should refresh at every 15 sec and show it to UI..
Also when I get details from database I am unable to get latest detail instead i get whole arraylist every time which affects my activity response.I want that only newly added data will be fetch from database so that it will take less time in loading but I want to display all data to list everytime. 

I have implement a thread (Commented code)which fetch data and show to listview but this is not right way to update UI..please suggest me a way so that i can refresh my list when new data is added into database

This is my activity

```
    public class MainActivity extends Activity {
    List<MyLocation> locationList = new ArrayList();
    ListView mList;
    LocationAdapter adapter;
    BroadcastReceiver receiver;
    LocationService mService;
    boolean mBound = false;
    private DbHelper dbHelper;
    private Button updateLocation;
        private ServiceConnection mConnection = new ServiceConnection() {

        @Override
        public void onServiceConnected(ComponentName className,
                                       IBinder service) {

            LocationService.LocalBinder binder = (LocationService.LocalBinder) service;
            mService = binder.getService();
            mBound = true;

        }

        @Override
        public void onServiceDisconnected(ComponentName arg0) {
            mBound = false;
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        updateLocation = (Button) findViewById(R.id.update_location);

        mList = (ListView) findViewById(R.id.listView);

        dbHelper = new DbHelper(this);

        updateLocation.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {

                    locationList = mService.displayLocation();
                    adapter = new LocationAdapter(MainActivity.this, locationList);
                    mList.setAdapter(adapter);
            }
        });
    /*
        Thread mThread = new Thread() {

            @Override
            public void run() {
                try {
                    while (!isInterrupted()) {
                        Thread.sleep(500);
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {

                                    locationList = dbHelper.getLocationDetails();
                                    Collections.reverse(locationList);
                                    adapter = new LocationAdapter(MainActivity.this, locationList);
                                    mList.setAdapter(adapter);

                            }
                        });
                    }
                } catch (InterruptedException e) {
                }
            }
        };

        mThread.start();*/

    }

    @Override
    protected void onStart() {
        super.onStart();

        Intent intent = new Intent(this, LocationService.class);
        bindService(intent, mConnection, Context.BIND_AUTO_CREATE);
    }

}

```

LocationService

```
    public class LocationService extends Service implements LocationListener, GoogleApiClient.ConnectionCallbacks {
    private final IBinder mBinder = new LocalBinder();
    ArrayList<MyLocation> locationList = new ArrayList<>();
    private DbHelper dbHelper;
    private Location mLocation;
    private GoogleApiClient mGoogleApiClient;
    private LocationRequest mLocationRequest;
    private String TAG = "Service";

    @Override
    public IBinder onBind(Intent arg0) {
        return mBinder;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        mGoogleApiClient = new GoogleApiClient.Builder(this)
                .addConnectionCallbacks(this)
                .addApi(LocationServices.API).build();
        mGoogleApiClient.connect();

        dbHelper = new DbHelper(this);
        createLocationRequest();
        displayLocation();

    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {

        Toast.makeText(this, "Service Started", Toast.LENGTH_LONG).show();
        return START_STICKY;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Log.d(TAG, "service destroy");

    }

         public List<MyLocation> displayLocation() {

           mLocation = LocationServices.FusedLocationApi
                   .getLastLocation(mGoogleApiClient);
           if (mLocation != null) {

               double latitude = mLocation.getLatitude();
               double longitude = mLocation.getLongitude();
               String lastUpdateTime = DateFormat.getTimeInstance().format(new Date());
               dbHelper.insertLocationDetails(longitude, latitude, lastUpdateTime);
               locationList = dbHelper.getLocationDetails();

               return locationList;

           } else {
               return null;
           }

    }

    protected void startLocationUpdates() {

        LocationServices.FusedLocationApi.requestLocationUpdates(
                mGoogleApiClient, mLocationRequest, this);
        Log.d(TAG, "Connected to update");

    }

    protected void createLocationRequest() {
        mLocationRequest = new LocationRequest();
        mLocationRequest.setInterval(5000);
        mLocationRequest.setFastestInterval(5000);
        mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);

    }

    public void onLocationChanged(Location location) {

        mLocation = location;

        Toast.makeText(getApplicationContext(), "Location changed",
                Toast.LENGTH_SHORT).show();

        displayLocation();
    }

    public void onConnected(Bundle arg0) {

        startLocationUpdates();

    }

    @Override
    public void onConnectionSuspended(int arg0) {
        mGoogleApiClient.connect();

    }

    public class LocalBinder extends Binder {
        public LocationService getService() {

            return LocationService.this;
        }
    }

}

```

Dbhelper

```
    public class DbHelper extends SQLiteOpenHelper {
    private static final int DATABASE_VERSION = 1;
    private static final String LONGITUDE = "longitude";
    private static final String LATITUDE = "latitude";
    private static final String LOCATION_CHANGE_TIME = "location_change_time";
    private static final String LOCATION_DETAIL_TABLE = "location_detail_table";
    private static final String CREATE_TABLE_LOCATION = "CREATE TABLE "
            + LOCATION_DETAIL_TABLE + " (" + LONGITUDE + " TEXT,"
            + LOCATION_CHANGE_TIME + " TEXT,"
            + LATITUDE + " TEXT)";
    public static String DATABASE_NAME = "Location_database";

    public DbHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);

    }

    @Override
    public void onCreate(SQLiteDatabase database) {
        database.execSQL(CREATE_TABLE_LOCATION);

    }

    public void insertLocationDetails(double longitude, double latitude, String time) {
        SQLiteDatabase database = this.getWritableDatabase();

        ContentValues values = new ContentValues();
        values.put(LONGITUDE, longitude);
        values.put(LATITUDE, latitude);
        values.put(LOCATION_CHANGE_TIME, time);
        long id=  database.insert(LOCATION_DETAIL_TABLE, null, values);
        System.out.println("Newly added item id "+id);
        database.close();

    }

    public ArrayList<MyLocation> getLocationDetails() {

        ArrayList<MyLocation> locationList = new ArrayList();
        String selectQuery = "SELECT  * FROM " + LOCATION_DETAIL_TABLE;
        SQLiteDatabase database = this.getReadableDatabase();
        Cursor cursor = database.rawQuery(selectQuery, null);

        if (cursor.moveToNext()) {
            do {
                MyLocation location = new MyLocation();

                String longitude = cursor.getString(cursor
                        .getColumnIndexOrThrow(LONGITUDE));
                String latitude = cursor.getString(cursor.getColumnIndexOrThrow(LATITUDE));
                String time = cursor.getString(cursor
                        .getColumnIndexOrThrow(LOCATION_CHANGE_TIME));
                location.setLatitude(latitude);
                location.setLongitude(longitude);
                location.setLastUpdatedTime(time);
                locationList.add(location);

            } while (cursor.moveToNext());
        }

        if (cursor != null) {
            cursor.close();
        }
        database.close();
        return locationList;
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

    }
}

```