[英]onLocationChanged called even with GPS turned off
I have been trying to get the current user location. 我一直在尝试获取当前的用户位置。 I turn on the GPS and launch the application. 我打开GPS并启动应用程序。 Everything seems to be ok, I can get the current user location. 一切似乎正常,我可以获取当前的用户位置。
The problem is when I turn off the GPS and run the application. 问题是当我关闭GPS并运行该应用程序时。 onLocationChanged()
is called. onLocationChanged()
被调用。 Should it be called with the GPS turned off? 是否应该在GPS关闭的情况下调用? If so, why? 如果是这样,为什么?
package company.com.locationservicesapi;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.location.Location;
import android.location.LocationManager;
import android.os.Bundle;
import android.provider.Settings;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesUtil;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.common.api.GoogleApiClient.ConnectionCallbacks;
import com.google.android.gms.common.api.GoogleApiClient.OnConnectionFailedListener;
import com.google.android.gms.common.api.PendingResult;
import com.google.android.gms.common.api.Status;
import com.google.android.gms.location.LocationListener;
import com.google.android.gms.location.LocationRequest;
import com.google.android.gms.location.LocationServices;
public class LocationServicesAPIActivity extends Activity implements ConnectionCallbacks, OnConnectionFailedListener, LocationListener {
private static final String TAG = "Location Service API";
GoogleApiClient googleApiClient;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
Log.i(TAG, "GPS turned on");
} else {
Log.i(TAG, "GPS turned off");
}
setContentView(R.layout.activity_location_services_api);
int result = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);
if (result == ConnectionResult.SUCCESS) {
googleApiClient = new GoogleApiClient.Builder(this)
.addApi(LocationServices.API)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.build();
googleApiClient.connect();
} else {
GooglePlayServicesUtil.getErrorDialog(result, this, 1).show();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.location_services_api, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
public void onConnected(Bundle bundle) {
Log.d(TAG, "Connected");
LocationRequest locationRequest = LocationRequest.create()
.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY)
.setInterval(1000)
.setNumUpdates(1);
LocationServices.FusedLocationApi.requestLocationUpdates(googleApiClient, locationRequest, this);
}
public void onLocationChanged(Location location) {
Log.d(TAG, "Location Changed");
TextView textView = (TextView) findViewById(R.id.textViewLocation);
textView.setText('@' + location.getLatitude() + "," + location.getLongitude());
}
@Override
public void onConnectionSuspended(int i) {
Log.d(TAG, "Connection Suspended");
}
@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
Log.d(TAG, "Connection Failed");
}
}
activity_location_services_api.xml: activity_location_services_api.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".LocationServicesAPIActivity">
<TextView
android:id="@+id/textViewLocation"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
AndroidManifest.xml: AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="company.com.locationservicesapi">
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme">
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
<activity
android:name=".LocationServicesAPIActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Logcat output: Logcat输出:
10-08 14:18:34.242 4208-4208/company.com.locationservicesapi I/Location Service API﹕ GPS turned off
10-08 14:18:34.625 4208-4208/company.com.locationservicesapi D/Location Service API﹕ Connected
10-08 14:18:35.765 4208-4208/company.com.locationservicesapi D/Location Service API﹕ Location Changed
The FusedLocationApi
isn't just calling onLocationChanged()
when a valid GPS signal is available. 当有效的GPS信号可用时, FusedLocationApi
不仅会调用onLocationChanged()
。 It uses WFPS, Cell-ID and Sensors to get valid locations, too. 它还使用WFPS,Cell-ID和传感器来获取有效位置。 That's what the FusedLocationApi is all about: Get the most accurate location available using all possibilities of your device. 这就是FusedLocationApi的全部意义:使用设备的所有可能性获取最准确的可用位置。
If you just want to listen to GPS signals, then the only way to do this is using the LocationManager
with the flag LocationManager.GPS_PROVIDER as your provider. 如果你只是想听听GPS信号,然后做到这一点的唯一方法是使用LocationManager
旗LocationManager.GPS_PROVIDER为你的供应商。
you have <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
inside your manifest, so it will use network to access approximate location. 您的清单中有<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
,因此它将使用网络访问大致位置。 see android manifest description 查看android清单说明
you can remove the location updates everytime you do not have any connections. 您可以在没有任何连接时删除位置更新。 just declare private LocationManager locationManager;
只需声明private LocationManager locationManager;
first and put 首先放
locationManager.removeUpdates(this);
this.locationManager = null;
on public void onConnectionFailed(ConnectionResult connectionResult)
在public void onConnectionFailed(ConnectionResult connectionResult)
i hope this helps 我希望这有帮助
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.