在退出MapView后,GPS位置始终在通知托盘中保持活动状态 [英] GPS Location always remains active in notification tray after exit MapView

查看:188
本文介绍了在退出MapView后,GPS位置始终在通知托盘中保持活动状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

b b 通过GPS设置的位置
LocationServices.FusedLocationApi.removeLocationUpdates()






编辑标题: b


在退出MapView之后的通知栏中


我想禁用移除GPS设置的位置和位置图标顶部通知托盘退出我的地图活动后,但它没有。



我在使用下面的变量:

  GoogleMap mGoogleMap; 
MapView mapView;
LocationRequest mLocationRequest;
GoogleApiClient mGoogleApiClient;
位置mLastLocation;
标记mCurrLocationMarker;
MapLocationListener mapLocationListener;

我试过的:

  @Override 
public void onPause(){
super.onPause();
//当Activity不再有效时停止位置更新
if(mGoogleApiClient!= null){
LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient,mapLocationListener);
if(mGoogleApiClient!= null){
mGoogleApiClient = null;
}
if(mapView!= null){
mapView = null;
}
if(mGoogleMap!= null){
mGoogleMap = null;
}
if(mLocationRequest!= null){
mLocationRequest = null;
}
if(mLastLocation!= null){
mLastLocation = null;
}
if(mCurrLocationMarker!= null){
mCurrLocationMarker = null;
}
if(mapLocationListener!= null){
mapLocationListener = null;
}
}
activity.finish();
}

据我所知,将null分配给我的引用不应该是解决方案,因为位置进程在系统后台运行,而不是使用我的应用程序引用。
,位置监听程序不再更新,但它并不像其他应用程序那样禁用顶部的位置图标。

我想要什么:
在退出后立即禁用GPS设置的位置和顶部通知栏中的位置图标我的地图活动。我不想让用户认为我在执行位置黑客攻击

我发现了什么:
我找到的所有解决方案都是使用 LocationManager.removeUpdate ,我没有它。我如何在我的活动中声明它?这是否真的可以解决我的问题?

使用丹尼尔纽金特的答案......

  import android.content.Context; 
导入android.content.Intent;
导入android.location.Location;
导入android.location.LocationManager;
导入android.os.Build;
导入android.os.Bundle;
导入android.support.v4.app.ActivityCompat;
导入android.support.v4.content.ContextCompat;
导入android.support.v7.app.AppCompatActivity;
导入android.util.Log;
导入android.view.KeyEvent;
导入android.view.MenuItem;
导入android.view.View;
导入android.widget.Button;

import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.location.LocationRequest;
import com.google.android.gms.location.LocationServices;
导入com.google.android.gms.maps.GoogleMap;
导入com.google.android.gms.maps.MapView;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.model.LatLng;
导入com.google.android.gms.maps.model.Marker;

导入static android.Manifest.permission.ACCESS_FINE_LOCATION;
import static android.content.pm.PackageManager.PERMISSION_GRANTED;
导入静态android.location.LocationManager.GPS_PROVIDER;
import static android.location.LocationManager.NETWORK_PROVIDER;

/ **
*由17年4月11日elliotching创建。
* /

public class ActivityMaps_ extends MyCustomActivity {

GoogleMap mGoogleMap;
MapView mapView;
LocationRequest mLocationRequest;
GoogleApiClient mGoogleApiClient;
位置mLastLocation;
标记mCurrLocationMarker;
MapLocationListener mapLocationListener;
LocationManager locManager;

按钮buttonSaveLocation;

double [] markerLocation;

private final static int mapZoomLevel = 18;

上下文context = this;
AppCompatActivity activity =(AppCompatActivity)context;

private class GoogleApiClientConnection实现了GoogleApiClient.ConnectionCallbacks,GoogleApiClient.OnConnectionFailedListener {
$ b @Override
public void onConnected(Bundle bundle){

Log.d(onConnected,onConnected。);
mLocationRequest = new LocationRequest();
mLocationRequest.setInterval(1000);
mLocationRequest.setFastestInterval(50);
mLocationRequest.setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY);
if(ContextCompat.checkSelfPermission(context,
ACCESS_FINE_LOCATION)
== PERMISSION_GRANTED){
LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient,mLocationRequest,mapLocationListener);
}

}

