如何使用Google位置服务API即使在后台也能获取位置更新? [英] How to use Google Location service API to get location updates even in background?

查看:160
本文介绍了如何使用Google位置服务API即使在后台也能获取位置更新?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,这不是网站上任何其他问题的重复。我已经看到使用 Service 和Google API的位置更新。



我已经尝试了两种方式,但在服务的情况下,它不起作用。但第一个链接的代码工作得很好,并正确地提供位置更新。但我认为当应用程序在后台时无法更新。



我希望服务中的代码能够连续获取位置更新,即使我的应用程序处于后台。



但无法弄清楚如何合并这两个代码。如果你问我试过了什么?比我刚刚复制服务类中的常用方法。但它给了我太多的错误:(



)如果有其他替代方法可用,请给我建议我是新来的android。



提前致谢!

解决方案

试试:

Service.java

  import android.app.Service; 
import android.content.Context;
import android.content.Intent;
import android.location.Location;
import android.os.Bundle;
import android.os.IBinder;
import android.support .annotation.Nullable;
导入android.util.Log;
导入android.widget.Toast;
导入com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.location.LocationListener;
import com.google.android.gms.location.LocationRequest;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.location.LocationServices;


import java.io.IOException;
import java.io.OutputStreamWriter ;

公共类GPSService扩展Service实现GoogleApiClient.ConnectionCallbacks,GoogleApiClient.OnConnectionFailedListener,LocationListener {


$ b私有GoogleApiClient mGoogleApiClient;

私人LocationRequest mLocationRequest;

@Nullable
@Override
public IBinder onBind(Intent intent){
return null;
}


@Override
public void onCreate(){
super.onCreate();

mGoogleApiClient = new GoogleApiClient.Builder(this)
.addApi(LocationServices.API)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
。建立();


$ b @Override
public int onStartCommand(Intent intent,int flags,int startId){

mGoogleApiClient.connect() ;

返回super.onStartCommand(intent,flags,startId);
}

@Override
public void onDestroy(){


mGoogleApiClient.disconnect();
super.onDestroy();






@Override
public void onConnected(Bundle bundle){
mLocationRequest = LocationRequest 。创建();
mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
mLocationRequest.setInterval(1000); //每秒更新位置

LocationServices.FusedLocationApi.requestLocationUpdates(
mGoogleApiClient,mLocationRequest,this);
}

@Override
public void onConnectionSuspended(int i){
Toast.makeText(this,GoogleApiClient connection has been suspend,Toast.LENGTH_LONG)。显示();

$ b @Override
public void onConnectionFailed(ConnectionResult connectionResult){
Toast.makeText(this,GoogleApiClient connection has failed,Toast.LENGTH_LONG).show ();
}

和MainActivity.java

  @Override 
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

Intent i = new Intent(this,GPSService.class);
startService(i);


}


First of all this is not a duplicate of any other question on the site. I've seen location updates using Service and Google API both.

I've tried both, but in case of service, it is not working. But the 1st link's code is working perfectly fine, and gives location updates correctly. But I think It can't get update when the app is in background.

I want that code inside a service, which can continuously get location updates even my app is in background.

But can't figure out how to merge this two codes. If you are asking what I've tried? Than I've just copy the common methods in the service class. But it gives me too many errors :(

If there are alternative available for this please suggest me. I'm new to android.

Thanks in advance!

解决方案

Try:

Service.java

import android.app.Service;
import android.content.Context;
import android.content.Intent;
 import android.location.Location;
import android.os.Bundle;
import android.os.IBinder;
import android.support.annotation.Nullable;
import android.util.Log;
import android.widget.Toast;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.location.LocationListener;
import com.google.android.gms.location.LocationRequest;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.location.LocationServices;


import java.io.IOException;
import java.io.OutputStreamWriter;

public class GPSService extends Service implements GoogleApiClient.ConnectionCallbacks,GoogleApiClient.OnConnectionFailedListener,LocationListener {



private GoogleApiClient mGoogleApiClient;

private LocationRequest mLocationRequest;

@Nullable
@Override
public IBinder onBind(Intent intent) {
    return null;
}


@Override
public void onCreate() {
    super.onCreate();

    mGoogleApiClient = new GoogleApiClient.Builder(this)
            .addApi(LocationServices.API)
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this)
            .build();

}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {

    mGoogleApiClient.connect();

    return super.onStartCommand(intent, flags, startId);
}

@Override
public void onDestroy() {


    mGoogleApiClient.disconnect();
    super.onDestroy();



}


@Override
public void onConnected(Bundle bundle) {
    mLocationRequest = LocationRequest.create();
    mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
    mLocationRequest.setInterval(1000); // Update location every second

    LocationServices.FusedLocationApi.requestLocationUpdates(
            mGoogleApiClient, mLocationRequest, this);
}

@Override
public void onConnectionSuspended(int i) {
    Toast.makeText(this,"GoogleApiClient connection has been suspend",Toast.LENGTH_LONG).show();
}

@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
    Toast.makeText(this,"GoogleApiClient connection has failed",Toast.LENGTH_LONG).show();
}

and MainActivity.java

    @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Intent i = new Intent(this,GPSService.class);
    startService(i);


}

这篇关于如何使用Google位置服务API即使在后台也能获取位置更新?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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