安卓:服务来获取和发送GPS坐标到服务器 [英] Android: Service to get and send GPS co-ordinates to server

查看:249
本文介绍了安卓:服务来获取和发送GPS坐标到服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这是重复的问题,但我没有得到解释的例子或解决方案。
我想开发出运行后调用后台服务这需要设备的GPS位置,并通过PHP Web服务发送到服务器上的应用程序。而当用户想要,他可以停止该服务。

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.

所以,请指导我的。

推荐答案

您可以将位置跟踪的 code这里

您需要用定时器或PendingIntents运行服务。

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);

或者

        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);

,你可以得到的位置与服务的 code这里

在这种情况下,连续运行的后台服务。所以电池会干。

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

这篇关于安卓:服务来获取和发送GPS坐标到服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