位置监听器在android 5.1.1下不起作用

时间:2017-06-06 05:20:49

标签: android service gps location android-5.0-lollipop

我使用以下位置侦听器代码作为服务类。这段代码在所有设备上运行良好,但是当我隐藏从网络提供商处获取GPS的代码行时,它不适用于5.0及以下的设备。我不想使用网络提供的GPS,因为它在某些时候不是近似的。所以有人可以帮助我。我附上了以下代码。

public class MyLocationService extends Service 
{

private static final String TAG = "MyLocationService";
private LocationManager mLocationManager = null;
private static final int LOCATION_INTERVAL = 1000;

private static final float LOCATION_DISTANCE = 0f;
Context context = this;
PowerManager.WakeLock wakeLock;
public int e=0;

LocationListener mLocationListeners =
        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);
    //Toast.makeText(this, "MyService Started.", Toast.LENGTH_SHORT).show();

    wakeLock.acquire();
    //return START_STICKY;
    return START_NOT_STICKY;
}

@Override
public void onCreate() {
    Log.e(TAG, "onCreate");
  //  if(Store.ab.equalsIgnoreCase("0")) {
        initializeLocationManager();
        PowerManager mgr = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
        wakeLock = mgr.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "MyWakeLock");

//            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);
        } 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());
        }
       // Toast.makeText(this, "onCreated", Toast.LENGTH_SHORT).show();

}

@Override
public void onDestroy() {
    Log.e(TAG, "onDestroy");
    super.onDestroy();
    if (mLocationManager != null) {
            Log.e(TAG, "onDestroy1");
                mLocationManager.removeUpdates(mLocationListeners);
    }
    stopSelf();
    try{
        if (wakeLock.isHeld()){
            wakeLock.release();
            stopSelf();
        }

    }catch (RuntimeRemoteException e){
        e.printStackTrace();
    }
    Toast.makeText(MyLocationService.this,"destroyed", Toast.LENGTH_SHORT).show();
}
private void initializeLocationManager() {
    Log.e(TAG, "initializeLocationManager");
    if (mLocationManager == null) {
        mLocationManager = (LocationManager) getApplicationContext().getSystemService(Context.LOCATION_SERVICE);
    }
}

// create LocationListener class to get location updates
private class LocationListener implements android.location.LocationListener
{
    Location mLastLocation;
    double latitude;
    double longitude;
    LatLng latLngcurrent;


    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);
        latitude = location.getLatitude();
        longitude = location.getLongitude();
        Store.latu=latitude;
        Store.longu=longitude;
        Store.latLngcurrent = new LatLng(location.getLatitude(), location.getLongitude());
     //   Toast.makeText(context,"Location " + String.valueOf(e), Toast.LENGTH_SHORT).show();
        e++;
      //  Toast.makeText(MyLocationService.this,Store.latu+" "+Store.longu, Toast.LENGTH_SHORT).show();

    }

    @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);
    }
}
} 

我在代码中保留了断点,当隐藏网络提供程序时以及在5.0以下的设备上运行代码时。它甚至没有进入MyLocationService类。

注意:在此代码中,我隐藏了网络提供商代码。

0 个答案:

没有答案