在阅读《第一行代码》和《疯狂android讲义》后,得到的启发式解决方案。 如果仅以GPS_PROVIDER获取location对象,那么在onCreate方法中,location一般都会为空,且在室内无法测试。 所以有了如下简单的解决方案:
package com.example.locationtest;import android.location.Location;import android.location.LocationListener;import android.location.LocationManager;import android.os.Bundle;import android.support.v7.app.ActionBarActivity;import android.widget.TextView;public class MainActivity extends ActionBarActivity {    LocationManager locationManager;    TextView textView;    Location location;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        textView = (TextView) findViewById(R.id.textView);        locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);        // 获取location对象        location = getBestLocation(locationManager);        updateView(location);        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,                3000, 8, new LocationListener() {                    @Override                    public void onStatusChanged(String provider, int status,                            Bundle extras) {                    }                    @Override                    public void onProviderEnabled(String provider) {                        updateView(locationManager                                .getLastKnownLocation(provider));                    }                    @Override                    public void onProviderDisabled(String provider) {                        updateView(null);                    }                    @Override                    public void onLocationChanged(Location location) {                        location = getBestLocation(locationManager);// 每次都去获取GPS_PROVIDER优先的location对象                        updateView(location);                    }                });    }    private void updateView(Location location) {        if (location != null) {            StringBuffer sb = new StringBuffer();            sb.append("位置信息:\n");            sb.append("经度:" + location.getLongitude() + ", 纬度:"                    + location.getLatitude());            textView.setText(sb.toString());        } else {            textView.setText("");        }    }    /**     * 获取location对象,优先以GPS_PROVIDER获取location对象,当以GPS_PROVIDER获取到的locaiton为null时     * ,则以NETWORK_PROVIDER获取location对象,这样可保证在室内开启网络连接的状态下获取到的location对象不为空     *      * @param locationManager     * @return     */    private Location getBestLocation(LocationManager locationManager) {        Location result = null;        if (locationManager != null) {            result = locationManager                    .getLastKnownLocation(LocationManager.GPS_PROVIDER);            if (result != null) {                return result;            } else {                result = locationManager                        .getLastKnownLocation(LocationManager.NETWORK_PROVIDER);                return result;            }        }        return result;    }}
布局文件如下:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:paddingBottom="@dimen/activity_vertical_margin"    android:paddingLeft="@dimen/activity_horizontal_margin"    android:paddingRight="@dimen/activity_horizontal_margin"    android:paddingTop="@dimen/activity_vertical_margin"    tools:context="com.example.locationtest.MainActivity" >    <TextView         android:id="@+id/textView"        android:layout_width="match_parent"        android:layout_height="wrap_content"       /></RelativeLayout>

用到的权限:
 <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/> <uses-permission android:name="android.permission.INTERNET"/>


更多相关文章

  1. 安卓布局被软键盘顶上去的解决方案
  2. Android finished with non-zero exit value 1错误解决方案
  3. Android碎片化问题 java.lang.NoClassDefFoundError: android/os
  4. UE4打包安卓项目遇到UATHelper: Packaging (Android (ETC1)): ER
  5. 找不到android系统库的解决方案
  6. 【Android错误解决方案】解决AndroidStudio在oppo和vivo手机上安
  7. Android 4.4.2插入exFAT格式U盘识别及加载的解决方案

随机推荐

  1. 【实战】如何通过html+css+mysql+php来快
  2. 在使用PHP编程时,使用存储过程还是硬编码S
  3. 如何在php / mysql中使用事务
  4. Zend Studio-8.0.0中文汉化教程及入门教
  5. 使用php codeigniter进行Mysql数据库同步
  6. 填充PHP数组:首先检查索引?
  7. 整理一些Thinkphp开发的实用的一些系统源
  8. Win7安装php7 + apache2.4,成功启动
  9. 速率结构的数据库/算法
  10. apache+php+mysql分离搭建LAMP