7

我的应用程序使用两个单独Process的 . 我在另一个中拥有所有Services组件Process和所有其他组件Process。当我试图从接收到一个 with 动作时获取 lastKnownLocation时LocationMangerIntent我得到null ,当我在调试器中看到对象显示它时。感谢您的帮助。这是代码片段-public int onStartCommand(Intent intent, int flags, int startId)ActivityLocationLocationManagerContext has changed, operation is not possible在此处输入图像描述

@Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        String action = intent.getAction();
        if (action != null) {
            if (action.equalsIgnoreCase(Synchronize.ACTION_UPDATE_LOCATION)) {
                Location location = getLastKnownLocation();
                if (location != null) {
                    new Synchronize(getApplicationContext()).
                            updateLocation(prefs.getInt(Utils.PROPERTY_USER_ID, 0),
                                    location.getLatitude(), location.getLongitude(),
                                    Utils.formatDateMilliSecondToUTC(location.getTime()));
                }
            } else if (action.equalsIgnoreCase(Synchronize.ACTION_SYNC_CONTACTS)) {
                new Synchronize(getApplicationContext()).syncContacts();
            } else if (action.equalsIgnoreCase(Synchronize.ACTION_SYNC_EVENTS)) {
                new Synchronize(getApplicationContext()).syncEvents();
            } else if (action.equalsIgnoreCase(Synchronize.ACTION_SYNC_OFFERS)) {
                new Synchronize(getApplicationContext()).syncOffers();
            } else if (action.equalsIgnoreCase(Synchronize.ACTION_SYNC_DISCOVERS)) {
                new Synchronize(getApplicationContext()).syncDiscoverEvents();
            }
        }
        return START_STICKY;
    }

    private Location getLastKnownLocation() {
        LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        if (Build.VERSION.SDK_INT > Build.VERSION_CODES.LOLLIPOP_MR1) {
            if (checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED &&
                    checkSelfPermission(Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
                return locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
            }
        } else {
            return locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
        }
        return null;
    }
4

0 回答 0