简体   繁体   English

即使GPS关闭,也会调用onLocationChanged

[英]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.

相关问题 即使可以使用GPS提供程序,也不会调用onLocationChanged - onLocationChanged isn't called even though the GPS provider is availble Android GPS onLocationChanged从未调用过 - Android GPS onLocationChanged never called GPS Android跟踪器LocationListener onLocationChanged(); 不叫 - GPS Android Tracker LocationListener onLocationChanged(); not called 永远不会通过与GPS提供程序一起使用requestLocationUpdates来调用onLocationChanged - onLocationChanged is never called by using requestLocationUpdates with GPS provider 如果GPS禁用或未与Google共享位置,则不会调用onLocationChanged - onLocationChanged not called if GPS disabled or if not sharing location with Google 当gps是提供者时,从未调用过OnLocationChanged - OnLocationChanged never called when gps is provider 即使在用户强行关闭gps的情况下,如何使用fusionloaction api在后台获取位置信息? - How to get location information in background using fusedloaction api even when gps is turned off forcefully by the user? 即使使用融合位置 API 关闭 GPS,如何获取位置更新? - How to get Location updates even when gps is turned off using fused location API? 如果关闭gps,则会出现离子显示错误 - Ionic display error if gps is turned off 关闭GPS的位置查找方法 - A way to find location with GPS turned off
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM