在Android后台获取用户GPS位置的最佳方法 [英] Best way to get user GPS location in background in Android

查看:19
本文介绍了在Android后台获取用户GPS位置的最佳方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的 android 应用程序中,我想每隔几分钟获取用户当前位置,并使用 Web 服务更新到我的中心服务器.目前我正在使用 Fused Location Provide 获取用户当前位置,请参阅 链接

In my android app i want to get user current location every few minute interval and update in to my center server using web service. Currently i am using Fused Location Provide for get user current location, See link

现在我想知道经常获取用户位置并调用网络服务的最佳方式.

now i want to know what is the best way to get user location frequently and call web service.

下面是我的代码,它给了我用户当前的位置:-

below is my code which gives me user current location: -

     locationrequest = LocationRequest.create();
     locationrequest.setInterval(10000);
     locationclient.requestLocationUpdates(locationrequest,new com.google.android.gms.location.LocationListener() {

        @Override
        public void onLocationChanged(Location location) {
              Log.i(TAG, "Last Known Location :" + location.getLatitude() + "," + location.getLongitude());
        }
    });

现在我必须从那里调用此代码.我可以在后台服务或其他地方使用它吗?

now from where i have to call this code. Can i use this in a background service or some where else.

请提供您的想法.

TIA.

推荐答案

其余答案均未使用:

com.google.android.gms.location.FusedLocationProviderClient

哪个是 Fused Location Provider,是 Google 与 Fused Location Provider 交互的主要入口,很难找到一个好的例子.这是 Google 于 2017 年年中发布的.

Which is the Fused Location Provider and the main entry point for interacting with the fused location provider by Google, and it is very hard to find a good example. This was released mid 2017 by Google.

Google Play 服务位置 API 优于 Android框架位置 API (android.location)

Google Play services location APIs are preferred over the Android framework location APIs (android.location)

如果您目前正在使用 Android 框架位置 API,强烈建议您尽快切换到 Google Play 服务位置 API.

If you are currently using the Android framework location APIs, you are strongly encouraged to switch to the Google Play services location APIs as soon as possible.

为了让您使用 Google Location API,首先将其添加到您的 build.gradle

For you to use the Google Location API, first add this to your build.gradle

compile 'com.google.android.gms:play-services:11.0.0'

那么就可以使用这个类Wherebouts.java:

Then you can use this class Wherebouts.java:

import android.location.Location;
import android.os.Looper;
import android.util.Log;

import com.google.android.gms.location.FusedLocationProviderClient;
import com.google.android.gms.location.LocationCallback;
import com.google.android.gms.location.LocationRequest;
import com.google.android.gms.location.LocationResult;
import com.google.android.gms.location.LocationServices;
import com.google.android.gms.location.LocationSettingsRequest;

/**
 * Uses Google Play API for obtaining device locations
 * Created by alejandro.tkachuk 
 * alejandro@calculistik.com
 * www.calculistik.com Mobile Development
 */

public class Wherebouts {

    private static final Wherebouts instance = new Wherebouts();

    private static final String TAG = Wherebouts.class.getSimpleName();

    private FusedLocationProviderClient mFusedLocationClient;
    private LocationCallback locationCallback;
    private LocationRequest locationRequest;
    private LocationSettingsRequest locationSettingsRequest;

    private Workable<GPSPoint> workable;

    private static final long UPDATE_INTERVAL_IN_MILLISECONDS = 1000;
    private static final long FASTEST_UPDATE_INTERVAL_IN_MILLISECONDS = 1000;

    private Wherebouts() {
        this.locationRequest = new LocationRequest();
        this.locationRequest.setInterval(UPDATE_INTERVAL_IN_MILLISECONDS);
        this.locationRequest.setFastestInterval(FASTEST_UPDATE_INTERVAL_IN_MILLISECONDS);
        this.locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);

        LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder();
        builder.addLocationRequest(this.locationRequest);
        this.locationSettingsRequest = builder.build();

        this.locationCallback = new LocationCallback() {
            @Override
            public void onLocationResult(LocationResult locationResult) {
                super.onLocationResult(locationResult); // why? this. is. retarded. Android.
                Location currentLocation = locationResult.getLastLocation();

                GPSPoint gpsPoint = new GPSPoint(currentLocation.getLatitude(), currentLocation.getLongitude());
                Log.i(TAG, "Location Callback results: " + gpsPoint);
                if (null != workable)
                    workable.work(gpsPoint);
            }
        };

        this.mFusedLocationClient = LocationServices.getFusedLocationProviderClient(MainApplication.getAppContext());
        this.mFusedLocationClient.requestLocationUpdates(this.locationRequest,
                this.locationCallback, Looper.myLooper());
    }

    public static Wherebouts instance() {
        return instance;
    }

    public void onChange(Workable<GPSPoint> workable) {
        this.workable = workable;
    }

    public LocationSettingsRequest getLocationSettingsRequest() {
        return this.locationSettingsRequest;
    }

    public void stop() {
        Log.i(TAG, "stop() Stopping location tracking");
        this.mFusedLocationClient.removeLocationUpdates(this.locationCallback);
    }

}

在您的 Activity 中,您可以像这样使用它,方法是传递一个 Workable 对象.一个 Workable 对象只不过是一个自定义的 Callback 类似对象.

From your Activity, you can use it like this, by passing a Workable object. A Workable object is nothing more than a custom Callback alike object.

 Wherebouts.instance().onChange(workable);

通过使用像 Workable 这样的回调,您将在 Activity 中编写与 UI 相关的代码,并将使用 GPS 的工作留给辅助类,如 Wherebouts.

By using a callback like Workable, you will write UI related code in your Activity and leave the hustle of working with GPS to a helper class, like Wherebouts.

    new Workable<GPSPoint>() {
        @Override
        public void work(GPSPoint gpsPoint) {
            // draw something in the UI with this new data
        }
    };

当然,您需要请求 Android 操作系统的相应权限才能让您的应用使用.您可以阅读以下文档以了解更多信息或进行一些研究.

Of course you would need to ask for the corresponding permissions to the Android OS for your App to use. You can read the following documentation for more info about that or do some research.

这篇关于在Android后台获取用户GPS位置的最佳方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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