如何修复“ GPS无法获取位置”?

时间:2019-04-25 14:25:24

标签: android android-gps

我正在创建一个Android应用程序以获取我当前的GPS位置。首先它正在工作,但是现在它总是返回null。它只是在手机的通知栏中显示GPS图标,但该图标没有被激活或突出显示。我尝试了许多技巧,但到目前为止没有任何效果。这是我的代码:

final FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
    @SuppressLint("RestrictedApi")
    @Override
    public void onClick(View view) {

        fab.setVisibility(View.VISIBLE);

        GpsTracker gt = new GpsTracker(getApplicationContext());
        Location l = gt.getLocation();
        if(l != null) {

            double lat = l.getLatitude();
            double lon = l.getLongitude();
            Toast.makeText(getApplicationContext(),"GPS Lat = "+lat+"\n lon = "+lon,Toast.LENGTH_SHORT).show();
            Snackbar.make(view, "GPS Lat = "+lat+"\n lon = "+lon, Snackbar.LENGTH_LONG)
                    .setAction("Action", null).show();
            } else {
                Snackbar.make(view, "GPS unable to Connect", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
                Toast.makeText(getApplicationContext(),"GPS unable to Connect",Toast.LENGTH_SHORT).show();
            }
        }
    });

GpsTracker.java

import android.content.Context;
import android.content.pm.PackageManager;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.util.Log;
import static android.content.Context.LOCATION_SERVICE;

public class GpsTracker implements LocationListener {

    public Context context;

    public GpsTracker(Context context) {
        super();
        this.context = context;
    }

    public Location getLocation(){
        if (ContextCompat.checkSelfPermission(context, android.Manifest.permission.ACCESS_FINE_LOCATION ) != PackageManager.PERMISSION_GRANTED) {
            Log.e("fist","error");
            return null;
        }
        try {
            LocationManager lm = (LocationManager) context.getSystemService(LOCATION_SERVICE);
            boolean isGPSEnabled = lm.isProviderEnabled(LocationManager.GPS_PROVIDER);
            if (isGPSEnabled) {
                lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 6000,10,this);
                Location loc = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
                return loc;
            } else {
                Log.e("sec","Please enable your GPS");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public void onLocationChanged(Location location) {
    }

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {
    }

    @Override
    public void onProviderEnabled(String provider) {
    }

    @Override
    public void onProviderDisabled(String provider) {
    }
}   

AndroidManifest.xml

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />

2 个答案:

答案 0 :(得分:0)

您仅访问最近的已知位置。这通常是空的,或者对于您当前的位置来说是完全不正确的。您应该等到从onLocationChanged获得一些成功的读数后,再将值返回给调用方。

也许您应该将在其中初始化位置管理器的代码拆分为方法startLocationTracking()。然后,在您的代码中,在首次创建动作/片段时调用它。位置管理器获取onLocationChanged事件后,它将使用当前位置更新Public变量。然后,您的getLocation()方法将简单地将该currentLocation变量返回给调用方。然后,您应该有一个在Action / Fragment关闭时调用的stopLocationTracking()方法。

答案 1 :(得分:0)

哈桑,

您是否考虑过使用Google服务来检索自己的位置。我更喜欢使用它。下面找到一个示例,但是您将需要

  • 第一步

在Google API控制台上设置Google API密钥:https://console.developers.google.com/?pli=1

选择适用于Android的Maps SDK并启用它,然后在其中生成API密钥。

Google Developer Console

  • 第二步

将API密钥复制并粘贴到项目中的strings.xml文件中

strings.xml file in your project

  • 第三步

在您的app.gradle文件中添加以下依赖项

implementation 'com.google.android.gms:play-services-maps:16.1.0'
  • 第四步

在应用程序标记后的AndroidManifest.xml中添加以下内容

<meta-data
        android:name="com.google.android.gms.version"
        android:value="@integer/google_play_services_version" />
<meta-data
        android:name="com.google.android.geo.API_KEY"
        android:value="@string/google_maps_key" />

<meta-data
        android:name="com.google.android.maps.v2.API_KEY"
        android:value="@string/google_maps_key" />
  • 第五步

实施以下课程

public class MainActivity extends AppCompatActivity
        implements  OnMapReadyCallback,
        GoogleApiClient.ConnectionCallbacks,
        GoogleApiClient.OnConnectionFailedListener, LocationListener {

/*Declare the following Variables */
    private final static int PLAY_SERVICES_RESOLUTION_REQUEST = 1000;
    private boolean mRequestingLocationUpdates = false;
    private static int UPDATE_INTERVAL = 10000; // 10 sec
    private static int FATEST_INTERVAL = 5000; // 5 sec
    private static int DISPLACEMENT = 10; // 10 meters
    public GoogleApiClient mGoogleApiClient;
    LocationRequest mLocationRequest;
    Location mLastLocation;
    public static double lat = 0, lng = 0;
    LocationManager locationManager;
    Location location = null;
  • 第六步

在OnCreate函数之外创建以下函数

protected synchronized void buildGoogleApiClient() {
        mGoogleApiClient = new GoogleApiClient.Builder(this)
                .addConnectionCallbacks(this)
                .addOnConnectionFailedListener(this)
                .addApi(LocationServices.API)
                .build();
        mGoogleApiClient.connect();
        createLocationRequest();
    }

    protected void createLocationRequest() {
        mLocationRequest = new LocationRequest();
        mLocationRequest.setInterval(UPDATE_INTERVAL);
        mLocationRequest.setFastestInterval(FATEST_INTERVAL);
        mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
        mLocationRequest.setSmallestDisplacement(DISPLACEMENT);

    }

    @SuppressLint("MissingPermission")
    protected void startLocationUpdates() {
        try {
            LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, (LocationListener) this);
        } catch (Exception e) {
            Log.d("LOCATION_MSG", e.getMessage());
        }
    }

    protected void stopLocationUpdates() {
        LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, (LocationListener) this);
    }
  • 第七步

在onCreate中调用buildGoogleApiClient()

