Android: Service to get and send GPS co-ordinates to server

こ雲淡風輕ζ 提交于 2020-01-06 10:51:16

问题


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.



来源:https://stackoverflow.com/questions/23379796/android-service-to-get-and-send-gps-co-ordinates-to-server

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!