Java Code Examples for android.location.Criteria#setBearingRequired()

The following examples show how to use android.location.Criteria#setBearingRequired() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: RawLocationProvider.java    From background-geolocation-android with Apache License 2.0 6 votes vote down vote up
@Override
public void onStart() {
    if (isStarted) {
        return;
    }

    Criteria criteria = new Criteria();
    criteria.setAltitudeRequired(false);
    criteria.setBearingRequired(false);
    criteria.setSpeedRequired(true);
    criteria.setCostAllowed(true);
    criteria.setAccuracy(Criteria.ACCURACY_FINE);
    criteria.setHorizontalAccuracy(translateDesiredAccuracy(mConfig.getDesiredAccuracy()));
    criteria.setPowerRequirement(Criteria.POWER_HIGH);

    try {
        locationManager.requestLocationUpdates(locationManager.getBestProvider(criteria, true), mConfig.getInterval(), mConfig.getDistanceFilter(), this);
        isStarted = true;
    } catch (SecurityException e) {
        logger.error("Security exception: {}", e.getMessage());
        this.handleSecurityException(e);
    }
}
 
Example 2
Source File: LocationUtils.java    From Android-UtilCode with Apache License 2.0 6 votes vote down vote up
/**
 * 设置定位参数
 *
 * @return {@link Criteria}
 */
private static Criteria getCriteria() {
    Criteria criteria = new Criteria();
    // 设置定位精确度 Criteria.ACCURACY_COARSE比较粗略,Criteria.ACCURACY_FINE则比较精细
    criteria.setAccuracy(Criteria.ACCURACY_FINE);
    // 设置是否要求速度
    criteria.setSpeedRequired(false);
    // 设置是否允许运营商收费
    criteria.setCostAllowed(false);
    // 设置是否需要方位信息
    criteria.setBearingRequired(false);
    // 设置是否需要海拔信息
    criteria.setAltitudeRequired(false);
    // 设置对电源的需求
    criteria.setPowerRequirement(Criteria.POWER_LOW);
    return criteria;
}
 
Example 3
Source File: RxLocationTool.java    From RxTools-master with Apache License 2.0 6 votes vote down vote up
/**
 * 设置定位参数
 *
 * @return {@link Criteria}
 */
private static Criteria getCriteria() {
    Criteria criteria = new Criteria();
    //设置定位精确度 Criteria.ACCURACY_COARSE比较粗略,Criteria.ACCURACY_FINE则比较精细
    criteria.setAccuracy(Criteria.ACCURACY_FINE);
    //设置是否要求速度
    criteria.setSpeedRequired(false);
    // 设置是否允许运营商收费
    criteria.setCostAllowed(false);
    //设置是否需要方位信息
    criteria.setBearingRequired(false);
    //设置是否需要海拔信息
    criteria.setAltitudeRequired(false);
    // 设置对电源的需求
    criteria.setPowerRequirement(Criteria.POWER_LOW);
    return criteria;
}
 
Example 4
Source File: Geolocation.java    From OsmGo with MIT License 6 votes vote down vote up
/**
 * Given the call's options, return a Criteria object
 * that will indicate which location provider we need to use.
 * @param call
 * @return
 */
private Criteria getCriteriaForCall(PluginCall call) {
  boolean enableHighAccuracy = call.getBoolean("enableHighAccuracy", false);
  boolean altitudeRequired = call.getBoolean("altitudeRequired", false);
  boolean speedRequired = call.getBoolean("speedRequired", false);
  boolean bearingRequired = call.getBoolean("bearingRequired", false);

  int timeout = call.getInt("timeout", 30000);
  int maximumAge = call.getInt("maximumAge", 0);

  Criteria c = new Criteria();
  c.setAccuracy(enableHighAccuracy ? Criteria.ACCURACY_FINE : Criteria.ACCURACY_COARSE);
  c.setAltitudeRequired(altitudeRequired);
  c.setBearingRequired(bearingRequired);
  c.setSpeedRequired(speedRequired);
  return c;
}
 
Example 5
Source File: LocationUtils.java    From AndroidUtilCode with Apache License 2.0 6 votes vote down vote up
/**
 * 设置定位参数
 *
 * @return {@link Criteria}
 */
