即使关闭GPS也会调用onLocationChanged

时间:2014-10-08 17:25:24

标签: android gps location logcat

我一直在努力获取当前的用户位置。我打开GPS并启动应用程序。一切似乎都没问题,我可以获得当前的用户位置。

问题是当我关闭GPS并运行应用程序时。调用onLocationChanged()。它应该在关闭GPS的情况下调用吗?如果是这样,为什么?

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:

<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:

<?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输出:

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

2 个答案:

答案 0 :(得分:0)

当有效的GPS信号可用时,FusedLocationApi不只是呼叫onLocationChanged()。它还使用WFPS,Cell-ID和Sensors来获取有效位置。这就是FusedLocationApi的全部内容:使用您设备的所有可能性获得最准确的位置。

如果您只是想收听GPS信号,那么唯一的方法就是使用LocationManager并将标志LocationManager.GPS_PROVIDER作为您的提供商。

答案 1 :(得分:0)

您的清单中有<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />,因此它会使用网络访问大致位置。见android manifest description

每次没有任何连接时,您都可以删除位置更新。只需先声明private LocationManager locationManager;并输入

locationManager.removeUpdates(this);
this.locationManager = null;

on public void onConnectionFailed(ConnectionResult connectionResult)

我希望这有帮助