Android使用criteria选择合适的地理位置服务实现方法
作者:leeon 时间:2022-09-26 14:24:34
本文实例讲述了Android使用criteria选择合适的地理位置服务实现方法。分享给大家供大家参考,具体如下:
/* LocationActivity.java
* @author octobershiner
* 2011 7 24
* SE.HIT
* 利用Criteria选择最优的位置服务,演示定位用户的位置并且监听位置变化的代码
* */
package uni.location;
import android.app.Activity;
import android.content.Context;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.Vibrator;
import android.util.Log;
import android.widget.TextView;
public class LocationActivity extends Activity {
/** Called when the activity is first created. */
//创建lcoationManager对象
private LocationManager manager;
private static final String TAG = "LOCATION DEMO";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//获取系统的服务,
manager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
//创建一个criteria对象
Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_COARSE);
//设置不需要获取海拔方向数据
criteria.setAltitudeRequired(false);
criteria.setBearingRequired(false);
//设置允许产生资费
criteria.setCostAllowed(true);
//要求低耗电
criteria.setPowerRequirement(Criteria.POWER_LOW);
String provider = manager.getBestProvider(criteria, false);
Log.i(TAG, "we choose "+ provider);
Location location = manager.getLastKnownLocation(provider);
//第一次获得设备的位置
updateLocation(location);
//重要函数,监听数据测试
manager.requestLocationUpdates(provider, 6000, 10,
locationListener);
}
//创建一个事件 *
private final LocationListener locationListener = new LocationListener() {
public void onLocationChanged(Location location) {
updateLocation(location);
}
public void onProviderDisabled(String provider){
updateLocation(null);
Log.i(TAG, "Provider now is disabled..");
}
public void onProviderEnabled(String provider){
Log.i(TAG, "Provider now is enabled..");
}
public void onStatusChanged(String provider, int status,Bundle extras){ }
};
//获取用户位置的函数,利用Log显示
private void updateLocation(Location location) {
String latLng;
if (location != null) {
double lat = location.getLatitude();
double lng = location.getLongitude();
latLng = "Latitude:" + lat + " Longitude:" + lng;
} else {
latLng = "Can't access your location";
}
Log.i(TAG, "The location has changed..");
Log.i(TAG, "Your Location:" +latLng);
}
}
同时修改manifest.xml文件:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="uni.location"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="8" />
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".LocationActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
</manifest>
演示结果:
可任看到 我们只要求低的精确度并且最低电量,从最后一行可以看到我的虚拟机网络服务并没有打开,但是选择最佳provider的时候,参数选择了false 所以同样可以选择。
希望本文所述对大家Android程序设计有所帮助。
标签:Android,地理位置
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
MyBatis-Plus多表联查的实现方法(动态查询和静态查询)
2023-11-23 22:26:45
![](https://img.aspxhome.com/file/2023/9/59839_0s.png)
Jenkins+Maven+SVN自动化部署java项目
2021-06-01 11:28:27
![](https://img.aspxhome.com/file/2023/7/61837_0s.png)
Android扫描和生成二维码
2022-09-11 22:57:16
Android开发实现判断通知栏是否打开及前往设置页面的方法
2022-10-21 10:43:45
Spring框架事务属性中事务隔离级别与传播行为全面讲解
2022-08-11 17:24:31
深入了解Hadoop如何实现序列化
2023-10-13 10:33:43
![](https://img.aspxhome.com/file/2023/8/62348_0s.png)
解决springboot 部署到 weblogic 中 jar 包冲突的问题
2021-06-06 11:32:36
![](https://img.aspxhome.com/file/2023/2/78882_0s.png)
SpringBoot中使用JeecgBoot的Autopoi导出Excel的方法步骤
2023-03-31 03:38:11
![](https://img.aspxhome.com/file/2023/8/99748_0s.png)
Java关于MyBatis缓存详解
2021-11-01 00:40:20
SpringBoot中的PUT和Delete请求使用
2022-01-22 19:33:32
![](https://img.aspxhome.com/file/2023/3/96053_0s.jpg)
C# WPF 自定义按钮的方法
2021-08-30 23:42:11
![](https://img.aspxhome.com/file/2023/0/69240_0s.gif)
详解SpringBoot配置连接池
2022-03-28 13:29:42
![](https://img.aspxhome.com/file/2023/8/105908_0s.png)
Java压缩文件工具类ZipUtil使用方法代码示例
2022-11-26 02:21:32
Spring BeanFactory和FactoryBean区别解析
2023-09-18 15:38:28
Android仿微信Viewpager-Fragment惰性加载(lazy-loading)
2023-12-16 23:50:59
![](https://img.aspxhome.com/file/2023/9/108269_0s.gif)
java实现短信验证码5分钟有效时间
2023-08-30 18:42:26
Java 抽象类与接口的对比
2022-05-31 08:28:30
Java 如何优雅的抛出业务异常
2023-11-24 04:04:34
macOS下Spring Boot开发环境搭建教程
2023-11-05 15:50:59
![](https://img.aspxhome.com/file/2023/1/85541_0s.jpg)
C#随机生成Unicode类型字符串
2023-12-05 08:42:29