@Override
public void onConnectionSuspended(int i){
}

@覆盖
public void onConnectionFailed(ConnectionResult connectionResult){
}
}

private class MapLocationListener实现com.google.android.gms.location.LocationListener {
@Override
public void onLocationChanged(Location location){
//执行我的位置更新...
}
}

私有类OnMapReady implements OnMapReadyCallback {

@Override
public void onMapReady(GoogleMap googleMap){
Log.d(onMapReady,onMapReady。);
mGoogleMap = googleMap;
mGoogleMap.setOnMapClickListener(new OnMapTouched());

// if device OS SDK> = 23(Marshmallow)
if(Build.VERSION.SDK_INT> = 23){
// IF Location已经授予$ Permission
if(ContextCompat.checkSelfPermission(context,ACCESS_FINE_LOCATION)== PERMISSION_GRANTED){

buildGoogleApiClient();
mGoogleMap.setMyLocationEnabled(true);

} else {

//请求位置权限
checkLocationPermission();



$ b //如果设备操作系统版本为5棒棒糖及以下(< = SDK_22)
其他{

if(checkLocationPermission_v22()){

buildGoogleApiClient();
mGoogleMap.setMyLocationEnabled(true);

} else {

// AlertDialog退出地图...

}
}
}
}

私人类OnMapTouched实现GoogleMap.OnMapClickListener {
$ b @Override
public void onMapClick(LatLng latLng){
//更改我的标记。 ..
}
}

@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);

// createMyView = setContentView(...)
createMyView(R.layout.activity_maps,R.id.toolbar);

mapLocationListener = new MapLocationListener();

buttonSaveLocation =(Button)findViewById(R.id.button_save_location);
buttonSaveLocation.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View view){
saveLocation();
}
});


mapView =(MapView)findViewById(R.id.mapView);
mapView.onCreate(savedInstanceState);

mapView.getMapAsync(new OnMapReady());

mapView.onResume();


}

private void saveLocation(){
Intent i = new Intent();
i.putExtra(savedlocation,markerLocation);
setResult(RESULT_OK,i);
this.onPause();
}

@Override
public void onPause(){
super.onPause(); (mGoogleApiClient!= null&& mapLocationListener!= null){
Log.d(FusedLocationApi,run!);

if
LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient,mapLocationListener);
}

activity.finish();
}

