本文整理汇总了Java中android.location.LocationManager.requestLocationUpdates方法的典型用法代码示例。如果您正苦于以下问题:Java LocationManager.requestLocationUpdates方法的具体用法?Java LocationManager.requestLocationUpdates怎么用?Java LocationManager.requestLocationUpdates使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类android.location.LocationManager
的用法示例。
在下文中一共展示了LocationManager.requestLocationUpdates方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: 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.showShortToastSafe("无法定位,请打开定位服务");
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;
}
示例2: provide
import android.location.LocationManager; //导入方法依赖的package包/类
@Override
protected void provide() {
Looper.prepare();
locationManager = (LocationManager) this.getContext().getSystemService(Context.LOCATION_SERVICE);
locationListener = new MyLocationListener();
long minTime = this.interval;
float minDistance = 0;
String provider;
if (Geolocation.LEVEL_EXACT.equals(level)) {
provider = LocationManager.GPS_PROVIDER;
}
else {
provider = LocationManager.NETWORK_PROVIDER;
}
locationManager.requestLocationUpdates(provider, minTime, minDistance, locationListener);
Looper.loop();
}
示例3: initLocation
import android.location.LocationManager; //导入方法依赖的package包/类
public void initLocation(boolean granted) {
if (granted) {
locationListener = new MyLocationListener();
locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
if (ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION)
!= PackageManager.PERMISSION_GRANTED
&& ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_COARSE_LOCATION)
!= PackageManager.PERMISSION_GRANTED) {
requestPermission(Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION);
return;
}
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);
hasLocation = true;
} else {
hasLocation = false;
}
}
示例4: 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;
}
示例5: 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);
}
}
示例6: onCreate
import android.location.LocationManager; //导入方法依赖的package包/类
protected void onCreate(Bundle paramBundle)
{
super.onCreate(paramBundle);
setContentView(R.layout.activity_maps);
localdb = openOrCreateDatabase("localdb",MODE_PRIVATE,null);
localdb.execSQL("CREATE TABLE IF NOT EXISTS position(imei NVARCHAR(100), latitude NVARCHAR(100),longitude NVARCHAR(100), timestamp DATETIME, out NVARCHAR(1));");
localdb.execSQL("DELETE FROM position WHERE timestamp <= date('now','-2 day');"); // to keep data in last day on user phone
setUpMapIfNeeded();
this.mMap = ((SupportMapFragment)getSupportFragmentManager().findFragmentById(R.id.map)).getMap();
this.mMap.setMyLocationEnabled(true);
LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
Criteria criteria = new Criteria();
String provider = locationManager.getBestProvider(criteria, true);
Location location = locationManager.getLastKnownLocation(provider);
locationManager.requestLocationUpdates(provider, 20000, 0, this);
TelephonyManager tm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
String myImei = tm.getDeviceId();
timer = new Timer();
timer.schedule(new RunMarker(this.mMap, myImei,this), 0, 20000);
editTextFromDate = (EditText) findViewById(R.id.editText);
search = (Button) findViewById(R.id.button);
DateField fromDate = new DateField(editTextFromDate,search, this, mMap, timer);
}
示例7: startObserving
import android.location.LocationManager; //导入方法依赖的package包/类
/**
* Start listening for location updates. These will be emitted via the
* {@link RCTDeviceEventEmitter} as {@code geolocationDidChange} events.
*
* @param options map containing optional arguments: highAccuracy (boolean)
*/
@ReactMethod
public void startObserving(ReadableMap options) {
if (LocationManager.GPS_PROVIDER.equals(mWatchedProvider)) {
return;
}
LocationOptions locationOptions = LocationOptions.fromReactMap(options);
try {
LocationManager locationManager =
(LocationManager) getReactApplicationContext().getSystemService(Context.LOCATION_SERVICE);
String provider = getValidProvider(locationManager, locationOptions.highAccuracy);
if (provider == null) {
emitError(PositionError.PERMISSION_DENIED, "No location provider available.");
return;
}
if (!provider.equals(mWatchedProvider)) {
locationManager.removeUpdates(mLocationListener);
locationManager.requestLocationUpdates(
provider,
1000,
locationOptions.distanceFilter,
mLocationListener);
}
mWatchedProvider = provider;
} catch (SecurityException e) {
throwLocationPermissionMissing(e);
}
}
示例8: initLocation
import android.location.LocationManager; //导入方法依赖的package包/类
@SuppressLint("MissingPermission")
private void initLocation() {
LocationManager locationManager =
(LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
locationManager.requestLocationUpdates(
LocationManager.NETWORK_PROVIDER,
0,
0,
this
);
}
示例9: onCreateView
import android.location.LocationManager; //导入方法依赖的package包/类
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_sos, container, false);
ButterKnife.bind(this, view);
sharedPreferences = getActivity().getSharedPreferences("Profile", MODE_PRIVATE);
Dexter.checkPermissions(new MultiplePermissionsListener() {
@Override
public void onPermissionsChecked(MultiplePermissionsReport report) {/* ... */}
@Override
public void onPermissionRationaleShouldBeShown(List<PermissionRequest> permissions, PermissionToken token) {/* ... */}
}, Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission.ACCESS_FINE_LOCATION);
LocationManager locationManager = (LocationManager) getActivity().getSystemService(Context.LOCATION_SERVICE);
LocationListener locationListener = new MyLocationListener();
try {
if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER))
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 5000, 10, locationListener);
else
Toast.makeText(getContext(), "Enable location services", Toast.LENGTH_SHORT).show();
} catch (SecurityException e) {
Log.e("locationListener", e.toString());
}
return view;
}
示例10: getCityByLocation
import android.location.LocationManager; //导入方法依赖的package包/类
void getCityByLocation() {
locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
if (ActivityCompat.shouldShowRequestPermissionRationale(this,
Manifest.permission.ACCESS_FINE_LOCATION)) {
// Explanation not needed, since user requests this himself
} else {
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
MY_PERMISSIONS_ACCESS_FINE_LOCATION);
}
} else if (locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER) ||
locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
progressDialog = new ProgressDialog(this);
progressDialog.setMessage(getString(R.string.getting_location));
progressDialog.setCancelable(false);
progressDialog.setButton(DialogInterface.BUTTON_NEGATIVE, getString(R.string.dialog_cancel), new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
try {
locationManager.removeUpdates(MainActivity.this);
} catch (SecurityException e) {
e.printStackTrace();
}
}
});
progressDialog.show();
if (locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) {
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, this);
}
if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
}
} else {
showLocationSettingsDialog();
}
}
示例11: privacyGuardWorkaround
import android.location.LocationManager; //导入方法依赖的package包/类
private void privacyGuardWorkaround() {
// Workaround for CM privacy guard. Register for location updates in order for it to ask us for permission
LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
try {
DummyLocationListener dummyLocationListener = new DummyLocationListener();
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, dummyLocationListener);
locationManager.removeUpdates(dummyLocationListener);
} catch (SecurityException e) {
// This will most probably not happen, as we just got granted the permission
}
}
示例12: getLocation
import android.location.LocationManager; //导入方法依赖的package包/类
public Location getLocation() {
try {
locationManager = (LocationManager) context.getSystemService(LOCATION_SERVICE);
isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
isNetworkEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
if (ActivityCompat.checkSelfPermission(context,
Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
if (isGPSEnabled) {
if (location == null) {
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
if (locationManager != null) {
location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
}
}
}
if (isNetworkEnabled) {
if (location == null) {
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, this);
if (locationManager != null) {
location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
}
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
return location;
}
示例13: getLocation
import android.location.LocationManager; //导入方法依赖的package包/类
public Location getLocation(){
try{
locationManager = (LocationManager) context.getSystemService(LOCATION_SERVICE);
isGPSEnabled = locationManager.isProviderEnabled(locationManager.GPS_PROVIDER);
isNetworkEnabled=locationManager.isProviderEnabled(locationManager.NETWORK_PROVIDER);
if(ContextCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED
|| ContextCompat.checkSelfPermission(context, Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED ){
if(isGPSEnabled){
if(location==null){
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 10000,10,this);
if(locationManager!=null){
location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
System.out.println("location null reached 57");
}
}
}
// if lcoation is not found from GPS than it will found from network //
if(location==null){
if(isNetworkEnabled){
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 10000,10,this);
if(locationManager!=null){
location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
}
}
}
}
}catch(Exception ex){
}
return location;
}
示例14: getLocation
import android.location.LocationManager; //导入方法依赖的package包/类
public Location getLocation()
{
try
{
locationManager = (LocationManager)getSystemService(LOCATION_SERVICE);
//getting GPS status
isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
//getting network status
isNetworkEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
if (!isGPSEnabled && !isNetworkEnabled)
{
// no network provider is enabled
}
else
{
this.canGetLocation = true;
//First get location from Network Provider
if (isNetworkEnabled)
{
locationManager.requestLocationUpdates(
LocationManager.NETWORK_PROVIDER,
MIN_TIME_BW_UPDATES,
MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
Log.d("Network", "Network");
if (locationManager != null)
{
location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
updateGPSCoordinates();
}
}
//if GPS Enabled get lat/long using GPS Services
if (isGPSEnabled)
{
if (location == null)
{
locationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER,
MIN_TIME_BW_UPDATES,
MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
Log.d("GPS Enabled", "GPS Enabled");
if (locationManager != null)
{
location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
updateGPSCoordinates();
}
}
}
}
}
catch (Exception e)
{
//e.printStackTrace();
Log.e("Error : Location", "Impossible to connect to LocationManager", e);
}
return location;
}
示例15: getLocation
import android.location.LocationManager; //导入方法依赖的package包/类
public Location getLocation(){
try{
locationManager=(LocationManager) mContext.getSystemService(LOCATION_SERVICE);
isGPSEnabled=locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
isNetworkEnabled=locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
if(!isGPSEnabled && !isNetworkEnabled){
Toast.makeText(getApplicationContext(), "Not Connected to Internet!", Toast.LENGTH_SHORT).show();
}else{
this.canGetLocation=true;
if(isNetworkEnabled){
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, Min_Time_Between_Update,Min_Distance_Change_For_Update, this);
Log.d("Network","Network");
if(locationManager!=null){
location=locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if(location!=null){
latitude=location.getLatitude();
longitude=location.getLongitude();
}
//xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
/* Geocoder geo=new Geocoder(this, Locale.ENGLISH);
try{
List<Address> addresses = geo.getFromLocation(latitude, longitude, 1);
if(addresses != null) {
Address fetchedAddress = addresses.get(0);
StringBuilder strAddress = new StringBuilder();
for(int i=0; i<fetchedAddress.getMaxAddressLineIndex(); i++) {
strAddress.append(fetchedAddress.getAddressLine(i)).append("\n");
}
abcd="I am at: " +strAddress.toString();
}
else
//tv.setText("No location found..!");
Toast.makeText(getApplicationContext(), "No location found!", Toast.LENGTH_LONG).show();
}catch(Exception e) {
// TODO: handle exception
e.printStackTrace();
//Toast.makeText(getApplicationContext(), "Could not get Address!! :-(", Toast.LENGTH_LONG).show();
} */
//xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
}
}
if(isGPSEnabled){
if(location==null){
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, Min_Time_Between_Update, Min_Distance_Change_For_Update, this);
Log.d("GPS Enabled", "GPS Enabled");
if(locationManager!=null){
location=locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if(location!=null){
latitude=location.getLatitude();
longitude=location.getLongitude();
}
}
}
}
}
}catch(Exception e){
e.printStackTrace();
}
return location;
}