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

The following examples show how to use android.location.Criteria#setPowerRequirement() . 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: 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 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: GoogleMapImpl.java    From android_packages_apps_GmsCore with Apache License 2.0 6 votes vote down vote up
private GoogleMapImpl(Context context, GoogleMapOptions options) {
    this.context = context;
    Context appContext = context;
    if (appContext.getApplicationContext() != null)
        appContext = appContext.getApplicationContext();
    Context wrappedContext = ApplicationContextWrapper.gmsContextWithAttachedApplicationContext(appContext);
    backendMap = new BackendMap(wrappedContext, this);
    uiSettings = new UiSettingsImpl(this);
    projection = new ProjectionImpl(backendMap.getViewport());
    this.options = options;

    criteria = new Criteria();
    criteria.setAccuracy(Criteria.ACCURACY_COARSE);
    criteria.setPowerRequirement(Criteria.POWER_MEDIUM);

    if (options != null) initFromOptions();
}
 
Example 4
Source File: NativeLocationClientImpl.java    From android_external_GmsLib with Apache License 2.0 6 votes vote down vote up
private static Criteria makeNativeCriteria(LocationRequest request) {
    Criteria criteria = new Criteria();
    switch (request.getPriority()) {
        case LocationRequest.PRIORITY_HIGH_ACCURACY:
            criteria.setAccuracy(Criteria.ACCURACY_FINE);
            criteria.setPowerRequirement(Criteria.POWER_HIGH);
            break;
        case LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY:
        default:
            criteria.setAccuracy(Criteria.ACCURACY_COARSE);
            criteria.setPowerRequirement(Criteria.POWER_MEDIUM);
            break;
        case LocationRequest.PRIORITY_NO_POWER:
        case LocationRequest.PRIORITY_LOW_POWER:
            criteria.setAccuracy(Criteria.ACCURACY_COARSE);
            criteria.setPowerRequirement(Criteria.POWER_LOW);
    }
    return criteria;
}
 
Example 5
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 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: 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 8
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 9
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 10
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 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: LocationInfo.java    From batteryhub with Apache License 2.0 5 votes vote down vote up
public static String getBestProvider(Context context) {
    LocationManager manager = (LocationManager)
            context.getSystemService(Context.LOCATION_SERVICE);
    Criteria criteria = new Criteria();

    criteria.setAccuracy(Criteria.ACCURACY_COARSE);
    criteria.setPowerRequirement(Criteria.POWER_LOW);

    return manager.getBestProvider(criteria, true);
}
 
Example 14
Source File: DalvikPositionService.java    From attach with GNU General Public License v3.0 5 votes vote down vote up
private Criteria getLocationProvider() {
    final Criteria criteria = new Criteria();
    if (timeInterval <= 1000 && distanceFilter <= 1.0f) {
        // Parameters HIGH/HIGHEST
        criteria.setAccuracy(Criteria.ACCURACY_FINE);
        criteria.setHorizontalAccuracy(Criteria.ACCURACY_HIGH);
        criteria.setVerticalAccuracy(Criteria.ACCURACY_HIGH);
        criteria.setBearingAccuracy(Criteria.ACCURACY_HIGH);
        criteria.setSpeedAccuracy(Criteria.ACCURACY_HIGH);
        criteria.setPowerRequirement(Criteria.POWER_HIGH);
    } else if (timeInterval >= 30000 && distanceFilter >= 100.0f) {
        // Parameters LOW/LOWEST
        criteria.setAccuracy(Criteria.ACCURACY_COARSE);
        criteria.setHorizontalAccuracy(Criteria.ACCURACY_LOW);
        criteria.setVerticalAccuracy(Criteria.ACCURACY_LOW);
        criteria.setBearingAccuracy(Criteria.ACCURACY_LOW);
        criteria.setSpeedAccuracy(Criteria.ACCURACY_LOW);
        criteria.setPowerRequirement(Criteria.POWER_LOW);
    } else {
        // Parameter MEDIUM
        criteria.setAccuracy(Criteria.ACCURACY_COARSE);
        criteria.setHorizontalAccuracy(Criteria.ACCURACY_MEDIUM);
        criteria.setVerticalAccuracy(Criteria.ACCURACY_MEDIUM);
        criteria.setBearingAccuracy(Criteria.ACCURACY_MEDIUM);
        criteria.setSpeedAccuracy(Criteria.ACCURACY_MEDIUM);
        criteria.setPowerRequirement(Criteria.POWER_MEDIUM);
    }
    return criteria;
}
 
Example 15
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 16
Source File: GeoLocation.java    From open-rmbt with Apache License 2.0 5 votes vote down vote up
public static Location getLastKnownLocation(Context context) {
    LocationManager locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
    
    final Criteria criteria = new Criteria();
    criteria.setAccuracy(Criteria.ACCURACY_FINE);
    criteria.setPowerRequirement(Criteria.POWER_HIGH);
    final String providerName = locationManager.getBestProvider(criteria, true);
    if (providerName == null)
        return null;
    return locationManager.getLastKnownLocation(providerName);
}
 