protected synchronized void buildGoogleApiClient(){

Log.d(buildGoogleApiClient,buildGoogleApiClient。);
GoogleApiClientConnection g = new GoogleApiClientConnection();
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(g)
.addOnConnectionFailedListener(g)
.addApi(LocationServices.API)
.build() ;
mGoogleApiClient.connect();


$ b private bool checkLocationPermission_v22(){
locManager =(LocationManager)context.getSystemService(LOCATION_SERVICE);
if(locManager.isProviderEnabled(GPS_PROVIDER)||
locManager.isProviderEnabled(NETWORK_PROVIDER)){
return true;
} else {
return false;
}
}

public static final int FR_PERMISSIONS_REQUEST_CODE_LOCATION = 99;
$ b $ private void checkLocationPermission(){
if(ContextCompat.checkSelfPermission(context,ACCESS_FINE_LOCATION)!= PERMISSION_GRANTED){

ActivityCompat.requestPermissions(activity,new String [ ] {ACCESS_FINE_LOCATION},FR_PERMISSIONS_REQUEST_CODE_LOCATION);

$ b}

@Override
public void onRequestPermissionsResult(int requestCode,String permissions [],int [] grantResults){

switch(requestCode){
case FR_PERMISSIONS_REQUEST_CODE_LOCATION:{

if(grantResults.length> 0&&&& amp;&&&&& amp; grantResults [0] == PERMISSION_GRANTED){

if(mGoogleApiClient == null){
buildGoogleApiClient();
if(ContextCompat.checkSelfPermission(this,ACCESS_FINE_LOCATION)== PERMISSION_GRANTED){

if
}
mGoogleMap.setMyLocationEnabled(true);
}

} else {

// AlertDialog退出地图...

}
return;
}


$ b $覆盖
public boolean onKeyDown(int keyCode,KeyEvent event){
if(Build.VERSION。 SDK_INT> 5
&& keyCode == KeyEvent.KEYCODE_BACK
&& event.getRepeatCount()== 0){

this.onPause();
返回true;
}
return super.onKeyDown(keyCode,event);
}

@Override
public boolean onOptionsItemSelected(MenuItem item){
if(item.getItemId()== android.R.id.home){
this.onPause();
返回true;
}
返回super.onOptionsItemSelected(item);
}

}


解决方案

使用此类进行位置跟踪。

  public class FusedLocationTracker实现LocationListener,GoogleApiClient.OnConnectionFailedListener,GoogleApiClient.ConnectionCallbacks {
上下文上下文;

保护GoogleApiClient mGoogleApiClient;
GoogleMap google地图;
LocationRequest mLocationRequest;
位置mCurrentLocation = null;
private static final long INTERVAL = 1000 * 5;
private static final long FASTEST_INTERVAL = 1000 * 1;
String TAG =LocationTracker;
boolean isGPSEnabled,wifiStatus;
WifiManager wifiManager;
FusedLocationDataInterface fusedLocationDataInterface;

public FusedLocationTracker(上下文上下文){
this.context = context;
wifiManager =(WifiManager)this.context.getSystemService(Context.WIFI_SERVICE);
wifiStatus = wifiManager.isWifiEnabled();
if(!wifiStatus){
wifiManager.setWifiEnabled(true);
wifiManager.startScan();
}
createLocationRequest();
buildGoogleApiClient();
}

public void createLocationRequest(){
mLocationRequest = new LocationRequest();
mLocationRequest.setInterval(INTERVAL);
mLocationRequest.setFastestInterval(FASTEST_INTERVAL);
mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);

Log.d(TAG,createLocationRequest);
}

protected synchronized void buildGoogleApiClient(){
Log.d(TAG,buildGoogleApiClient);
mGoogleApiClient = new GoogleApiClient.Builder(context)
.addApi(LocationServices.API)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.build() ;

mGoogleApiClient.connect();
}

@Override
public void onConnected(@Nullable Bundle bundle){
Log.d(TAG,onConnected);
startLocationUpdates();


public void startLocationUpdates(){
Log.d(TAG,startLocationUpdates);
if(ActivityCompat.checkSelfPermission(context,Manifest.permission.ACCESS_FINE_LOCATION)!= PackageManager.PERMISSION_GRANTED
&&& ActivityCompat.checkSelfPermission(context,Manifest.permission.ACCESS_COARSE_LOCATION)!= PackageManager.PERMISSION_GRANTED){
return;
}
PendingResult< Status> pendingResult = LocationServices.FusedLocationApi.requestLocationUpdates(
mGoogleApiClient,mLocationRequest,this);
}

@Override
public void onConnectionSuspended(int i){
Log.d(TAG,onConnectionSuspended);
mGoogleApiClient.connect();
}

@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult){
Log.d(TAG,onConnectionSuspended);


@Override
public void onLocationChanged(Location location){
Log.d(TAG,onLocationChanged);
if(location!= null){
fusedLocationDataInterface =(FusedLocationDataInterface)context;
fusedLocationDataInterface.getFusedLocationData(location);



public void stopLocationUpdates(){
Log.d(TAG,stopLocationUpdates);
if(mGoogleApiClient.isConnected()){
LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient,this);
if(!wifiStatus){
wifiManager.setWifiEnabled(false);






创建用于写入位置的接口

pre $公共接口FusedLocationDataInterface {
public void getFusedLocationData(Location location);

$ / code>

onCreate()方法创建类 FusedLocationTracker.class 的对象并在活动中实现接口。



覆盖此方法

  @Override 
public void getFusedLocationData(Location location){
LatLng locate = new LatLng(location。 getLatitude(),location.getLongitude());
mMap.addMarker(new MarkerOptions()。title(My Location)。position(locate));
mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(locate,16.0f));

$ / code>

如果您想停止位置服务,请调用 stopLocationUpdates()


Original Title:

"Location set by GPS" still remains active after LocationServices.FusedLocationApi.removeLocationUpdates()


Edited Title:

GPS Location always remains active in notification tray after exit MapView

I want to disable remove the "Location set by GPS" and the location icon in top notification tray right after exit my map activity but it doesn't.

I'm using someone's code in stackoverflow which using following variable only:

GoogleMap mGoogleMap;
MapView mapView;
LocationRequest mLocationRequest;
GoogleApiClient mGoogleApiClient;
Location mLastLocation;
Marker mCurrLocationMarker;
MapLocationListener mapLocationListener;

What I've tried:

@Override
public void onPause() {
    super.onPause();
    //stop location updates when Activity is no longer active
    if (mGoogleApiClient != null) {
        LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, mapLocationListener);
        if(mGoogleApiClient != null){
            mGoogleApiClient = null;
        }
        if(mapView != null){
            mapView = null;
        }
        if(mGoogleMap != null){
            mGoogleMap = null;
        }
        if(mLocationRequest != null){
            mLocationRequest = null;
        }
        if(mLastLocation != null){
            mLastLocation = null;
        }
        if(mCurrLocationMarker != null){
            mCurrLocationMarker = null;
        }
        if(mapLocationListener != null){
            mapLocationListener = null;
        }
    }
    activity.finish();
}

I understand that assigning null to my references should not be the solution because the location process is running in system background not using my app references.
And Yes, the location listener is no longer update but it just not disable the location icon on top like other apps did.
What I want:
disable the "Location set by GPS" and the location icon in top notification tray immediately after exit my map activity. I don't want to make my user thinks I'm doing "location hacking"

What I've found:
All the solution I found is using LocationManager.removeUpdate which I don't have it. How can I declare it in my activity? Is this can really solve my problem?

EDIT: Attacthed Code:
Using Daniel Nugent's answer...

import android.content.Context;
import android.content.Intent;
import android.location.Location;
import android.location.LocationManager;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.KeyEvent;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;

import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.location.LocationRequest;
import com.google.android.gms.location.LocationServices;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapView;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Marker;

import static android.Manifest.permission.ACCESS_FINE_LOCATION;
import static android.content.pm.PackageManager.PERMISSION_GRANTED;
import static android.location.LocationManager.GPS_PROVIDER;
import static android.location.LocationManager.NETWORK_PROVIDER;

/**
 * Created by elliotching on 11-Apr-17.
 */

public class ActivityMaps_ extends MyCustomActivity {

    GoogleMap mGoogleMap;
    MapView mapView;
    LocationRequest mLocationRequest;
    GoogleApiClient mGoogleApiClient;
    Location mLastLocation;
    Marker mCurrLocationMarker;
    MapLocationListener mapLocationListener;
    LocationManager locManager;

    Button buttonSaveLocation;

    double[] markerLocation;

    private final static int mapZoomLevel = 18;

    Context context = this;
    AppCompatActivity activity = (AppCompatActivity) context;

    private class GoogleApiClientConnection implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener {

        @Override
        public void onConnected(Bundle bundle) {

            Log.d("onConnected", "onConnected.");
            mLocationRequest = new LocationRequest();
            mLocationRequest.setInterval(1000);
            mLocationRequest.setFastestInterval(50);
            mLocationRequest.setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY);
            if (ContextCompat.checkSelfPermission(context,
                    ACCESS_FINE_LOCATION)
                    == PERMISSION_GRANTED) {
                LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, mapLocationListener);
            }

        }

        @Override
        public void onConnectionSuspended(int i) {
        }

        @Override
        public void onConnectionFailed(ConnectionResult connectionResult) {
        }
    }

    private class MapLocationListener implements com.google.android.gms.location.LocationListener {
        @Override
        public void onLocationChanged(Location location) {
            // DO MY LOCATION UPDATE...
        }
    }

    private class OnMapReady implements OnMapReadyCallback {

        @Override
        public void onMapReady(GoogleMap googleMap) {
            Log.d("onMapReady", "onMapReady.");
            mGoogleMap = googleMap;
            mGoogleMap.setOnMapClickListener(new OnMapTouched());

            //if device OS SDK >= 23 (Marshmallow)
            if (Build.VERSION.SDK_INT >= 23) {
                //IF Location Permission already granted
                if (ContextCompat.checkSelfPermission(context, ACCESS_FINE_LOCATION) == PERMISSION_GRANTED) {

                    buildGoogleApiClient();
                    mGoogleMap.setMyLocationEnabled(true);

                } else {

                    // Request Location Permission
                    checkLocationPermission();

                }
            }

            //if device OS is version 5 Lollipop and below ( <= SDK_22 )
            else {

                if (checkLocationPermission_v22()) {

                    buildGoogleApiClient();
                    mGoogleMap.setMyLocationEnabled(true);

                } else {

                    // AlertDialog TO EXIT MAP...

                }
            }
        }
    }

    private class OnMapTouched implements GoogleMap.OnMapClickListener {

        @Override
        public void onMapClick(LatLng latLng) {
            // CHANGE MY MARKER...
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // createMyView = setContentView(...)
        createMyView(R.layout.activity_maps, R.id.toolbar);

        mapLocationListener = new MapLocationListener();

        buttonSaveLocation = (Button) findViewById(R.id.button_save_location);
        buttonSaveLocation.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                saveLocation();
            }
        });


        mapView = (MapView) findViewById(R.id.mapView);
        mapView.onCreate(savedInstanceState);

        mapView.getMapAsync(new OnMapReady());

        mapView.onResume();


    }

    private void saveLocation() {
        Intent i = new Intent();
        i.putExtra("savedlocation", markerLocation);
        setResult(RESULT_OK, i);
        this.onPause();
    }

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

        if (mGoogleApiClient != null && mapLocationListener != null) {
            Log.d("FusedLocationApi", "run!");
            LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, mapLocationListener);
        }

        activity.finish();
    }

    protected synchronized void buildGoogleApiClient() {

        Log.d("buildGoogleApiClient", "buildGoogleApiClient.");
        GoogleApiClientConnection g = new GoogleApiClientConnection();
        mGoogleApiClient = new GoogleApiClient.Builder(this)
                .addConnectionCallbacks(g)
                .addOnConnectionFailedListener(g)
                .addApi(LocationServices.API)
                .build();
        mGoogleApiClient.connect();

    }

    private boolean checkLocationPermission_v22() {
        locManager = (LocationManager) context.getSystemService(LOCATION_SERVICE);
        if (locManager.isProviderEnabled(GPS_PROVIDER) ||
                locManager.isProviderEnabled(NETWORK_PROVIDER)) {
            return true;
        } else {
            return false;
        }
    }

    public static final int FR_PERMISSIONS_REQUEST_CODE_LOCATION = 99;

    private void checkLocationPermission() {
        if (ContextCompat.checkSelfPermission(context, ACCESS_FINE_LOCATION) != PERMISSION_GRANTED) {

            ActivityCompat.requestPermissions(activity, new String[]{ACCESS_FINE_LOCATION}, FR_PERMISSIONS_REQUEST_CODE_LOCATION);

        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {

        switch (requestCode) {
            case FR_PERMISSIONS_REQUEST_CODE_LOCATION: {

                if (grantResults.length > 0 && grantResults[0] == PERMISSION_GRANTED) {

                    if (ContextCompat.checkSelfPermission(this, ACCESS_FINE_LOCATION) == PERMISSION_GRANTED) {

                        if (mGoogleApiClient == null) {
                            buildGoogleApiClient();
                        }
                        mGoogleMap.setMyLocationEnabled(true);
                    }

                } else {

                    // AlertDialog to EXIT MAP...

                }
                return;
            }
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (Build.VERSION.SDK_INT > 5
                && keyCode == KeyEvent.KEYCODE_BACK
                && event.getRepeatCount() == 0) {

            this.onPause();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == android.R.id.home) {
            this.onPause();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

}

解决方案

Use this class for location tracking .

public class FusedLocationTracker implements LocationListener, GoogleApiClient.OnConnectionFailedListener, GoogleApiClient.ConnectionCallbacks {
    Context context;

    protected GoogleApiClient mGoogleApiClient;
    GoogleMap googleMap;
    LocationRequest mLocationRequest;
    Location mCurrentLocation = null;
    private static final long INTERVAL = 1000 * 5;
    private static final long FASTEST_INTERVAL = 1000 * 1;
    String TAG = "LocationTracker";
    boolean isGPSEnabled, wifiStatus;
    WifiManager wifiManager;
    FusedLocationDataInterface fusedLocationDataInterface;

    public FusedLocationTracker(Context context){
        this.context = context;
        wifiManager = (WifiManager)this.context.getSystemService(Context.WIFI_SERVICE);
        wifiStatus = wifiManager.isWifiEnabled();
        if (!wifiStatus){
            wifiManager.setWifiEnabled(true);
            wifiManager.startScan();
        }
        createLocationRequest();
        buildGoogleApiClient();
    }

    public void createLocationRequest() {
        mLocationRequest = new LocationRequest();
        mLocationRequest.setInterval(INTERVAL);
        mLocationRequest.setFastestInterval(FASTEST_INTERVAL);
        mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);

        Log.d(TAG, "createLocationRequest");
    }

    protected synchronized void buildGoogleApiClient() {
        Log.d(TAG, "buildGoogleApiClient");
        mGoogleApiClient = new GoogleApiClient.Builder(context)
                .addApi(LocationServices.API)
                .addConnectionCallbacks(this)
                .addOnConnectionFailedListener(this)
                .build();

        mGoogleApiClient.connect();
    }

    @Override
    public void onConnected(@Nullable Bundle bundle) {
        Log.d(TAG, "onConnected");
        startLocationUpdates();
    }

    public void startLocationUpdates() {
        Log.d(TAG, "startLocationUpdates");
        if (ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION)!= PackageManager.PERMISSION_GRANTED
            && ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            return;
        }
        PendingResult<Status> pendingResult = LocationServices.FusedLocationApi.requestLocationUpdates(
            mGoogleApiClient, mLocationRequest, this);
    }

    @Override
    public void onConnectionSuspended(int i) {
        Log.d(TAG, "onConnectionSuspended");
        mGoogleApiClient.connect();
    }

    @Override
    public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
        Log.d(TAG, "onConnectionSuspended");
    }

    @Override
    public void onLocationChanged(Location location) {
        Log.d(TAG, "onLocationChanged");
        if (location != null) {
            fusedLocationDataInterface = (FusedLocationDataInterface) context;
            fusedLocationDataInterface.getFusedLocationData(location);
        }
    }

    public void stopLocationUpdates() {
        Log.d(TAG, "stopLocationUpdates");
        if (mGoogleApiClient.isConnected()) {
            LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, this);
            if (!wifiStatus){
                wifiManager.setWifiEnabled(false);
            }
        }
    }
}

create interface for writing location

public interface FusedLocationDataInterface {
    public void getFusedLocationData(Location location);
}

In main activity in onCreate() method create object of class FusedLocationTracker.class and implement interface in the activity.

override this method

@Override
public void getFusedLocationData(Location location) {
    LatLng locate = new LatLng(location.getLatitude(), location.getLongitude());
    mMap.addMarker(new MarkerOptions().title("My Location").position(locate));
    mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(locate, 16.0f));
}

if you want to stop the locations service call the stopLocationUpdates().

这篇关于在退出MapView后,GPS位置始终在通知托盘中保持活动状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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