本文整理汇总了Java中android.location.LocationManager类的典型用法代码示例。如果您正苦于以下问题:Java LocationManager类的具体用法?Java LocationManager怎么用?Java LocationManager使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
LocationManager类属于android.location包,在下文中一共展示了LocationManager类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: getDeviceLoc
import android.location.LocationManager; //导入依赖的package包/类
/**
* Get the current location of the device
* ref: https://chantisandroid.blogspot.ca/2017/06/get-current-location-example-in-android.html
* @return current location
*/
private Location getDeviceLoc() {
if (ActivityCompat.checkSelfPermission(MapsActivity.this, Manifest.permission.ACCESS_FINE_LOCATION)
!= PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission
(MapsActivity.this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(MapsActivity.this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, REQUEST_LOCATION);
} else {
Location location = service.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
Location location1 = service.getLastKnownLocation(LocationManager.GPS_PROVIDER);
Location location2 = service.getLastKnownLocation(LocationManager.PASSIVE_PROVIDER);
if (location != null) {
return location;
} else if (location1 != null) {
return location1;
} else if (location2 != null) {
return location2;
} else {
Toast.makeText(this, "Unable to trace your location", Toast.LENGTH_SHORT).show();
}
}
return null;
}
示例2: refreshLastKnownLocation
import android.location.LocationManager; //导入依赖的package包/类
/**
* Requests an updated location if the last known location is older than maxAge milliseconds.
*
* Note: this must be called only on the UI thread.
*/
@SuppressFBWarnings("LI_LAZY_INIT_UPDATE_STATIC")
static void refreshLastKnownLocation(Context context, long maxAge) {
ThreadUtils.assertOnUiThread();
// We're still waiting for a location update.
if (sListener != null) return;
LocationManager locationManager =
(LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
Location location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if (location == null || getLocationAge(location) > maxAge) {
String provider = LocationManager.NETWORK_PROVIDER;
if (locationManager.isProviderEnabled(provider)) {
sListener = new SelfCancelingListener(locationManager);
locationManager.requestSingleUpdate(provider, sListener, null);
}
}
}
示例3: onActivityResult
import android.location.LocationManager; //导入依赖的package包/类
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case REQUEST_ENABLE_BT:
// When the request to enable Bluetooth returns
if (resultCode == Activity.RESULT_OK) {
Toast.makeText(this, R.string.bt_on, Toast.LENGTH_SHORT).show();
} else {
// User did not enable Bluetooth or an error occurred
Toast.makeText(this, R.string.bt_not_on, Toast.LENGTH_SHORT).show();
finish();
}
break;
case REQUEST_ENABLE_LOCATION:
if (isLocationProviderEnabled((LocationManager) getSystemService(Context.LOCATION_SERVICE))) {
initBluManager();
}
break;
default:
Log.e(TAG, "Unknown request code");
break;
}
}
示例4: onLocationChanged
import android.location.LocationManager; //导入依赖的package包/类
@Override
public void onLocationChanged(Location location) {
map.setOnMapLoadedCallback(this);
this.location = location;
if(locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER) != null)
{
mystringLatitude = String.valueOf(getLatitude());
mystringLongitude = String.valueOf(getLongitude());
// Toast.makeText(this,"Activity OLC Update " + networkTS,Toast.LENGTH_SHORT).show();
}
this.location = location;
Latitude = String.valueOf(getLatitude());
Longitude = String.valueOf(getLongitude());
}
示例5: onActive
import android.location.LocationManager; //导入依赖的package包/类
@Override
protected void onActive() {
if (ActivityCompat.checkSelfPermission(context, permission.ACCESS_FINE_LOCATION)
!= PackageManager.PERMISSION_GRANTED
&& ActivityCompat.checkSelfPermission(context, permission.ACCESS_COARSE_LOCATION)
!= PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
// ActivityCompat#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
return;
}
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, listener);
}
示例6: onCreate
import android.location.LocationManager; //导入依赖的package包/类
@Override
public void onCreate() {
hasAltitude = false;
hasVelocity = false;
hasAcceleration = false;
listenerStarted = false;
hasBearing = false;
hasDistance = false;
mLocation = null;
callbacks = new ArrayList<>();
mLocationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
// send start broadcast
Intent startIntent = new Intent();
startIntent.setAction(SERVICE_START);
sendBroadcast(startIntent);
}
示例7: call
import android.location.LocationManager; //导入依赖的package包/类
@Override
public Object call(Object who, Method method, Object... args) throws Throwable {
if (isFakeLocationEnable()) {
Object transport = ArrayUtils.getFirst(args, mirror.android.location.LocationManager.GpsStatusListenerTransport.TYPE);
Object locationManager = mirror.android.location.LocationManager.GpsStatusListenerTransport.this$0.get(transport);
mirror.android.location.LocationManager.GpsStatusListenerTransport.onGpsStarted.call(transport);
mirror.android.location.LocationManager.GpsStatusListenerTransport.onFirstFix.call(transport, 0);
if (mirror.android.location.LocationManager.GpsStatusListenerTransport.mListener.get(transport) != null) {
MockLocationHelper.invokeSvStatusChanged(transport);
} else {
MockLocationHelper.invokeNmeaReceived(transport);
}
GPSListenerThread.get().addListenerTransport(locationManager);
return true;
}
return super.call(who, method, args);
}
示例8: register
import android.location.LocationManager; //导入依赖的package包/类
/**
* 注册
* <p>使用完记得调用{@link #unregister()}</p>
* <p>需添加权限 {@code <uses-permission android:name="android.permission.INTERNET"/>}</p>
* <p>需添加权限 {@code <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>}</p>
* <p>需添加权限 {@code <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>}</p>
* <p>如果{@code minDistance}为0,则通过{@code minTime}来定时更新;</p>
* <p>{@code minDistance}不为0,则以{@code minDistance}为准;</p>
* <p>两者都为0,则随时刷新。</p>
*
* @param minTime 位置信息更新周期(单位:毫秒)
* @param minDistance 位置变化最小距离:当位置距离变化超过此值时,将更新位置信息(单位:米)
* @param listener 位置刷新的回调接口
* @return {@code true}: 初始化成功<br>{@code false}: 初始化失败
*/
public static boolean register(long minTime, long minDistance, OnLocationChangeListener listener) {
if (listener == null) return false;
mLocationManager = (LocationManager) Utils.getContext().getSystemService(Context.LOCATION_SERVICE);
mListener = listener;
if (!isLocationEnabled()) {
ToastUtils.showShortSafe("无法定位,请打开定位服务");
return false;
}
String provider = mLocationManager.getBestProvider(getCriteria(), true);
Location location = mLocationManager.getLastKnownLocation(provider);
if (location != null) listener.getLastKnownLocation(location);
if (myLocationListener == null) myLocationListener = new MyLocationListener();
mLocationManager.requestLocationUpdates(provider, minTime, minDistance, myLocationListener);
return true;
}
示例9: checkBluetooth
import android.location.LocationManager; //导入依赖的package包/类
public BLEClient.BtError checkBluetooth(){
btManager = (BluetoothManager) context.getSystemService(Context.BLUETOOTH_SERVICE);
btAdapter = btManager.getAdapter();
if(!context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH))
return BLEClient.BtError.NoBluetooth;
if(!context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH_LE))
return BLEClient.BtError.NoBLE;
if(btAdapter == null || !btAdapter.isEnabled())
return BLEClient.BtError.Disabled;
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP && useNewMethod){
if((ContextCompat.checkSelfPermission(context, android.Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_DENIED) &&
(ContextCompat.checkSelfPermission(context, android.Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_DENIED)){
return BtError.NoLocationPermission;
}
LocationManager lm = (LocationManager)context.getSystemService(Context.LOCATION_SERVICE);
if(!(lm.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) && !(lm.isProviderEnabled(LocationManager.GPS_PROVIDER))){
return BtError.LocationDisabled;
}
}
return BLEClient.BtError.None;
}
示例10: onCreate
import android.location.LocationManager; //导入依赖的package包/类
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
textOut = (TextView) findViewById(R.id.textOut);
locationManager = (LocationManager) getSystemService(LOCATION_SERVICE); // <5>
geocoder = new Geocoder(this); // <6>
// Initialize with the last known location
Location lastLocation = locationManager
.getLastKnownLocation(LocationManager.GPS_PROVIDER); // <7>
if (lastLocation != null)
onLocationChanged(lastLocation);
}
示例11: onStartCommand
import android.location.LocationManager; //导入依赖的package包/类
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
final int minTime = (int) TimeUnit.MINUTES.toMillis(4);
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, minTime, 150, this);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, minTime, 150, this);
if (intent.getAction() != null) {
if (intent.getAction().equals(GEOCODE_ON_ACTION))
mustGeocode = true;
else if (intent.getAction().equals(GEOCODE_OFF_ACTION))
mustGeocode = false;
}
sendLocalBroadcastIntent(getBestLastKnownLocation(this));
}
return START_REDELIVER_INTENT;
}
示例12: registerProvidersChangedReceiver
import android.location.LocationManager; //导入依赖的package包/类
private void registerProvidersChangedReceiver(Context broadcastContext) {
receiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
Log.i(TAG, "registerProvidersChangedReceiver, action: " + action);
if (LocationManager.PROVIDERS_CHANGED_ACTION.equals(action)) {
start();
}
}
};
if (receiver != null && broadcastContext != null) {
IntentFilter filter = new IntentFilter(LocationManager.PROVIDERS_CHANGED_ACTION);
broadcastContext.registerReceiver(receiver, filter);
}
}
示例13: getLocation
import android.location.LocationManager; //导入依赖的package包/类
public Location getLocation() {
if (ContextCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
Toast.makeText(context, "Permission not granted", Toast.LENGTH_SHORT).show();
return null;
}
LocationManager lm = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
boolean isGPSEnabled = lm.isProviderEnabled(LocationManager.GPS_PROVIDER);
if (isGPSEnabled) {
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 6000, 10, this);
Location l = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
return l;
} else {
Toast.makeText(context, "Please enable GPS !", Toast.LENGTH_LONG).show();
}
return null;
}
示例14: attachLocation
import android.location.LocationManager; //导入依赖的package包/类
/**
* Adds the user's current location the the habit event
*
* @param view View
*/
public void attachLocation(View view) {
// boolean enabled = service.isProviderEnabled(LocationManager.GPS_PROVIDER);
// if (!enabled) {
// Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
// startActivity(intent);
// } else {
// getDeviceLoc();
// }
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
if (!locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
buildAlertMessageNoGps();
} else if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
try {
location = getDeviceLoc();
Toast.makeText(this,"Location Attached",Toast.LENGTH_SHORT).show();
} catch (Exception e) {
Toast.makeText(this,"Unable to trace your location",Toast.LENGTH_SHORT).show();
}
}
}
示例15: onPreExecute
import android.location.LocationManager; //导入依赖的package包/类
@Override
protected void onPreExecute() {
Log.d(TAG, "Trying to determine location...");
locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
locationListener = new BackgroundLocationListener();
try {
if (locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) {
// Only uses 'network' location, as asking the GPS every time would drain too much battery
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);
} else {
Log.d(TAG, "'Network' location is not enabled. Cancelling determining location.");
onPostExecute(null);
}
} catch (SecurityException e) {
Log.e(TAG, "Couldn't request location updates. Probably this is an Android (>M) runtime permissions issue ", e);
}
}