【问题标题】:How to use Google Location service API to get location updates even in background?即使在后台,如何使用 Google 位置服务 API 来获取位置更新?
【发布时间】:2016-09-05 09:36:29
【问题描述】:

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

我都试过了,但在维修的情况下,它不起作用。但是第一个链接的代码运行良好,并且可以正确更新位置。但我认为当应用程序处于后台时它无法更新。

我希望服务中的代码,即使我的应用在后台,它也可以持续获取位置更新。

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

如果有其他可用的方法,请建议我。我是安卓新手。

提前致谢!

【问题讨论】:

    标签: java android service geolocation


    【解决方案1】:

    试试:

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

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

    【讨论】:

      猜你喜欢
      • 2015-07-28
      • 2020-03-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-10
      • 1970-01-01
      • 2014-01-28
      相关资源
      最近更新 更多