如何将App背景上的GPS位置更新为前景? [英] How to update GPS Location on App Background to Foreground?

查看:131
本文介绍了如何将App背景上的GPS位置更新为前景?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将GPS_PROVIDER或NETWORK_SERVICE_PROVIDER用于纬度和经度.我想检测具体何时App会从后台变为前台(按Home按钮).

I am using GPS either GPS_PROVIDER or NETWORK_SERVICE_PROVIDER for latitude and longitude . I want to detect when specifically App goes background to foreground (Either By Home button) .

问题:

  1. GPS检测到的位置不会不时更新,直到有任何应用程序使用它,这样才能获取更新的位置.
  2. 将背景检测到应用程序的前景并检测位置,而不会出现电池问题.
  3. 任何GPS位置都会不时更新,而不会出现电池问题.

推荐答案

要在后台获取位置,请经常在android服务类中使用FusedLocation提供程序.

For get location in background frequently use FusedLocation provider in a android service class.

融合的位置提供了更准确的位置,并消耗了更少的电量.

Fused location provide give more accurate location and consume less power.

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

private static final String TAG = "LocationService";
private static final int LOCATION_INTERVAL = 1000;
private Context mContext;
private LocationRequest locationRequest;
private GoogleApiClient googleApiClient;
private FusedLocationProviderApi fusedLocationProviderApi;

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

@Override
public int onStartCommand(Intent intent, int flags, int startId){
    Log.e(TAG, "onStartCommand");
    super.onStartCommand(intent, flags, startId);       
    return START_STICKY;
}

@Override
public void onCreate(){
    Log.e(TAG, "onCreate");
    mContext = this;
    getLocation();
}

@Override
public void onDestroy(){
    Log.e(TAG, "onDestroy");
    super.onDestroy();
    try{
        if(googleApiClient!=null){
            googleApiClient.disconnect();
        }
    }
    catch(Exception e){
        e.printStackTrace();
    }

} 

private void getLocation(){
    locationRequest = LocationRequest.create();
    locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
    locationRequest.setInterval(LOCATION_INTERVAL);
    locationRequest.setFastestInterval(LOCATION_INTERVAL);
    fusedLocationProviderApi = LocationServices.FusedLocationApi;
    googleApiClient = new GoogleApiClient.Builder(this)
    .addApi(LocationServices.API)
    .addConnectionCallbacks(this)
    .addOnConnectionFailedListener(this)
    .build();
    if (googleApiClient != null) {
        googleApiClient.connect();
      }
  }

 @Override
public void onConnected(Bundle arg0) {
//  Location location = fusedLocationProviderApi.getLastLocation(googleApiClient);
    fusedLocationProviderApi.requestLocationUpdates(googleApiClient, locationRequest, this);
}

@Override
public void onConnectionSuspended(int arg0) {

}

@Override
public void onResponse(int reqCode, int statusCode, String json) {

}

@Override
public void onCancel(boolean canceled) {

}

@Override
public void onProgressChange(int progress) {

}

@Override
public void onLocationChanged(Location location) {
    Toast.makeText(mContext, "Driver location :"+location.getLatitude()+" , "+location.getLongitude(), Toast.LENGTH_SHORT).show();

}

@Override
public void onStatusChanged(String provider, int status, Bundle extras) {

}

@Override
public void onProviderEnabled(String provider) {

}

@Override
public void onProviderDisabled(String provider) {

}

@Override
public void onConnectionFailed(ConnectionResult arg0) {

}
}

并通过您的活动致电服务-

and call service from your activity-

    startService(new Intent(mContext,LocationService.class));

希望它会对您有所帮助.

hope it will help you.

已编辑

还要在清单中添加以下代码-

Also add below code to your manifest -

    <service 
         android:name=".LocationService">
     </service>

<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>

这篇关于如何将App背景上的GPS位置更新为前景?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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