说到GPS这个名词,相信大家都不陌生,GPS全球定位技术嘛,嗯,Android中定位的方式 一般有这四种:GPS定位,WIFI定准,基站定位,AGPS定位(基站+GPS);
本系列教程只讲解GPS定位的基本使用!GPS是通过与卫星交互来获取设备当前的经纬度,准确 度较高,但也有一些缺点,最大的缺点就是:室内几乎无法使用...需要收到4颗卫星或以上 信号才能保证GPS的准确定位!但是假如你是在室外,无网络的情况,GPS还是可以用的!
本节我们就来探讨下Android中的GPS的基本用法~
官方API文档:LocationManager
这玩意是系统服务来的,不能直接new,需要:
- LocationManager lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
另外用GPS定位别忘了加权限:
- <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
好的,获得了LocationManager对象后,我们可以调用下面这些常用的方法:
requestLocationUpdates(String provider, long minTime, float minDistance, LocationListener listener): 通过制定的LocationProvider周期性地获取定位信息,并触发listener所对应的触发器
官方API文档:LocationProvider
这比是GPS定位组件的抽象表示,调用下述方法可以获取该定位组件的相关信息!
常用的方法如下:
supportsSpeed():判断是LocationProvider否支持速度信息
官方API文档:Location
位置信息的抽象类,我们可以调用下述方法获取相关的定位信息!
常用方法如下:
boolean hasAccuracy():判断该定位信息是否含有精度信息
官方API文档:Criteria
获取LocationProvider时,可以设置过滤条件,就是通过这个类来设置相关条件的~
常用方法如下:
setSpeedRequired(boolean speedRequired):设置是否要求LocationProvider能提供速度信息
运行效果图:
由图可以看到,当前可用的LocationProvider有三个,分别是:
实现代码:
布局文件:activity_main.xml:
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="vertical">
- <Button
- android:id="@+id/btn_one"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="获得系统所有的LocationProvider" />
- <Button
- android:id="@+id/btn_two"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="根据条件获取LocationProvider" />
- <Button
- android:id="@+id/btn_three"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="获取指定的LocationProvider" />
- <TextView
- android:id="@+id/tv_result"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:layout_margin="10dp"
- android:background="#81BB4D"
- android:padding="5dp"
- android:textColor="#FFFFFF"
- android:textSize="20sp"
- android:textStyle="bold" />
- </LinearLayout>
MainActivity.java:
- public class MainActivity extends AppCompatActivity implements View.OnClickListener {
- private Button btn_one;
- private Button btn_two;
- private Button btn_three;
- private TextView tv_result;
- private LocationManager lm;
- private List<String> pNames = new ArrayList<String>(); // 存放LocationProvider名称的集合
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
- bindViews();
- }
- private void bindViews() {
- btn_one = (Button) findViewById(R.id.btn_one);
- btn_two = (Button) findViewById(R.id.btn_two);
- btn_three = (Button) findViewById(R.id.btn_three);
- tv_result = (TextView) findViewById(R.id.tv_result);
- btn_one.setOnClickListener(this);
- btn_two.setOnClickListener(this);
- btn_three.setOnClickListener(this);
- }
- @Override
- public void onClick(View v) {
- switch (v.getId()) {
- case R.id.btn_one:
- pNames.clear();
- pNames = lm.getAllProviders();
- tv_result.setText(getProvider());
- break;
- case R.id.btn_two:
- pNames.clear();
- Criteria criteria = new Criteria();
- criteria.setCostAllowed(false); //免费
- criteria.setAltitudeRequired(true); //能够提供高度信息
- criteria.setBearingRequired(true); //能够提供方向信息
- pNames = lm.getProviders(criteria, true);
- tv_result.setText(getProvider());
- break;
- case R.id.btn_three:
- pNames.clear();
- pNames.add(lm.getProvider(LocationManager.GPS_PROVIDER).getName()); //指定名称
- tv_result.setText(getProvider());
- break;
- }
- }
- //遍历数组返回字符串的方法
- private String getProvider(){
- StringBuilder sb = new StringBuilder();
- for (String s : pNames) {
- sb.append(s + "\n");
- }
- return sb.toString();
- }
- }
在我们使用GPS定位前的第一件事应该是去判断GPS是否已经打开或可用,没打开的话我们需要去 打开GPS才能完成定位!这里不考虑AGPS的情况~
- private boolean isGpsAble(LocationManager lm){
- return lm.isProviderEnabled(android.location.LocationManager.GPS_PROVIDER)?true:false;
- }
方法一:强制打开GPS,Android 5.0后无用....
- //强制帮用户打开GPS 5.0以前可用
- private void openGPS(Context context){
- Intent gpsIntent = new Intent();
- gpsIntent.setClassName("com.android.settings", "com.android.settings.widget.SettingsAppWidgetProvider");
- gpsIntent.addCategory("android.intent.category.ALTERNATIVE");
- gpsIntent.setData(Uri.parse("custom:3"));
- try {
- PendingIntent.getBroadcast(LocationActivity.this, 0, gpsIntent, 0).send();
- } catch (PendingIntent.CanceledException e) {
- e.printStackTrace();
- }
- }
方法二:打开GPS位置信息设置页面,让用户自行打开
- //打开位置信息设置页面让用户自己设置
- private void openGPS2(){
- Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
- startActivityForResult(intent,0);
- }
这个非常简单,调用requestLocationUpdates方法设置一个LocationListener定时检测位置而已!
示例代码如下:
布局:activity_location.xml:
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="vertical">
- <TextView
- android:id="@+id/tv_show"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:padding="5dp"
- android:textSize="20sp"
- android:textStyle="bold" />
- </LinearLayout>
LocationActivity.java:
- /**
- * Created by Jay on 2015/11/20 0020.
- */
- public class LocationActivity extends AppCompatActivity {
- private LocationManager lm;
- private TextView tv_show;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_location);
- tv_show = (TextView) findViewById(R.id.tv_show);
- lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
- if (!isGpsAble(lm)) {
- Toast.makeText(LocationActivity.this, "请打开GPS~", Toast.LENGTH_SHORT).show();
- openGPS2();
- }
- //从GPS获取最近的定位信息
- Location lc = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
- updateShow(lc);
- //设置间隔两秒获得一次GPS定位信息
- lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 2000, 8, new LocationListener() {
- @Override
- public void onLocationChanged(Location location) {
- // 当GPS定位信息发生改变时,更新定位
- updateShow(location);
- }
- @Override
- public void onStatusChanged(String provider, int status, Bundle extras) {
- }
- @Override
- public void onProviderEnabled(String provider) {
- // 当GPS LocationProvider可用时,更新定位
- updateShow(lm.getLastKnownLocation(provider));
- }
- @Override
- public void onProviderDisabled(String provider) {
- updateShow(null);
- }
- });
- }
- //定义一个更新显示的方法
- private void updateShow(Location location) {
- if (location != null) {
- StringBuilder sb = new StringBuilder();
- sb.append("当前的位置信息:\n");
- sb.append("精度:" + location.getLongitude() + "\n");
- sb.append("纬度:" + location.getLatitude() + "\n");
- sb.append("高度:" + location.getAltitude() + "\n");
- sb.append("速度:" + location.getSpeed() + "\n");
- sb.append("方向:" + location.getBearing() + "\n");
- sb.append("定位精度:" + location.getAccuracy() + "\n");
- tv_show.setText(sb.toString());
- } else tv_show.setText("");
- }
- private boolean isGpsAble(LocationManager lm) {
- return lm.isProviderEnabled(android.location.LocationManager.GPS_PROVIDER) ? true : false;
- }
- //打开设置页面让用户自己设置
- private void openGPS2() {
- Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
- startActivityForResult(intent, 0);
- }
- }
好的,非常简单,因为gps需要在室外才能用,于是趁着这个机会小跑出去便利店买了杯奶茶, 顺道截下图~
requestLocationUpdates (String provider, long minTime, float minDistance, LocationListener listener)
当时间超过minTime(单位:毫秒),或者位置移动超过minDistance(单位:米),就会调用listener中的方法更新GPS信息,建议这个minTime不小于60000,即1分钟,这样会更加高效而且省电,加入你需要尽可能 实时地更新GPS,可以将minTime和minDistance设置为0
对了,别忘了,你还需要一枚权限:
- <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
嗯,就是固定一个点,当手机与该点的距离少于指定范围时,可以触发对应的处理! 有点像地理围栏...我们可以调用LocationManager的addProximityAlert方法添加临近警告! 完整方法如下:
addProximityAlert(double latitude,double longitude,float radius,long expiration,PendingIntent intent)
属性说明:
示例代码如下:
ProximityActivity.java:
- /**
- * Created by Jay on 2015/11/21 0021.
- */
- public class ProximityActivity extends AppCompatActivity {
- private LocationManager lm;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_proximity);
- lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
- //定义固定点的经纬度
- double longitude = 113.56843;
- double latitude = 22.374937;
- float radius = 10; //定义半径,米
- Intent intent = new Intent(this, ProximityReceiver.class);
- PendingIntent pi = PendingIntent.getBroadcast(this, -1, intent, 0);
- lm.addProximityAlert(latitude, longitude, radius, -1, pi);
- }
- }
还需要注册一个广播接收者:ProximityReceiver.java:
- /**
- * Created by Jay on 2015/11/21 0021.
- */
- public class ProximityReceiver extends BroadcastReceiver{
- @Override
- public void onReceive(Context context, Intent intent) {
- boolean isEnter = intent.getBooleanExtra( LocationManager.KEY_PROXIMITY_ENTERING, false);
- if(isEnter) Toast.makeText(context, "你已到达南软B1栋附近", Toast.LENGTH_LONG).show();
- else Toast.makeText(context, "你已离开南软B1栋附近", Toast.LENGTH_LONG).show();
- }
- }
别忘了注册:
- <receiver android:name=".ProximityReceiver"/>
运行效果图:
PS:好吧,设置了10m,结果我从B1走到D1那边,不止10m了吧...还刚好下雨
好的,本节给大家介绍了Android中GPS定位的一些基本用法,非常简单,内容部分参考的 李刚老师的《Android疯狂讲义》,只是对例子进行了一些修改以及进行了可用性的测试! 本节就到这里,谢谢~