当前位置: 首页>>代码示例>>Java>>正文


Java LocationManager类代码示例

本文整理汇总了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;
 }
 
开发者ID:CMPUT301F17T23,项目名称:routineKeen,代码行数:29,代码来源:MapsActivity.java

示例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);
        }
    }
}
 
开发者ID:rkshuai,项目名称:chromium-for-android-56-debug-video,代码行数:24,代码来源:GeolocationTracker.java

示例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;
    }
}
 
开发者ID:UDOOboard,项目名称:UDOOBluLib-android,代码行数:25,代码来源:UdooBluAppCompatActivity.java

示例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());
}
 
开发者ID:SkylineLabs,项目名称:FindX,代码行数:21,代码来源:SelectHome.java

示例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);
}
 
开发者ID:charlesng,项目名称:SampleAppArch,代码行数:18,代码来源:LastLocationListener.java

示例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);
}
 
开发者ID:w86763777,项目名称:BikeLine,代码行数:18,代码来源:GPSService.java

示例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);
}
 
开发者ID:coding-dream,项目名称:TPlayer,代码行数:18,代码来源:MethodProxies.java

示例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;
}
 
开发者ID:hoangkien0705,项目名称:Android-UtilCode,代码行数:31,代码来源:LocationUtils.java

示例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;
}
 
开发者ID:MB3hel,项目名称:Quick-Bluetooth-LE,代码行数:22,代码来源:BLEClient.java

示例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);
}
 
开发者ID:sdrausty,项目名称:buildAPKsSamples,代码行数:17,代码来源:WhereAmI.java

示例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;
}
 
开发者ID:gvinciguerra,项目名称:custode,代码行数:17,代码来源:LocationService.java

示例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);
    }
}
 
开发者ID:WorldBank-Transport,项目名称:RoadLab-Pro,代码行数:17,代码来源:GPSDetector.java

示例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;
    }
 
开发者ID:StickyBoi,项目名称:SkateSpot,代码行数:18,代码来源:GPSTracker.java

示例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();
            }
        }
    }
 
开发者ID:CMPUT301F17T23,项目名称:routineKeen,代码行数:29,代码来源:AddHabitEvent.java

示例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);
    }
}
 
开发者ID:hichemcesar24,项目名称:Weather-Android,代码行数:18,代码来源:AlarmReceiver.java


注:本文中的android.location.LocationManager类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。