Java 退出MapView后,GPS位置始终在通知栏中保持活动状态
原标题: 之后,“GPS设置的位置”仍保持活动状态 LocationServices.FusedLocationApi.RemovelocationUpdate()Java 退出MapView后,GPS位置始终在通知栏中保持活动状态,java,android,gps,android-mapview,Java,Android,Gps,Android Mapview,原标题: 之后,“GPS设置的位置”仍保持活动状态 LocationServices.FusedLocationApi.RemovelocationUpdate() 编辑的标题: 退出MapView后,GPS位置始终在通知栏中保持活动状态 我想在退出地图活动后立即禁用“GPS设置的位置”和顶部通知栏中的位置图标,但没有 我正在stackoverflow中使用某人的代码,该代码仅使用以下变量: GoogleMap mGoogleMap; MapView mapView; LocationReque
编辑的标题: 退出MapView后,GPS位置始终在通知栏中保持活动状态 我想在退出地图活动后立即禁用“GPS设置的位置”和顶部通知栏中的位置图标,但没有 我正在stackoverflow中使用某人的代码,该代码仅使用以下变量:
GoogleMap mGoogleMap;
MapView mapView;
LocationRequest mLocationRequest;
GoogleApiClient mGoogleApiClient;
Location mLastLocation;
Marker mCurrLocationMarker;
MapLocationListener mapLocationListener;
我所尝试的:
我理解,为我的引用分配null不应该是解决方案,因为定位进程在系统后台运行,不使用我的应用引用。和是,定位侦听器不再更新,但它只是不像其他应用那样禁用顶部的位置图标。
我想要什么:
退出我的地图活动后,立即禁用“GPS设置的位置”和顶部通知栏中的位置图标。我不想让我的用户认为我在做“位置黑客”
我所发现的:
我发现的所有解决方案都是使用
LocationManager。删除我没有的更新。我如何在活动中声明它?这真的能解决我的问题吗
编辑:附加代码:
使用的答案
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);
}
}
导入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.AppActivity;
导入android.util.Log;
导入android.view.KeyEvent;
导入android.view.MenuItem;
导入android.view.view;
导入android.widget.Button;
导入com.google.android.gms.common.ConnectionResult;
导入com.google.android.gms.common.api.GoogleAppClient;
导入com.google.android.gms.location.LocationRequest;
导入com.google.android.gms.location.LocationServices;
导入com.google.android.gms.maps.GoogleMap;
导入com.google.android.gms.maps.MapView;
导入com.google.android.gms.maps.OnMapReadyCallback;
导入com.google.android.gms.maps.model.LatLng;
导入com.google.android.gms.maps.model.Marker;
导入静态android.Manifest.permission.ACCESS\u FINE\u位置;
导入静态android.content.pm.PackageManager.PERMISSION_;
导入静态android.location.LocationManager.GPS\U提供程序;
导入静态android.location.LocationManager.NETWORK\u提供程序;
/**
*由elliotching于2017年4月11日创建。
*/
公共类活动映射扩展MyCustomActivity{
谷歌地图;
地图视图;
位置请求mLocationRequest;
GoogleapClient MGoogleapClient;
位置mLastLocation;
标记器mCurrLocationMarker;
MapLocationListener MapLocationListener;
地点经理地点经理;
按钮定位;
双[]标记定位;
私有最终静态int-mapZoomLevel=18;
上下文=这个;
AppCompatActivity活动=(AppCompatActivity)上下文;
私有类GoogleAppClient连接实现GoogleAppClient.ConnectionCallbacks、GoogleAppClient.OnConnectionFailedListener{
@凌驾
未连接的公共空间(捆绑包){
Log.d(“未连接”、“未连接”);
mlLocationRequest=新位置请求();
mlLocationRequest.setInterval(1000);
mlLocationRequest.SetFastTestInterval(50);
mLocationRequest.setPriority(位置请求、优先级、平衡、功率、精度);
if(ContextCompat.checkSelfPermission)(上下文,
通道(精细位置)
==权限(已授予){
LocationServices.FusedLocationApi.RequestLocationUpdate(mgoogleAppClient、mlLocationRequest、mapLocationListener);
}
}
@凌驾
公共空间连接暂停(int i){
}
@凌驾
公共无效onConnectionFailed(ConnectionResult ConnectionResult){
}
}
私有类MapLocationListener实现com.google.android.gms.location.LocationListener{
@凌驾
已更改位置上的公共无效(位置){
//做我的位置更新。。。
}
}
私有类OnMapReady实现OnMapReadyCallback{
@凌驾
4月1日公开作废(谷歌地图谷歌地图){
Log.d(“onMapReady”、“onMapReady”);
mGoogleMap=谷歌地图;
setOnMapClickListener(新的OnMapTouched());
//如果设备操作系统SDK>=23(棉花糖)
如果(Build.VERSION.SDK_INT>=23){
//如果已授予位置权限
if(ContextCompat.checkSelfPermission(context,ACCESS\u FINE\u LOCATION)==已授予的权限){
buildGoogleAppClient();
mGoogleMap.setMyLocationEnabled(true);
}否则{
//请求位置权限
checkLocationPermission();
}
}
//如果设备操作系统为版本5棒棒糖及以下版本(0&&grantResults[0]==已授予权限){
if(ContextCompat.checkSelfPermission(此,访问\u FINE\u位置)=已授予的权限){
if(mGoogleApiClient==null){
buildGoogleAppClient();
}
mGoogleMap.setMyLocationEnabled(true);
}
}否则{
//警报对话框以退出映射。。。
}
返回;
}
}
}
@凌驾
公共布尔onKeyDown(int-keyCode,KeyEvent-偶数)
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);
}
}
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);
}
}
}
}
public interface FusedLocationDataInterface {
public void getFusedLocationData(Location location);
}
@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));
}