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

The following examples show how to use android.location.Criteria#setCostAllowed() . 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: 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 2
Source File: GPSTesterActivityController.java    From android-gps-test-tool with Apache License 2.0 6 votes vote down vote up
/**
 * Let's you test scenarios based on various criteria settings. Does <b>not</b> currently include
 * all criteria from: http://developer.android.com/reference/android/location/Criteria.html. 
 * You can view the results of your settings changes in the Best Provider window at the
 * bottom of the main activity screen.<br><br>
 * <b>NOTE:</b> This does <b>override</b> how the default application operates.  
 */
private String getBestProviderNameViaCriteria(){
    final int power = Integer.parseInt(_preferences.getString("pref_key_setPower", "1"));
    final int accuracy = Integer.parseInt(_preferences.getString("pref_key_setAccuracy", "1"));	    
    final boolean cost = Boolean.valueOf(_preferences.getString("pref_key_setCost", "true"));
    	    
    Criteria criteria = new Criteria();
    criteria.setAccuracy(accuracy);
    criteria.setCostAllowed(cost);
    criteria.setPowerRequirement(power);
    
    String finalBestProvider = "<b><font color='yellow'>Best Provider (via Criteria)</font></b><br>";
    String bestProviderName = _locationManager.getBestProvider(criteria, true);
    
    if(bestProviderName != null){
    	_bestLocationProviderTextView.setText(Html.fromHtml(finalBestProvider + bestProviderName));	    
    }
    else{
    	_bestLocationProviderTextView.setText(Html.fromHtml(finalBestProvider + "N/A"));
    }
    
    return bestProviderName;
}
 
Example 3
Source File: Preferences.java    From al-muazzin with GNU Lesser General Public License v2.1 6 votes vote down vote up
public Location getCurrentLocation(Context context) {
    Criteria criteria = new Criteria();
    criteria.setAccuracy(Criteria.ACCURACY_FINE);
    criteria.setCostAllowed(true);

    LocationManager locationManager = (LocationManager) context
            .getSystemService(Context.LOCATION_SERVICE);
    try {
        Location currentLocation = locationManager.getLastKnownLocation(locationManager
                .getBestProvider(criteria, true));
        if (currentLocation == null) {
            criteria.setAccuracy(Criteria.ACCURACY_COARSE);
            currentLocation = locationManager.getLastKnownLocation(locationManager
                    .getBestProvider(criteria, true));
        }
        return currentLocation;
    } catch (IllegalArgumentException iae) {
        return null;
    }
}
 
Example 4
Source File: GpsTraceService.java    From MobileGuard with MIT License 6 votes vote down vote up
@Override
public void onCreate() {
    super.onCreate();

    // locate
    locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    // check permission
    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
        return;
    }

    // get best provider
    Criteria criteria = new Criteria();
    criteria.setCostAllowed(true);
    criteria.setAccuracy(Criteria.ACCURACY_FINE);
    String bestProvider = locationManager.getBestProvider(criteria, true);
    System.out.println("best provider:" + bestProvider);
    // start listening
    listener = new MyLocationListener();
    locationManager.requestLocationUpdates(bestProvider, 0, 0, listener);
}
 
Example 5
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 6
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 7
Source File: Util.java    From letv with Apache License 2.0 6 votes vote down vote up
public static String getLocation(Context context) {
    if (context == null) {
        return "";
    }
    try {
        LocationManager locationManager = (LocationManager) context.getSystemService(HOME_RECOMMEND_PARAMETERS.LOCATION);
        Criteria criteria = new Criteria();
        criteria.setCostAllowed(false);
        criteria.setAccuracy(2);
        String bestProvider = locationManager.getBestProvider(criteria, true);
        if (bestProvider != null) {
            Location lastKnownLocation = locationManager.getLastKnownLocation(bestProvider);
            if (lastKnownLocation == null) {
                return "";
            }
            double latitude = lastKnownLocation.getLatitude();
            g = latitude + "*" + lastKnownLocation.getLongitude();
            return g;
        }
    } catch (Throwable e) {
        f.b("getLocation", "getLocation>>>", e);
    }
    return "";
}
 
Example 8
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 9
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 10
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 11
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 12
Source File: AndroidLocationEngineImpl.java    From mapbox-events-android with MIT License 5 votes vote down vote up
@VisibleForTesting
static Criteria getCriteria(int priority) {
  Criteria criteria = new Criteria();
  criteria.setAccuracy(priorityToAccuracy(priority));
  criteria.setCostAllowed(true);
  criteria.setPowerRequirement(priorityToPowerRequirement(priority));
  return criteria;
}
 
Example 13
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 14
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 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 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 17
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 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());
		}
	}
}