private static Criteria getCriteria() {
    Criteria criteria = new Criteria();
    // 设置定位精确度 Criteria.ACCURACY_COARSE比较粗略,Criteria.ACCURACY_FINE则比较精细
    criteria.setAccuracy(Criteria.ACCURACY_FINE);
    // 设置是否要求速度
    criteria.setSpeedRequired(false);
    // 设置是否允许运营商收费
    criteria.setCostAllowed(false);
    // 设置是否需要方位信息
    criteria.setBearingRequired(false);
    // 设置是否需要海拔信息
    criteria.setAltitudeRequired(false);
    // 设置对电源的需求
    criteria.setPowerRequirement(Criteria.POWER_LOW);
    return criteria;
}
 
Example 6
Source File: GPSTracker.java    From AsteroidOSSync with GNU General Public License v3.0 6 votes vote down vote up
public void updateLocation() {
    try {
        mLocationManager = (LocationManager) mContext
                .getSystemService(LOCATION_SERVICE);

        Criteria criteria = new Criteria();
        criteria.setAccuracy(Criteria.ACCURACY_LOW);
        criteria.setSpeedRequired(false);
        criteria.setAltitudeRequired(false);
        criteria.setBearingRequired(false);
        criteria.setCostAllowed(true);
        criteria.setPowerRequirement(Criteria.POWER_LOW);
        String provider = mLocationManager.getBestProvider(criteria, true);
        if(provider != null) {
            mLocationManager.requestSingleUpdate(provider, this, null);
            mCanGetLocation = true;
        }
    }
    catch (SecurityException | NullPointerException e) {
        e.printStackTrace();
    }
}
 
Example 7
Source File: LocationUtils.java    From DevUtils with Apache License 2.0 6 votes vote down vote up
/**
 * 获取定位参数对象
 * @return {@link Criteria}
 */
private static Criteria getCriteria() {
    Criteria criteria = new Criteria();
    // 设置定位精确度 Criteria.ACCURACY_COARSE 比较粗略, Criteria.ACCURACY_FINE 则比较精细
    criteria.setAccuracy(Criteria.ACCURACY_FINE);
    // 设置是否要求速度
    criteria.setSpeedRequired(false);
    // 设置是否允许运营商收费
    criteria.setCostAllowed(false);
    // 设置是否需要方位信息
    criteria.setBearingRequired(false);
    // 设置是否需要海拔信息
    criteria.setAltitudeRequired(false);
    // 设置对电源的需求
    criteria.setPowerRequirement(Criteria.POWER_LOW);
    return criteria;
}
 
Example 8
Source File: MapLocationUtil.java    From lunzi with Apache License 2.0 6 votes vote down vote up
private String getProvider(LocationManager locationManager) {
    // 构建位置查询条件
    Criteria criteria = new Criteria();
    // 查询精度:高
    criteria.setAccuracy(Criteria.ACCURACY_FINE);
    // 速度
    criteria.setSpeedRequired(false);
    // 是否查询海拨:否
    criteria.setAltitudeRequired(false);
    // 是否查询方位角:否
    criteria.setBearingRequired(false);
    // 电量要求:低
    criteria.setPowerRequirement(Criteria.POWER_LOW);
    // 返回最合适的符合条件的provider,第2个参数为true说明,如果只有一个provider是有效的,则返回当前provider
    return locationManager.getBestProvider(criteria, true);
}
 
Example 9
Source File: LocationUtils.java    From Easer with GNU General Public License v3.0 5 votes vote down vote up
static Criteria getCriteria() {
    Criteria criteria = new Criteria();
    criteria.setAltitudeRequired(false);
    criteria.setBearingRequired(false);
    criteria.setCostAllowed(false);
    criteria.setSpeedRequired(false);
    criteria.setAccuracy(Criteria.NO_REQUIREMENT);
    return criteria;
}
 
Example 10
Source File: OrientationManager.java    From PTVGlass with MIT License 5 votes vote down vote up
/**
 * Initializes a new instance of {@code OrientationManager}, using the specified context to
 * access system services.
 */
public OrientationManager(SensorManager sensorManager, LocationManager locationManager) {
    mRotationMatrix = new float[16];
    mOrientation = new float[9];
    mSensorManager = sensorManager;
    mLocationManager = locationManager;
    mListeners = new LinkedHashSet<OnChangedListener>();

    Criteria criteria = new Criteria();
    criteria.setAccuracy(Criteria.ACCURACY_FINE);
    criteria.setBearingRequired(false);
    criteria.setSpeedRequired(false);

    mLocationProvider = mLocationManager.getBestProvider(criteria, true /* enabledOnly */);
}
 