Example 17
Source File: GeoLowResSucker.java    From CameraV with GNU General Public License v3.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
public GeoLowResSucker(Context context) {
	super(context);
	setSucker(this);
	
	lm = (LocationManager) context.getApplicationContext().getSystemService(Context.LOCATION_SERVICE);
		
	criteria = new Criteria();
	criteria.setAccuracy(Criteria.ACCURACY_LOW);
	criteria.setPowerRequirement(Criteria.POWER_LOW);
	
	mBestProvider = lm.getBestProvider(criteria, true); //we only want providers that are on
	
	lm.requestLocationUpdates(mBestProvider, MIN_TIME, MIN_DISTANCE, this);
	
	setTask(new TimerTask() {

		@Override
		public void run() throws NullPointerException {
			if(getIsRunning()) {
				try {
					double[] loc = updateLocation();
					if (loc != null)
						sendToBuffer(new ILogPack(Geo.Keys.GPS_COORDS, "[" + loc[0] + "," + loc[1] + "]"));
				} catch(NullPointerException e) {
					Log.e(LOG, "location NPE", e);
				}
			}
		}
	});
	
	getTimer().schedule(getTask(), 0, Geo.LOG_RATE);
}
 
Example 18
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());
		}
	}
}
 
Example 19
Source File: InstrumentationService.java    From sana.mobile with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@SuppressLint("NewApi")
public void handleMessage(Message msg) {
    Log.i(TAG, "msg obj = " + String.valueOf(msg.obj));
    switch (msg.what) {
        case MSG_GET_LOCATION:
            Uri uri = Uri.parse(msg.obj.toString());
            if (msg.getData() != null) {
                //Intent reply = msg.getData().getParcelable(Intent.EXTRA_INTENT);
                PendingIntent replyTo = msg.getData().getParcelable(Intent.EXTRA_INTENT);
                //Log.d(TAG, "replyTo: " + String.valueOf(replyTo));
                LocationListener listener = getListener(null, uri, msg.arg1, msg.getData());
                try {
                    Criteria criteria = new Criteria();
                    if (android.os.Build.VERSION.SDK_INT > android.os.Build.VERSION_CODES.FROYO) {
                        criteria.setPowerRequirement(Criteria.POWER_HIGH);
                        criteria.setHorizontalAccuracy(Criteria.ACCURACY_HIGH);
                    } else {
                        criteria.setAccuracy(Criteria.ACCURACY_FINE);
                    }
                    List<String> providers = locationManager.getProviders(criteria, true);
                    for (String provider : providers) {
                        Log.d(TAG, "Using location provider: " + provider);
                        locationManager.requestLocationUpdates(provider, 0, 0, listener);
                    }
                    //if(TextUtils.isEmpty(provider))
                    //	throw new IllegalArgumentException("No location providers available");
                    // add to our listeners so that we can clean up later if necessary
                    //replies.put(msg.arg1, replyTo);
                    if (providers.size() == 0) {
                        Location nullLocation = new Location(LocationManager.GPS_PROVIDER);
                        nullLocation.setAccuracy(0);
                        nullLocation.setLatitude(0);
                        nullLocation.setLongitude(0);
                        listener.onLocationChanged(nullLocation);
                    } else {
                        listeners.put(msg.arg1, listener);
                    }
                } catch (Exception e) {
                    Log.e(TAG, "Error getting location updates: " + e.getMessage());
                    e.printStackTrace();
                    removeListener(msg.arg1);
                }
            } else {
                Log.w(TAG, "no replyTo in original intent sent to InstrumentationService");
                removeListener(msg.arg1);
            }
            break;
        default:
            Log.w(TAG, "Unknown message! Message = " + msg.what);
            removeListener(msg.arg1);
    }
}
 
Example 20
Source File: GeoLocation.java    From open-rmbt with Apache License 2.0 4 votes vote down vote up
/** Load Listeners */
public void start()
{
    if (!started)
    {
        started = true;
        
        final Criteria criteriaCoarse = new Criteria();
        /* "Coarse" accuracy means "no need to use GPS". */
        criteriaCoarse.setAccuracy(Criteria.ACCURACY_COARSE);
        criteriaCoarse.setPowerRequirement(Criteria.POWER_LOW);
        final String coarseProviderName = locationManager.getBestProvider(criteriaCoarse, true);
        
        if (coarseProviderName != null)
        {
            isBetterLocation(locationManager.getLastKnownLocation(coarseProviderName));
            
            coarseLocationListener = new LocListener();
            locationManager.requestLocationUpdates(coarseProviderName,
            minTime,
            minDistance, coarseLocationListener);
        }
        
        if (gpsEnabled)
        {
            final Criteria criteriaFine = new Criteria();
            /* "Fine" accuracy means "use GPS". */
            criteriaFine.setAccuracy(Criteria.ACCURACY_FINE);
            criteriaFine.setPowerRequirement(Criteria.POWER_HIGH);
            final String fineProviderName = locationManager.getBestProvider(criteriaFine, true);
            
            if (fineProviderName != null)
            {
                isBetterLocation(locationManager.getLastKnownLocation(fineProviderName));
                
                fineLocationListener = new LocListener();
                locationManager.requestLocationUpdates(fineProviderName,
                minTime,
                minDistance, fineLocationListener);
            }
        }
    }
}