在后台服务中使用融合位置获取GPS更新对于前(15分钟)? [英] Getting GPS updates using fused location in background service For ex(15 mins)?

查看:197
本文介绍了在后台服务中使用融合位置获取GPS更新对于前(15分钟)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是android开发新手,需要在后台服务中获得gps位置,而无需用户交互,例如每15分钟需要获取坐标并将结果发送到服务器,我如何实现这一点。然后,我尝试融合的位置api syncadapter组合。它的作品,我得到的坐标,但我需要在专用的android服务中使用该位置类我如何实现这一需要,使该服务运行永远15分钟获取坐标并发送这导致服务器让我发布我的谷歌api客户端类。请检查下面的代码,我已经尝试。

  import android.content。上下文; 
导入android.location.Location;
导入android.os.Bundle;
导入android.support.annotation.NonNull;
导入android.util.Log;

import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesUtil;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.common.api.PendingResult;
import com.google.android.gms.common.api.Status;
import com.google.android.gms.location.FusedLocationProviderApi;
import com.google.android.gms.location.LocationListener;
import com.google.android.gms.location.LocationRequest;
import com.google.android.gms.location.LocationServices;

import java.text.DateFormat;
import java.util.Date;

/ **
*由2016年10月14日由4264创建。
* /

public class Locationlistener实现了GoogleApiClient.ConnectionCallbacks,GoogleApiClient.OnConnectionFailedListener,LocationListener {


LocationRequest mLocationRequest;
GoogleApiClient mGoogleApiClient;
位置mCurrentLocation;
String mLastUpdateTime;
私有上下文上下文;
private static final long INTERVAL = 1000 * 10;
private static final long FASTEST_INTERVAL = 1000 * 5;
public Locationlistener(Context c)
{
context = c;
//如果GoolglePlayServices不可用,则显示错误对话框
createLocationRequest();
mGoogleApiClient = new GoogleApiClient.Builder(context)
.addApi(LocationServices.API)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.build() ;
mGoogleApiClient.connect();
}
protected void createLocationRequest(){
mLocationRequest = new LocationRequest();
mLocationRequest.setInterval(INTERVAL);
mLocationRequest.setFastestInterval(FASTEST_INTERVAL);
mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
}
public String lat(){
String lat = null;
if(mCurrentLocation == null){
Log.d(is null,null);
}
else {
lat = String.valueOf(mCurrentLocation.getLatitude());
}
返回lat;
}

protected void startLocationUpdates(){
PendingResult< Status> pendingResult = LocationServices.FusedLocationApi.requestLocationUpdates(
mGoogleApiClient,mLocationRequest,this);
Log.d(开始,位置更新开始..............:);
}
public void updateUI(){
Log.d(updated,UI update initiated .............);
if(null!= mCurrentLocation){
String lat = String.valueOf(mCurrentLocation.getLatitude());
String lng = String.valueOf(mCurrentLocation.getLongitude());
Log.e(latw,lat);
Log.e(Long,lng);

} else {
Log.d(,location is null ...............);



$ b @Override
public void onConnected(Bundle bundle){
Log.d(connected,onConnected - isConnected ...............:+ mGoogleApiClient.isConnected());
startLocationUpdates();
}


@Override
public void onConnectionSuspended(int i){

}



$ b @Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult){
Log.d(failed,Connection failed:+ connectionResult.toString()) ;
}

@Override
public void onLocationChanged(Location location){
Log.d(locationchanged,Firing onLocationChanged ........ ......................................);
mCurrentLocation = location;
mLastUpdateTime = DateFormat.getTimeInstance()。format(new Date());
updateUI();


$ / code $ / pre
$ b $

它在syncadapter中有效,但我需要在服务我怎么能实现这个和另一个疑问,如果用户关闭GPS我怎么能得到的位置是可能的与网络位置更新提前谢谢!!

解决方案

我建议您使用AlarmManager尽快唤醒设备并接收位置(间隔0),然后在每15分钟继续睡眠。因此,这种方法比不停止服务更好。



如果用户关闭GPS,您将仍然(网络提供商)位置更新但由于位置设置不准确是不够的。同样为了解决这个问题,您可以使用 SettingsApi


向位置服务发出请求时,设备的系统设置可能处于阻止应用获取位置数据的状态需要。例如,GPS或Wi-Fi扫描可能会关闭。



I'm new for android development need to get gps location in background service without user interaction like for every 15 mins need to fetch the coordinates and send the results to server how can i achieve this. Then i tried fused location api syncadapter combination.It works, i'm getting coordinates but i need to use that location class in dedicated android-service how can i achieve this need to make that service run for ever 15 mins fetch the coordinates and send that result to server let me post my Google api client class.Please check below code i have tried.

import android.content.Context;
import android.location.Location;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.util.Log;

import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesUtil;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.common.api.PendingResult;
import com.google.android.gms.common.api.Status;
import com.google.android.gms.location.FusedLocationProviderApi;
import com.google.android.gms.location.LocationListener;
import com.google.android.gms.location.LocationRequest;
import com.google.android.gms.location.LocationServices;

import java.text.DateFormat;
import java.util.Date;

/**
 * Created by 4264 on 14-10-2016.
 */

public class Locationlistener implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener,LocationListener  {


    LocationRequest mLocationRequest;
    GoogleApiClient mGoogleApiClient;
    Location mCurrentLocation;
    String mLastUpdateTime;
    private Context context;
    private static final long INTERVAL = 1000 * 10;
    private static final long FASTEST_INTERVAL = 1000 * 5;
    public Locationlistener(Context c)
    {
        context=c;
        //show error dialog if GoolglePlayServices not available
        createLocationRequest();
        mGoogleApiClient = new GoogleApiClient.Builder(context)
                .addApi(LocationServices.API)
                .addConnectionCallbacks(this)
                .addOnConnectionFailedListener(this)
                .build();
        mGoogleApiClient.connect();
    }
    protected void createLocationRequest() {
        mLocationRequest = new LocationRequest();
        mLocationRequest.setInterval(INTERVAL);
        mLocationRequest.setFastestInterval(FASTEST_INTERVAL);
        mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
    }
    public String lat(){
        String lat=null;
        if(mCurrentLocation==null){
            Log.d("is null","null");
        }
        else {
            lat=String.valueOf(mCurrentLocation.getLatitude());
        }
        return lat;
    }

    protected void startLocationUpdates() {
        PendingResult<Status> pendingResult = LocationServices.FusedLocationApi.requestLocationUpdates(
                mGoogleApiClient, mLocationRequest, this);
        Log.d("started", "Location update started ..............: ");
    }
    public void updateUI() {
        Log.d("updated", "UI update initiated .............");
        if (null != mCurrentLocation) {
            String lat = String.valueOf(mCurrentLocation.getLatitude());
            String lng = String.valueOf(mCurrentLocation.getLongitude());
            Log.e("latw",lat);
            Log.e("Long",lng);

        } else {
            Log.d("", "location is null ...............");
        }
    }


    @Override
    public void onConnected(Bundle bundle) {
        Log.d("connected", "onConnected - isConnected ...............: " + mGoogleApiClient.isConnected());
        startLocationUpdates();
    }


    @Override
    public void onConnectionSuspended(int i) {

    }




    @Override
    public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
        Log.d("failed", "Connection failed: " + connectionResult.toString());
    }

    @Override
    public void onLocationChanged(Location location) {
        Log.d("locationchanged", "Firing onLocationChanged..............................................");
        mCurrentLocation = location;
        mLastUpdateTime = DateFormat.getTimeInstance().format(new Date());
        updateUI();
    }
}

It works in syncadapter but i need to make this in service how can i achieve this and another doubt if user turn off the gps how can i get location is it possible with network location updates thanks in advance!!

解决方案

I would suggest you to use AlarmManager to wake up device and receive location as soon as possible (interval 0) then continue to sleep at each 15 minutes. So this approach better than non-stop service.

If user turns off the gps, you will be still getting (network provider) location updates but not accurate due to location settings aren't adequate. Also to solve this, you can use SettingsApi

When making a request to location services, the device's system settings may be in a state that prevents an app from obtaining the location data that it needs. For example, GPS or Wi-Fi scanning may be switched off.

这篇关于在后台服务中使用融合位置获取GPS更新对于前(15分钟)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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