Example 11
Source File: OrientationManager.java    From PTVGlass with MIT License 5 votes vote down vote up
/**
 * Starts tracking the user's location and orientation. After calling this method, any
 * {@link OnChangedListener}s added to this object will be notified of these events.
 */
public void start() {
    if (!mTracking) {
        mSensorManager.registerListener(mSensorListener,
                mSensorManager.getDefaultSensor(Sensor.TYPE_ROTATION_VECTOR),
                SensorManager.SENSOR_DELAY_UI);

        // The rotation vector sensor doesn't give us accuracy updates, so we observe the
        // magnetic field sensor solely for those.
        mSensorManager.registerListener(mSensorListener,
                mSensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD),
                SensorManager.SENSOR_DELAY_UI);

        Location lastLocation = mLocationManager
                .getLastKnownLocation(LocationManager.PASSIVE_PROVIDER);
        if (lastLocation != null) {
            long locationAge = lastLocation.getTime() - System.currentTimeMillis();
            if (locationAge < MAX_LOCATION_AGE_MILLIS) {
                mLocation = lastLocation;
                updateGeomagneticField();
            }
        }

        Criteria criteria = new Criteria();
        criteria.setAccuracy(Criteria.ACCURACY_FINE);
        criteria.setBearingRequired(false);
        criteria.setSpeedRequired(false);

        List<String> providers =
                mLocationManager.getProviders(criteria, true /* enabledOnly */);
        for (String provider : providers) {
            mLocationManager.requestLocationUpdates(provider,
                    MILLIS_BETWEEN_LOCATIONS, METERS_BETWEEN_LOCATIONS, mLocationListener,
                    Looper.getMainLooper());
        }

        mTracking = true;
    }
}
 
Example 12
Source File: OrientationManager.java    From SpeedHud with Apache License 2.0 5 votes vote down vote up
/**
 * Starts tracking the user's location and orientation. After calling this method, any
 * {@link OnChangedListener}s added to this object will be notified of these events.
 */
public void start() {
    if (!mTracking) {
        mSensorManager.registerListener(mSensorListener,
                mSensorManager.getDefaultSensor(Sensor.TYPE_ROTATION_VECTOR),
                SensorManager.SENSOR_DELAY_UI);

        // The rotation vector sensor doesn't give us accuracy updates, so we observe the
        // magnetic field sensor solely for those.
        mSensorManager.registerListener(mSensorListener,
                mSensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD),
                SensorManager.SENSOR_DELAY_UI);

        Location lastLocation = mLocationManager
                .getLastKnownLocation(LocationManager.PASSIVE_PROVIDER);
        if (lastLocation != null) {
            long locationAge = lastLocation.getTime() - System.currentTimeMillis();
            if (locationAge < MAX_LOCATION_AGE_MILLIS) {
                mLocation = lastLocation;
                updateGeomagneticField();
            }
        }

        Criteria criteria = new Criteria();
        criteria.setAccuracy(Criteria.ACCURACY_FINE);
        criteria.setBearingRequired(false);
        criteria.setSpeedRequired(false);

        List<String> providers =
                mLocationManager.getProviders(criteria, true /* enabledOnly */);
        for (String provider : providers) {
            mLocationManager.requestLocationUpdates(provider,                        MILLIS_BETWEEN_LOCATIONS, METERS_BETWEEN_LOCATIONS, mLocationListener,
            Looper.getMainLooper());
        }

        mTracking = true;
    }
}
 
Example 13
Source File: LocationUtils.java    From iot-starter-for-android with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Helper method to create a criteria for location change listener
 *
 * @return criteria constructed for the listener
 */
private Criteria getCriteria() {
    Criteria criteria = new Criteria();
    criteria.setPowerRequirement(Criteria.POWER_LOW);
    criteria.setAccuracy(Criteria.ACCURACY_FINE);
    criteria.setAltitudeRequired(false);
    criteria.setBearingRequired(true);
    criteria.setCostAllowed(true);
    criteria.setSpeedRequired(true);
    return criteria;
}
 
