Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/svg/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 即使GPS已关闭,也会调用onLocationChanged_Android_Gps_Location_Logcat - Fatal编程技术网

Android 即使GPS已关闭,也会调用onLocationChanged

Android 即使GPS已关闭,也会调用onLocationChanged,android,gps,location,logcat,Android,Gps,Location,Logcat,我一直在尝试获取当前用户位置。我打开GPS并启动应用程序。一切似乎都很好,我可以得到当前的用户位置 问题是当我关闭GPS并运行应用程序时onLocationChanged()。是否应该在GPS关闭的情况下调用它?若然,原因为何 package company.com.locationservicesapi; import android.app.Activity; import android.content.Context; import android.content.Intent; im

我一直在尝试获取当前用户位置。我打开GPS并启动应用程序。一切似乎都很好,我可以得到当前的用户位置

问题是当我关闭GPS并运行应用程序时<调用code>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");
    }
}
活动\位置\服务\ 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>

当有效的GPS信号可用时,
FusedLocationApi
不仅仅调用
onLocationChanged()
。它还使用WFP、单元ID和传感器获取有效位置。这就是FusedLocationApi的全部内容:使用设备的所有可能功能获取最准确的可用位置

如果您只想收听GPS信号,那么唯一的方法是使用带有标志LocationManager.GPS\u的
LocationManager
提供程序作为您的提供程序。

您的清单中有
,因此它将使用网络访问大致位置。看

您可以在每次没有任何连接时删除位置更新。只需声明
private LocationManager LocationManager第一个并放置

locationManager.removeUpdates(this);
this.locationManager = null;
public void onConnectionFailed(ConnectionResult ConnectionResult)


我希望这有帮助

不确定是否是这样,但您可以从手机网络中检索位置。它不是那么准确,但由于您同时包含了粗略和精细位置的权限,因此它可能是从那里获得的。如果我只有“android.permission.ACCESS\u fine\u location”给出了上述相同的行为。如果我只有“android.permission.ACCESS\u rough\u LOCATION”,应用程序会崩溃,但会出现一个异常:“java.lang.SecurityException:Provider gps需要访问\u FINE\u LOCATION权限”。它不是被弃用了吗?如果我没有遗漏什么,我已经读到当前的API来自Google Play服务。LocationManager不是,AFAICT永远不会被弃用。FusedLocationApi只不过是LocationManager的包装器(与之前的LocationClient一样,不推荐使用)。
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
locationManager.removeUpdates(this);
this.locationManager = null;