  • 第八步

覆盖方法

 @Override
    public void onConnected(@Nullable Bundle bundle) {
        mLocationRequest = new LocationRequest();
        mLocationRequest.setInterval(UPDATE_INTERVAL);
        mLocationRequest.setFastestInterval(FATEST_INTERVAL);
        mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
        mLocationRequest.setSmallestDisplacement(DISPLACEMENT);
    }

    @Override
    public void onConnectionSuspended(int i) {
        mGoogleApiClient.connect();
    }

    @Override
    public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {

    }

    @SuppressLint("MissingPermission")
    @Override
    public void onLocationChanged(Location location) {
        mLastLocation = location;
        mLastLocation = LocationServices.FusedLocationApi
                .getLastLocation(mGoogleApiClient);
    }

    @Override
    public void onMapReady(GoogleMap googleMap) {

    }
  • 第九步

在您的OnClick侦听器中:

fab.setOnClickListener

添加此

    mLastLocation = LocationServices.FusedLocationApi
            .getLastLocation(mGoogleApiClient);
    startLocationUpdates();

                    if (mLastLocation != null)

    {

        try {
            lat = mLastLocation.getLatitude();
            lng = mLastLocation.getLongitude();
        } catch (Exception e) {
            lat = 0;
            lng = 0;
        }
      /* Check your log cat for the following */
 Log.d("LOC_Latitude" , Double.toString(lat));
       Log.d("LOC_Longitude" , Double.toString(lng));


    }else{

    //Something is a miss, maybe the location service on your phone is not on, it could even be the internet connection.               
    }
  • 注意事项 确保对清单具有所有先决条件权限,某些设备需要在运行时请求权限。您可以使用以下方法来做到这一点

    / *在我们的onCreate函数之外声明此方法* /

        public static boolean hasPermissions(Context context, String... permissions) {
    
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && context != null && permissions != null) {
                for (String permission : permissions) {
                    if (ActivityCompat.checkSelfPermission(context, permission) != PackageManager.PERMISSION_GRANTED) {
                        return false;
                    }
                }
            }
            return true;
        }
    
    /*Add this in your onCreate Function */  
    
    
    
    String[] PERMISSIONS = {Manifest.permission.INTERNET, Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission.ACCESS_FINE_LOCATION};
    
        if (!hasPermissions(this, PERMISSIONS)) {
            ActivityCompat.requestPermissions(this, PERMISSIONS, 1);
        }
    

还请确保您对AndroidManifest.xml具有以下权限

<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />

尝试一下,并在任何情况下恢复原状。