Example 14
Source File: LocationHelper.java    From RxAndroidBootstrap with Apache License 2.0 5 votes vote down vote up
/**
 * Returns best location using LocationManager.getBestProvider()
 *
 * @param context
 * @return Location|null
 */
public static Location getLocation(Context context) {

    Log.d("LocationHelper", "getLocation()");


    // fetch last known location and update it
    try {
        LocationManager lm = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);

        Criteria criteria = new Criteria();
        criteria.setAccuracy(Criteria.ACCURACY_FINE);
        criteria.setAltitudeRequired(false);
        criteria.setBearingRequired(false);
        criteria.setCostAllowed(true);
        String strLocationProvider = lm.getBestProvider(criteria, true);


        Log.d("LocationHelper", "strLocationProvider=" + strLocationProvider);

        Location location = getLastKnownLocation(context);//lm.getLastKnownLocation(strLocationProvider);
        if (location != null) {
            return location;
        }
        return null;
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
}
 
Example 15
Source File: BackgroundLocationUpdateService.java    From cordova-background-geolocation-services with Apache License 2.0 5 votes vote down vote up
@Override
public void onCreate() {
    super.onCreate();
    Log.i(TAG, "OnCreate");

    toneGenerator           = new ToneGenerator(AudioManager.STREAM_NOTIFICATION, 100);
    notificationManager     = (NotificationManager)this.getSystemService(Context.NOTIFICATION_SERVICE);
    connectivityManager     = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);

    // Location Update PI
    Intent locationUpdateIntent = new Intent(Constants.LOCATION_UPDATE);
    locationUpdatePI = PendingIntent.getBroadcast(this, 9001, locationUpdateIntent, PendingIntent.FLAG_UPDATE_CURRENT);
    registerReceiver(locationUpdateReceiver, new IntentFilter(Constants.LOCATION_UPDATE));

    Intent detectedActivitiesIntent = new Intent(Constants.DETECTED_ACTIVITY_UPDATE);
    detectedActivitiesPI = PendingIntent.getBroadcast(this, 9002, detectedActivitiesIntent, PendingIntent.FLAG_UPDATE_CURRENT);
    registerReceiver(detectedActivitiesReceiver, new IntentFilter(Constants.DETECTED_ACTIVITY_UPDATE));

    // Receivers for start/stop recording
    registerReceiver(startRecordingReceiver, new IntentFilter(Constants.START_RECORDING));
    registerReceiver(stopRecordingReceiver, new IntentFilter(Constants.STOP_RECORDING));
    registerReceiver(startAggressiveReceiver, new IntentFilter(Constants.CHANGE_AGGRESSIVE));

    // Location criteria
    criteria = new Criteria();
    criteria.setAltitudeRequired(false);
    criteria.setBearingRequired(false);
    criteria.setSpeedRequired(true);
    criteria.setCostAllowed(true);
}
 
Example 16
Source File: LocationHelper.java    From MVPAndroidBootstrap with Apache License 2.0 5 votes vote down vote up
/**
 * Returns best location using LocationManager.getBestProvider()
 *
 * @param context
 * @return Location|null
 */
public static Location getLocation(Context context) {

    Log.d("LocationHelper", "getLocation()");


    // fetch last known location and update it
    try {
        LocationManager lm = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);

        Criteria criteria = new Criteria();
        criteria.setAccuracy(Criteria.ACCURACY_FINE);
        criteria.setAltitudeRequired(false);
        criteria.setBearingRequired(false);
        criteria.setCostAllowed(true);
        String strLocationProvider = lm.getBestProvider(criteria, true);


        Log.d("LocationHelper", "strLocationProvider=" + strLocationProvider);

        Location location = getLastKnownLocation(context);//lm.getLastKnownLocation(strLocationProvider);
        if (location != null) {
            return location;
        }
        return null;
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
}
 
Example 17
Source File: ShareUtil.java    From Huochexing12306 with Apache License 2.0 5 votes vote down vote up
public Location getLocation(Activity activity)
{
    LocationManager locationManager = (LocationManager) activity.getSystemService(Context.LOCATION_SERVICE);
    // 查找到服务信息
    Criteria criteria = new Criteria();
    criteria.setAccuracy(Criteria.ACCURACY_FINE); // 高精度
    criteria.setAltitudeRequired(false);
    criteria.setBearingRequired(false);
    criteria.setCostAllowed(true);
    criteria.setPowerRequirement(Criteria.POWER_LOW); // 低功耗

    String provider = locationManager.getBestProvider(criteria, false); // 获取GPS信息
    Location location = locationManager.getLastKnownLocation(provider); // 通过GPS获取位置
    return location;
}
 
