Android: Service to get and send GPS co-ordinates

2019-08-31 23:02发布

问题:

I know this is duplicate question, but I am not getting the explanatory example or solution. I want to develop an application which after run calls a background service which takes the GPS location of device and send it via PHP web service to the server. And when user wants, he can stop the service.

I am working on service for the first time. I searched on google and found many tutorial, but not getting it.

So please guide me for this.

回答1:

you can able to send current location or lat and lag point into your server using webservice

frequently you want to update your location or lat and lag use handler

final Handler handler = new Handler();
handler.postDelayed( new Runnable() {

 @Override
 public void run() {
handler.postDelayed( this, 60 * 1000 );
 }
}, 60 * 1000 );


回答2:

You can get the Location Tracking Code Here,

you need to run service with Timers or PendingIntents.

Timer timer = new Timer();
    TimerTask task = new TimerTask() {

        @Override
        public void run() {
            // getLocation From Here
        }
    };
    timer.scheduleAtFixedRate(task, 0, 60*1000);

Or

        PendingIntent pendingIntent = PendingIntent.getBroadcast(mActivity,0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
        AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(System.currentTimeMillis());
        calendar.add(Calendar.SECOND, 10);
        interval = 1 * 60 * 1000;   
        alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, 
        calendar.getTimeInMillis(), interval, pendingIntent);

And you can get the Location Tracking with Service Code Here

In this case you running a background service continuously. so battery will dry.