Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/206.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 5.1.1下不工作_Android_Service_Gps_Location_Android 5.0 Lollipop - Fatal编程技术网

位置侦听器在android 5.1.1下不工作

位置侦听器在android 5.1.1下不工作,android,service,gps,location,android-5.0-lollipop,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 mLoca

我将以下位置侦听器代码用作服务类。这段代码在所有设备上都运行良好,但当我隐藏从网络提供商获取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类中

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