找出我的位置是如何收集的。强制关闭Location.getProvider();

时间:2014-02-24 17:44:11

标签: android gps location

当GPS关闭时,我需要检查GPS服务从哪里获取提供商。我把它设置为Go GPS>网络>默认我假设是wifi。

不久前,当我将Nexus 5上的位置设置设置为省电时,它无法在Google地图或我的应用中找到我的位置。尽管它清楚地说明了网络/ Wifi。我已经把它改回高精度,仍然是一个力量关闭。

无论如何,有谁知道如何找到我的位置来自哪里,GPS,网络或Wifi?首先,手机没有网络(没有SIM卡),所以就这样了。

我在尝试执行location.getProvider()时不断关闭力量。我假设那是因为它返回null。位于下方。

if (LocationManager.GPS_PROVIDER.equals(location.getProvider())) {
            Log.i("TAG", "GPS");    
        } else if (LocationManager.NETWORK_PROVIDER.equals(location.getProvider())) {
            Log.i("TAG", "NETWORK");
        }
        else {
            Log.i("TAG", "NEITHER");
}

02-24 17:26:27.016: E/AndroidRuntime(3395): Process: com.package.name, PID: 3395
02-24 17:26:27.016: E/AndroidRuntime(3395): java.lang.NullPointerException
02-24 17:26:27.016: E/AndroidRuntime(3395):     at com.package.name.MapsPage$runGet.onPostExecute(MapsPage.java:535)
02-24 17:26:27.016: E/AndroidRuntime(3395):     at com.package.name.MapsPage$runGet.onPostExecute(MapsPage.java:1)
02-24 17:26:27.016: E/AndroidRuntime(3395):     at android.os.AsyncTask.finish(AsyncTask.java:632)
02-24 17:26:27.016: E/AndroidRuntime(3395):     at android.os.AsyncTask.access$600(AsyncTask.java:177)
02-24 17:26:27.016: E/AndroidRuntime(3395):     at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:645)
02-24 17:26:27.016: E/AndroidRuntime(3395):     at android.os.Handler.dispatchMessage(Handler.java:102)
02-24 17:26:27.016: E/AndroidRuntime(3395):     at android.os.Looper.loop(Looper.java:136)
02-24 17:26:27.016: E/AndroidRuntime(3395):     at android.app.ActivityThread.main(ActivityThread.java:5017)
02-24 17:26:27.016: E/AndroidRuntime(3395):     at java.lang.reflect.Method.invokeNative(Native Method)
02-24 17:26:27.016: E/AndroidRuntime(3395):     at java.lang.reflect.Method.invoke(Method.java:515)
02-24 17:26:27.016: E/AndroidRuntime(3395):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
02-24 17:26:27.016: E/AndroidRuntime(3395):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
02-24 17:26:27.016: E/AndroidRuntime(3395):     at dalvik.system.NativeStart.main(Native Method)

正如您所看到的那样,我正在服务器调用的onPostExecute中运行它作为第一件事。

有人有什么想法吗?是否有更好的方式来调用它?我正在启动GPS探测器作为服务,如果它失败则会寻找网络,如果它失败则转到Wifi(或者至少我认为); logcat几乎没有提供任何信息。

非常感谢对此有所帮助,谢谢!

修改

public class MapsPage extends Fragment implements LocationListener {
    @Override
public void onLocationChanged(Location location) {
}

public void onProviderDisabled(String provider) {
    Log.d("TAG", "onProviderDisabled: " + provider);
}

public void onProviderEnabled(String provider) {
    Log.d("TAG", "onProviderEnabled: " + provider);
}

public void onStatusChanged(String provider, int status, Bundle extras) {
    Log.d("TAG", "onStatusChanged: " + provider);
}

/*LocationListener[] mLocationListeners = new LocationListener[] {
        new LocationListener(LocationManager.GPS_PROVIDER),
        new LocationListener(LocationManager.NETWORK_PROVIDER)
};*/
}

由于我遇到的错误,请在下面注释掉。

1 个答案:

答案 0 :(得分:1)

