开发者

How can i send just two parameters to a web server using JSON at regular intervals?

The application is based on GPS tracking where the latitude and l开发者_Go百科ongitude co-ordinates have to be sent at regular intervals. For now we are manually entering the co-ordinates through DDMS. Sending the data has been quite a problem. So, do any of you have some suggestions?


I think you have to store somewhere location so create ApplicationController which extends Application class from android and set it in android manifest, in AC create field Location and in onLocationChanged(Location location) made function which will save new location in AC than made TimerTask with frequency how often it should get location from AC and send it wherever you want.

ApplicationController will keep all information as long as application is "alive" in that class you have to also create accessors ;) sample code below:

ApplicationController.java

import android.app.Application;
import android.location.Location;

public class ApplicationController extends Application{
    private Location location = null;

    @Override
    public void onCreate() {
        super.onCreate();
    }

    public void setLocation(Location location){
        this.location = location;
    }

    public Location getLocation(){
        return location; 
    }
}

Entry in android manifest:

    <application 
        android:label="@string/app_name" 
        android:allowClearUserData="false" android:name="core.ApplicationController" 
android:icon="@drawable/driving">

Sample use in code:

AC = (ApplicationController) getApplicationContext();

    private void initializeGpsTask(long frequency){
        gpsTask = new TimerTask() {
            @Override
            public void run() {
                Log.i(TAG, "GPS TASK");
                if(AC.getLocation()!=null){
                insertIntoDatabase(transactions.generateRequest(
                                        AC.getLocation()));
                }
            }
        };
        Timer timer = new Timer();
        timer.schedule(gpsTask, frequency, frequency);-
}

That might be helpful LINK


You have to implement locationListener class to listen the location

onLocationChanged(Location location) {

lat = location.getLatitude();
lon = location.getLongitude(0;
}

Now when you get the latitude and longitude you will have to sent it to the server at regular interval. You can use the service to listen the location information from background and if you got the onLocationChanged event make a http connection to your web service and post the latitude and longitude to the server.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