Example 18
Source File: DistanceFilterLocationProvider.java    From background-geolocation-android with Apache License 2.0 5 votes vote down vote up
@Override
public void onCreate() {
    super.onCreate();

    locationManager = (LocationManager) mContext.getSystemService(Context.LOCATION_SERVICE);
    alarmManager = (AlarmManager) mContext.getSystemService(Context.ALARM_SERVICE);

    // Stop-detection PI
    stationaryAlarmPI = PendingIntent.getBroadcast(mContext, 0, new Intent(STATIONARY_ALARM_ACTION), 0);
    registerReceiver(stationaryAlarmReceiver, new IntentFilter(STATIONARY_ALARM_ACTION));

    // Stationary region PI
    stationaryRegionPI = PendingIntent.getBroadcast(mContext, 0, new Intent(STATIONARY_REGION_ACTION), PendingIntent.FLAG_CANCEL_CURRENT);
    registerReceiver(stationaryRegionReceiver, new IntentFilter(STATIONARY_REGION_ACTION));

    // Stationary location monitor PI
    stationaryLocationPollingPI = PendingIntent.getBroadcast(mContext, 0, new Intent(STATIONARY_LOCATION_MONITOR_ACTION), 0);
    registerReceiver(stationaryLocationMonitorReceiver, new IntentFilter(STATIONARY_LOCATION_MONITOR_ACTION));

    // One-shot PI (TODO currently unused)
    singleUpdatePI = PendingIntent.getBroadcast(mContext, 0, new Intent(SINGLE_LOCATION_UPDATE_ACTION), PendingIntent.FLAG_CANCEL_CURRENT);
    registerReceiver(singleUpdateReceiver, new IntentFilter(SINGLE_LOCATION_UPDATE_ACTION));

    // Location criteria
    criteria = new Criteria();
    criteria.setAltitudeRequired(false);
    criteria.setBearingRequired(false);
    criteria.setSpeedRequired(true);
    criteria.setCostAllowed(true);
}
 
Example 19
Source File: MainActivity.java    From nearbydemo with Eclipse Public License 1.0 4 votes vote down vote up
public void getLocation(Context context) {
	LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
	// 设置Criteria的信息
	Criteria criteria = new Criteria();
	// 经度要求
	criteria.setAccuracy(Criteria.ACCURACY_FINE);
	criteria.setAltitudeRequired(false);
	criteria.setBearingRequired(false);
	criteria.setCostAllowed(false);
	criteria.setPowerRequirement(Criteria.POWER_LOW);
	// 根据设置的Criteria对象,获取最符合此标准的provider对象
	// 取得效果做好的criteria
	String currentProvider = locationManager.getBestProvider(criteria, true);
	Log.d(TAG, "currentProvider: " + currentProvider);
	// 根据当前provider对象获取最后一次位置信息
	Location currentLocation = locationManager.getLastKnownLocation(currentProvider);
	if (currentLocation == null) {
		locationManager.requestLocationUpdates(currentProvider, 0, 0, new getGpsLocationListner());
	}
	// 直到获得最后一次位置信息为止,如果未获得最后一次位置信息,则显示默认经纬度
	// 每隔10秒获取一次位置信息
	while (true) {
		currentLocation = locationManager.getLastKnownLocation(currentProvider);
		if (currentLocation != null) {
			Log.d(TAG, "Latitude: " + currentLocation.getLatitude());
			Log.d(TAG, "location: " + currentLocation.getLongitude());
			Map<String, String> map = new HashMap<String, String>();
			map.put("latitude", String.valueOf(currentLocation.getLatitude()));
			map.put("longitude", String.valueOf(currentLocation.getLongitude()));
			map.put("user_id", PhoneUtil.getImei(MainActivity.this));
			requestServer(map);
			break;
		} else {
			Log.d(TAG, "Latitude: " + 0);
			Log.d(TAG, "location: " + 0);
		}
		try {
			Thread.sleep(10000);
		} catch (InterruptedException e) {
			Log.e(TAG, e.getMessage());
		}
	}
}