List of usage examples for android.location Criteria POWER_HIGH
int POWER_HIGH
To view the source code for android.location Criteria POWER_HIGH.
Click Source Link
From source file:Main.java
/** * this criteria will settle for less accuracy, high power, and cost *//*from www . j av a2s . c o m*/ public static Criteria createCoarseCriteria() { Criteria c = new Criteria(); c.setAccuracy(Criteria.ACCURACY_COARSE); c.setAltitudeRequired(false); c.setBearingRequired(false); c.setSpeedRequired(false); c.setCostAllowed(true); c.setPowerRequirement(Criteria.POWER_HIGH); return c; }
From source file:com.nearnotes.NoteLocation.java
@Override public Dialog onCreateDialog(Bundle savedInstanceState) { // Use the Builder class for convenient dialog construction Bundle extras = getArguments();/* w w w .ja va 2s.c o m*/ mTypeFrag = extras.getInt("TypeFrag"); SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(getActivity()); Boolean gpsPref = sharedPref.getBoolean("pref_key_ignore_gps", false); mLocationManager = (LocationManager) getActivity().getSystemService(Context.LOCATION_SERVICE); mCriteria = new Criteria(); if (gpsPref) { mCriteria.setPowerRequirement(Criteria.POWER_HIGH); } else mCriteria.setPowerRequirement(Criteria.POWER_MEDIUM); mProvider = mLocationManager.getBestProvider(mCriteria, true); AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); boolean oldApi = false; int locationMode = 4; Log.e("mProvider", mProvider); ConnectivityManager cm = (ConnectivityManager) getActivity().getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo networkInfo = cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI); boolean networkAvailable = true; TelephonyManager tm = (TelephonyManager) getActivity().getSystemService(Context.TELEPHONY_SERVICE); boolean networkType = true; if (tm.getNetworkType() == TelephonyManager.NETWORK_TYPE_UNKNOWN && !networkInfo.isConnected()) { networkType = false; } Log.d("Phone state before if statement", "Phone State: " + mServiceState); Log.e("network isavailable", String.valueOf(networkInfo.isAvailable())); if (!networkType || !mLocationManager.isProviderEnabled("network")) { networkAvailable = false; } try { Log.e("Location_mode", String.valueOf( Settings.Secure.getInt(getActivity().getContentResolver(), Settings.Secure.LOCATION_MODE))); locationMode = Settings.Secure.getInt(getActivity().getContentResolver(), Settings.Secure.LOCATION_MODE); } catch (SettingNotFoundException e) { oldApi = true; // TODO Auto-generated catch block e.printStackTrace(); } if ((oldApi && mProvider.matches("passive")) || locationMode == LOCATION_MODE_OFF || (!networkAvailable && (mProvider.matches("network") || (!gpsPref && mProvider.matches("gps"))))) { builder.setTitle(getString(R.string.dialog_location_no_location_services_title)); builder.setMessage(getString(R.string.dialog_location_no_location_services_message)); builder.setNeutralButton(R.string.dialog_location_button_settings, noNetworkButton); mAbortRequest = true; } else if ((oldApi && mProvider.matches("gps") && gpsPref) || (mProvider.matches("gps") && gpsPref && (locationMode == LOCATION_MODE_SENSORS_ONLY || locationMode == LOCATION_MODE_HIGH_ACCURACY))) { if (mTypeFrag == NOTE_EDIT) { builder.setTitle(getString(R.string.dialog_location_finding_note_gps)); } else if (mTypeFrag == NOTE_LIST) { builder.setTitle(getString(R.string.dialog_location_updating_note_gps)); } if (locationMode == LOCATION_MODE_SENSORS_ONLY || (oldApi && mProvider.matches("gps")) || !networkAvailable) { builder.setMessage(getString(R.string.dialog_location_only_gps_message)); builder.setNeutralButton(R.string.dialog_location_button_settings, noNetworkButton); } else builder.setPositiveButton(R.string.dialog_location_use_network, null); builder.setView(getActivity().getLayoutInflater().inflate(R.layout.dialogue_location, null)); } else if ((oldApi && mProvider.matches("network")) || (mProvider.matches("network") && (locationMode == LOCATION_MODE_BATTERY_SAVING || locationMode == LOCATION_MODE_HIGH_ACCURACY))) { builder.setView(getActivity().getLayoutInflater().inflate(R.layout.dialogue_location, null)); if (mTypeFrag == NOTE_EDIT) { builder.setTitle(getString(R.string.dialog_location_finding_note_network)); } else if (mTypeFrag == NOTE_LIST) { builder.setTitle(getString(R.string.dialog_location_updating_note_network)); } } builder.setNegativeButton(R.string.cancel, cancelListener); // Create the AlertDialog object and return it // builder.create(); builder.setOnKeyListener(new DialogInterface.OnKeyListener() { @Override public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) { if (keyCode == KeyEvent.KEYCODE_BACK) { mCallback.onLocationFound(null, mTypeFrag); mLocationManager.removeUpdates(NoteLocation.this); Toast.makeText(getActivity(), "Location request cancelled", Toast.LENGTH_SHORT).show(); dialog.cancel(); return true; } return false; } }); mRealDialog = builder.create(); // final LocationListener getFragment() = this.; mRealDialog.setOnShowListener(usingNetwork); mRealDialog.setCanceledOnTouchOutside(false); // mRealDialog.setCancelable(false); return mRealDialog; }
From source file:com.example.testapplication.DialogLocation.java
@SuppressLint("InlinedApi") @Override/*from w ww . j a v a 2s .c om*/ public Dialog onCreateDialog(Bundle savedInstanceState) { mProgressBar = new ProgressBar(getActivity(), null, android.R.attr.progressBarStyleLarge); mProgressBarInv = new ProgressBar(getActivity(), null, android.R.attr.progressBarStyleLarge); mProgressBarInv.setVisibility(ProgressBar.GONE); mLocationManager = (LocationManager) getActivity().getSystemService(Context.LOCATION_SERVICE); mCriteria = new Criteria(); int criteria = (mGpsPref) ? Criteria.POWER_HIGH : Criteria.POWER_MEDIUM; mCriteria.setPowerRequirement(criteria); mProvider = mLocationManager.getBestProvider(mCriteria, true); AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); ConnectivityManager cm = (ConnectivityManager) getActivity().getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo networkInfo = cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI); TelephonyManager tm = (TelephonyManager) getActivity().getSystemService(Context.TELEPHONY_SERVICE); int telephonyInfo = tm.getNetworkType(); boolean networkAvailable = true; if ((telephonyInfo == TelephonyManager.NETWORK_TYPE_UNKNOWN && !networkInfo.isConnected()) || !mLocationManager.isProviderEnabled("network")) { networkAvailable = false; } int locationMode = -1; int locationType = -1; if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { try { locationMode = Settings.Secure.getInt(getActivity().getContentResolver(), Settings.Secure.LOCATION_MODE); } catch (SettingNotFoundException e) { e.printStackTrace(); } if (locationMode == Settings.Secure.LOCATION_MODE_OFF || (!networkAvailable && (mProvider.matches("network")))) locationType = NO_LOCATION_SERVICES; else if (mGpsPref && (locationMode == Settings.Secure.LOCATION_MODE_SENSORS_ONLY || locationMode == Settings.Secure.LOCATION_MODE_HIGH_ACCURACY)) locationType = (locationMode == Settings.Secure.LOCATION_MODE_SENSORS_ONLY || !networkAvailable) ? USING_ONLY_GPS_LOCATION : USING_GPS_LOCATION_NETWORK_AVAILABLE; else if (mProvider.matches("network") && (locationMode == Settings.Secure.LOCATION_MODE_BATTERY_SAVING || locationMode == Settings.Secure.LOCATION_MODE_HIGH_ACCURACY)) locationType = USING_NETWORK_LOCATION; } else { if (mProvider.matches("passive") || !networkAvailable && (mProvider.matches("network") || (!mGpsPref && mProvider.matches("gps")))) locationType = NO_LOCATION_SERVICES; else if (mProvider.matches("gps") && mGpsPref) locationType = ((mProvider.matches("gps")) || !networkAvailable) ? USING_ONLY_GPS_LOCATION : USING_GPS_LOCATION_NETWORK_AVAILABLE; else if (mProvider.matches("network")) locationType = USING_NETWORK_LOCATION; } switch (locationType) { case NO_LOCATION_SERVICES: builder.setTitle(DIALOG_LOCATION_NO_LOCATION_SERVICES_TITLE); builder.setMessage(DIALOG_LOCATION_NO_LOCATION_SERVICES_MESSAGE); builder.setNeutralButton(DIALOG_LOCATION_BUTTON_SETTINGS, noNetworkButton); mAbortRequest = true; break; case USING_ONLY_GPS_LOCATION: builder.setTitle(DIALOG_LOCATION_UPDATING_GPS_TITLE); builder.setMessage(DIALOG_LOCATION_ONLY_GPS_MESSAGE); builder.setNeutralButton(DIALOG_LOCATION_BUTTON_SETTINGS, noNetworkButton); builder.setView(mProgressBar); break; case USING_GPS_LOCATION_NETWORK_AVAILABLE: builder.setTitle(DIALOG_LOCATION_UPDATING_GPS_TITLE); builder.setPositiveButton(DIALOG_LOCATION_USE_NETWORK, null); builder.setView(mProgressBar); break; case USING_NETWORK_LOCATION: builder.setView(mProgressBar); builder.setTitle(DIALOG_LOCATION_UPDATING_NETWORK_TITLE); break; } builder.setNegativeButton(DIALOG_LOCATION_CANCEL, cancelListener); builder.setOnKeyListener(new DialogInterface.OnKeyListener() { @Override public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) { if (keyCode == KeyEvent.KEYCODE_BACK) { mCallback.onLocationFound(null, mFragmentId); mLocationManager.removeUpdates(DialogLocation.this); Toast.makeText(getActivity(), "Location request cancelled", Toast.LENGTH_SHORT).show(); dialog.cancel(); return true; } return false; } }); mRealDialog = builder.create(); mRealDialog.setOnShowListener(usingNetwork); mRealDialog.setCanceledOnTouchOutside(false); return mRealDialog; }
From source file:ru.dublgis.androidlocation.PassiveLocationProvider.java
static public boolean requestSingleUpdate(PendingIntent intent) { try {/*from w w w. ja va 2s . com*/ if (null != mContext && null != mLocationManager && isPermissionGranted(mContext)) { Criteria criteria = new Criteria(); criteria.setPowerRequirement(Criteria.POWER_HIGH); mLocationManager.requestSingleUpdate(criteria, intent); return true; } } catch (Throwable e) { Log.e(TAG, "Failed to start location updates", e); } return false; }
From source file:jp.gr.java_conf.ya.shiobeforandroid3.UpdateTweetDrive.java
private final void init_location() { pref_app = PreferenceManager.getDefaultSharedPreferences(this); new Thread(new Runnable() { @Override//from w ww. ja v a 2 s . c o m public final void run() { try { final Criteria criteria = new Criteria(); criteria.setAccuracy(Criteria.ACCURACY_FINE); criteria.setPowerRequirement(Criteria.POWER_HIGH); mLocationManager = (LocationManager) getSystemService(LOCATION_SERVICE); if (mLocationManager != null) { // final String provider = manager.getBestProvider(criteria, true); boolean provider_flag = false; final List<String> providers = mLocationManager.getProviders(true); for (final String provider : providers) { if ((provider.equals(LocationManager.GPS_PROVIDER)) || (provider.equals(LocationManager.NETWORK_PROVIDER))) { if (mLocationManager.isProviderEnabled(provider)) { provider_flag = true; } } WriteLog.write(UpdateTweetDrive.this, "requestLocationUpdates() provider: " + provider); final int pref_locationinfo_mintime = ListAdapter.getPrefInt(UpdateTweetDrive.this, "pref_locationinfo_mintime", "300000"); try { new Runnable() { @Override public final void run() { if (ContextCompat.checkSelfPermission(UpdateTweetDrive.this, android.Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) { mLocationManager.requestLocationUpdates(provider, pref_locationinfo_mintime, 0, UpdateTweetDrive.this); } else { if (Build.VERSION.SDK_INT >= 23) requestPermissions( new String[] { android.Manifest.permission.ACCESS_FINE_LOCATION }, REQUEST_PERMISSION); } } }; } catch (final Exception e) { WriteLog.write(UpdateTweetDrive.this, e); } } if (startup_flag == true) { startup_flag = false; if (provider_flag == false) { toast(getString(R.string.open_location_source_settings)); try { startActivity(new Intent("android.settings.LOCATION_SOURCE_SETTINGS")); } catch (final ActivityNotFoundException e) { WriteLog.write(UpdateTweetDrive.this, e); } catch (final Exception e) { WriteLog.write(UpdateTweetDrive.this, e); } } } } } catch (final IllegalArgumentException e) { WriteLog.write(UpdateTweetDrive.this, e); } catch (final RuntimeException e) { WriteLog.write(UpdateTweetDrive.this, e); } } }).start(); }
From source file:org.navitproject.navit.NavitVehicle.java
/** * @brief Creates a new {@code NavitVehicle} * * @param context/*from w w w. ja va 2s .co m*/ * @param pcbid The address of the position callback function called when a location update is received * @param scbid The address of the status callback function called when a status update is received * @param fcbid The address of the fix callback function called when a * {@code android.location.GPS_FIX_CHANGE} is received, indicating a change in GPS fix status */ NavitVehicle(Context context, int pcbid, int scbid, int fcbid) { if (ContextCompat.checkSelfPermission(context, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) { // Permission is not granted return; } this.context = context; sLocationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE); preciseLocationListener = new NavitLocationListener(); preciseLocationListener.precise = true; fastLocationListener = new NavitLocationListener(); /* Use 2 LocationProviders, one precise (usually GPS), and one not so precise, but possible faster. The fast provider is disabled when the precise provider gets its first fix. */ // Selection criteria for the precise provider Criteria highCriteria = new Criteria(); highCriteria.setAccuracy(Criteria.ACCURACY_FINE); highCriteria.setAltitudeRequired(true); highCriteria.setBearingRequired(true); highCriteria.setCostAllowed(true); highCriteria.setPowerRequirement(Criteria.POWER_HIGH); // Selection criteria for the fast provider Criteria lowCriteria = new Criteria(); lowCriteria.setAccuracy(Criteria.ACCURACY_COARSE); lowCriteria.setAltitudeRequired(false); lowCriteria.setBearingRequired(false); lowCriteria.setCostAllowed(true); lowCriteria.setPowerRequirement(Criteria.POWER_HIGH); Log.e("NavitVehicle", "Providers " + sLocationManager.getAllProviders()); preciseProvider = sLocationManager.getBestProvider(highCriteria, false); Log.e("NavitVehicle", "Precise Provider " + preciseProvider); fastProvider = sLocationManager.getBestProvider(lowCriteria, false); Log.e("NavitVehicle", "Fast Provider " + fastProvider); vehicle_pcbid = pcbid; vehicle_scbid = scbid; vehicle_fcbid = fcbid; context.registerReceiver(preciseLocationListener, new IntentFilter(GPS_FIX_CHANGE)); sLocationManager.requestLocationUpdates(preciseProvider, 0, 0, preciseLocationListener); sLocationManager.addGpsStatusListener(preciseLocationListener); /* * Since Android criteria have no way to specify "fast fix", lowCriteria may return the same * provider as highCriteria, even if others are available. In this case, do not register two * listeners for the same provider but pick the fast provider manually. (Usually there will * only be two providers in total, which makes the choice easy.) */ if (fastProvider == null || preciseProvider.compareTo(fastProvider) == 0) { List<String> fastProviderList = sLocationManager.getProviders(lowCriteria, false); fastProvider = null; for (String fastCandidate : fastProviderList) { if (preciseProvider.compareTo(fastCandidate) != 0) { fastProvider = fastCandidate; break; } } } if (fastProvider != null) { sLocationManager.requestLocationUpdates(fastProvider, 0, 0, fastLocationListener); } }
From source file:com.marianhello.cordova.bgloc.LocationUpdateService.java
/** */* www . j av a2 s . c o m*/ * @param value set true to engage "aggressive", battery-consuming tracking, false for stationary-region tracking */ private void setPace(Boolean value) { Log.i(TAG, "setPace: " + value); Boolean wasMoving = isMoving; isMoving = value; isAcquiringStationaryLocation = false; isAcquiringSpeed = false; stationaryLocation = null; locationManager.removeUpdates(this); criteria.setAccuracy(Criteria.ACCURACY_FINE); criteria.setHorizontalAccuracy(translateDesiredAccuracy(desiredAccuracy)); criteria.setPowerRequirement(Criteria.POWER_HIGH); if (isMoving) { // setPace can be called while moving, after distanceFilter has been recalculated. We don't want to re-acquire velocity in this case. if (!wasMoving) { isAcquiringSpeed = true; } } else { isAcquiringStationaryLocation = true; } // Temporarily turn on super-aggressive geolocation on all providers when acquiring velocity or stationary location. if (isAcquiringSpeed || isAcquiringStationaryLocation) { locationAcquisitionAttempts = 0; // Turn on each provider aggressively for a short period of time List<String> matchingProviders = locationManager.getAllProviders(); for (String provider : matchingProviders) { if (provider != LocationManager.PASSIVE_PROVIDER) { locationManager.requestLocationUpdates(provider, 0, 0, this); } } } else { locationManager.requestLocationUpdates(locationManager.getBestProvider(criteria, true), locationTimeout * 1000, scaledDistanceFilter, this); } }
From source file:com.ushahidi.android.app.util.Util.java
/** this criteria needs high accuracy, high power, and cost */ public static Criteria createFineCriteria() { Criteria c = new Criteria(); c.setAccuracy(Criteria.ACCURACY_FINE); c.setAltitudeRequired(false);/*from w ww. jav a 2 s . c o m*/ c.setBearingRequired(false); c.setSpeedRequired(false); c.setCostAllowed(true); c.setPowerRequirement(Criteria.POWER_HIGH); return c; }
From source file:com.anton.gavel.GavelMain.java
@TargetApi(Build.VERSION_CODES.GINGERBREAD) @Override//from w w w.ja v a 2 s . com public void onClick(View arg0) { //click listener for location button LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); Criteria criteria = new Criteria(); criteria.setAccuracy(Criteria.ACCURACY_FINE); criteria.setPowerRequirement(Criteria.POWER_HIGH); //String provider = locationManager.getBestProvider(criteria, true); locationManager.requestSingleUpdate(criteria, new LocationListener() { @Override public void onLocationChanged(Location location) { if (Build.VERSION.SDK_INT < Build.VERSION_CODES.GINGERBREAD) createDialog(DIALOG_NO_GEOCODING); else if (Geocoder.isPresent()) (new ReverseGeocodingTask(getBaseContext())).execute(new Location[] { location }); // Invoking reverse geocoding in an AsyncTask. } @Override public void onProviderDisabled(String provider) { } @Override public void onProviderEnabled(String provider) { } @Override public void onStatusChanged(String provider, int status, Bundle extras) { } }, null); }
From source file:net.jongrakko.zipsuri.activity.PostUploadActivity.java
@Override public void onCheckedChanged(final CompoundButton buttonView, boolean isChecked) { if (isChecked) { switch (buttonView.getId()) { case R.id.radioButtonAddressGPS: mEditTextAddress.setOnClickListener(null); this.mGoogleMap.setOnMapClickListener(this); this.mGoogleMap.setOnMyLocationButtonClickListener(this); if (ContextCompat.checkSelfPermission(getContext(), Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) { mGoogleMap.setMyLocationEnabled(true); LocationProvider lprovider; LocationManager lm = (LocationManager) getActivity() .getSystemService(getActivity().LOCATION_SERVICE); lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this); lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, this); String provider;/*from ww w .ja v a 2 s . com*/ Criteria criteria = new Criteria(); criteria.setAccuracy(Criteria.ACCURACY_FINE); criteria.setPowerRequirement(Criteria.POWER_HIGH); provider = lm.getBestProvider(criteria, true); if (provider == null || provider.equals("passive")) { // ? ? ?? new AlertDialog.Builder(getActivity()).setTitle(" ??") .setNeutralButton("??", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { buttonView.toggle(); startActivityForResult( new Intent( android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS), 0); } }).setOnCancelListener(new DialogInterface.OnCancelListener() { @Override public void onCancel(DialogInterface dialog) { dialog.dismiss(); } }).show(); } else { // ? ? lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1, 1, this); Location l = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER); if (l != null) { Log.e("hello??", "okok"); } } if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { requestPermissions(new String[] { Manifest.permission.ACCESS_FINE_LOCATION }, MY_LOCATION_REQUEST_CODE); } } break; case R.id.radioButtonAddressSelf: this.mGoogleMap.setOnMapClickListener(null); mEditTextAddress.setOnClickListener(this); this.mGoogleMap.setOnMyLocationButtonClickListener(null); mGoogleMap.setMyLocationEnabled(false); mEditTextAddress.setOnClickListener(this); startActivityForResult(new Intent(getContext(), SearchAddressActivity.class), SEARCH_ADDRESS); break; } } }