早上好,这是来自gps提供商的本地方法,您可以在项目中实现它们......

     @Override
    public void onProviderDisabled(String provider)
    {
        Log.e(TAG, "onProviderDisabled: " + provider);
    }
    @Override
    public void onProviderEnabled(String provider)
    {
        Log.e(TAG, "onProviderEnabled: " + provider);
    }
    @Override
    public void onStatusChanged(String provider, int status, Bundle extras)
    {
        Log.e(TAG, "onStatusChanged: " + provider);// here you will see gps or network in your logcat
    }
}
LocationListener[] mLocationListeners = new LocationListener[] {
        new LocationListener(LocationManager.GPS_PROVIDER),
        new LocationListener(LocationManager.NETWORK_PROVIDER)
};

02-24 11:28:29.722  16488-16488/com.solmoviles.tmp E/BOOMBOOMTESTGPS﹕ onProviderDisabled: gps

02-24 11:28:29.790 16488-16488 / com.solmoviles.tmp E / BOOMBOOMTESTGPS:onProviderDisabled:network 02-24 11:28:29.790 16488-16488 / com.solmoviles.tmp E / BOOMBOOMTESTGPS:onProviderDisabled:gps

编辑这是我获取位置的类的完整代码

private class LocationListener implements android.location.LocationListener{
    Location mLastLocation;
    public LocationListener(String provider)
    {
        Log.e(TAG, "LocationListener " + provider);
        mLastLocation = new Location(provider);
    }
    @Override
    public void onLocationChanged(Location location)
    {
        Log.e(TAG, "onLocationChanged: " + location);
        mLastLocation.set(location);
    }
        @Override
    public void onProviderDisabled(String provider)
    {
        Log.e(TAG, "onProviderDisabled: " + provider);
    }
    @Override
    public void onProviderEnabled(String provider)
    {
        Log.e(TAG, "onProviderEnabled: " + provider);
    }
    @Override
    public void onStatusChanged(String provider, int status, Bundle extras)
    {
        Log.e(TAG, "onStatusChanged: " + provider);
    }
}
LocationListener[] mLocationListeners = new LocationListener[] {
        new LocationListener(LocationManager.GPS_PROVIDER),
        new LocationListener(LocationManager.NETWORK_PROVIDER)
};
@Override
public IBinder onBind(Intent arg0)
{
    return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId)
{
    Log.e(TAG, "onStartCommand");
    super.onStartCommand(intent, flags, startId);
    return START_STICKY;
}
@Override
public void onCreate()
{

    Log.e(TAG, "onCreate");
    initializeLocationManager();
    try {
        mLocationManager.requestLocationUpdates(
                LocationManager.NETWORK_PROVIDER, LOCATION_INTERVAL, LOCATION_DISTANCE,
                mLocationListeners[1]);
    } catch (java.lang.SecurityException ex) {
        Log.i(TAG, "fail to request location update, ignore", ex);
    } catch (IllegalArgumentException ex) {
        Log.d(TAG, "network provider does not exist, " + ex.getMessage());
    }
    try {
        mLocationManager.requestLocationUpdates(
                LocationManager.GPS_PROVIDER, LOCATION_INTERVAL, LOCATION_DISTANCE,
                mLocationListeners[0]);
    } catch (java.lang.SecurityException ex) {
        Log.i(TAG, "fail to request location update, ignore", ex);
    } catch (IllegalArgumentException ex) {
        Log.d(TAG, "gps provider does not exist " + ex.getMessage());
    }
}
@Override
public void onDestroy()
{
    Log.e(TAG, "onDestroy");
    super.onDestroy();
    if (mLocationManager != null) {
        for (int i = 0; i < mLocationListeners.length; i++) {
            try {
                mLocationManager.removeUpdates(mLocationListeners[i]);
            } catch (Exception ex) {
                Log.i(TAG, "fail to remove location listners, ignore", ex);
            }
        }
    }
}
private void initializeLocationManager() {
    Log.e(TAG, "initializeLocationManager");
    if (mLocationManager == null) {
        mLocationManager = (LocationManager) getApplicationContext().getSystemService(Context.LOCATION_SERVICE);
    }
}