onLocationChanged()不会在服务中自动调用

时间:2018-02-28 11:22:07

标签: android android-gps

写一个Service类来获取该位置,它正在某些设备上获取该位置,如三星s7边缘,而无法在三星j5等设备上获取位置。我只是想知道它是代码问题还是移动特定问题。以下代码是用于获取位置的Service类:

    public class LocationService extends Service {
    private static final String TAG = "myService";
    private LocationManager mLocationManager = null;
    private static final int LOCATION_INTERVAL = 1000;
    private static final float LOCATION_DISTANCE = 50f;
    boolean inside;
    TelephonyManager tMgr;
    String mPhoneNumber;

    private class MyLocationListener implements 
    android.location.LocationListener {
        Location mLastLocation;

        public MyLocationListener(String provider) {
            Log.d(TAG, "LocationListener " + provider);
            mLastLocation = new Location(provider);
        }

        @Override
        public void onLocationChanged(Location location) {
            Log.d("myLocation", "onLocationChanged: " + location);
            Location locationA = new Location("point A");
            locationA.setLatitude(33.65340376);
            locationA.setLongitude(73.03382854);
            float distance = locationA.distanceTo(location);

            Log.e(TAG, "Total distance: " + distance);
            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);
        }
    }

    MyLocationListener[] mLocationListeners = new MyLocationListener[]{
            new MyLocationListener(LocationManager.GPS_PROVIDER),
            new MyLocationListener(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() {
        DatabaseHandler db = new DatabaseHandler(getApplicationContext());
        int lastStatus = db.getLastTimeStatus();
        if (lastStatus == 0) {
            inside = true;
        } else if (lastStatus == 1) {
            inside = false;
        }
        tMgr = (TelephonyManager) getApplicationContext().getSystemService(Context.TELEPHONY_SERVICE);
        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_SMS) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED) {

            return;
        }
        mPhoneNumber = tMgr.getLine1Number();

        Log.e(TAG, "onCreate");
        initializeLocationManager();

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

onProviderEnabled()等其他所有方法都可以正常工作,但onlocationChanged()大部分时间都没有选择用户的当前位置,但有时会突然同时触发4,5位置。

0 个答案:

没有答案