Android保证首次获取到的location对象不为空的解决方案

本文介绍了一种在Android应用中通过结合GPS_PROVIDER与NETWORK_PROVIDER获取位置信息的方法,确保了无论在室内还是室外都能获取到有效的location对象,提高了应用的定位准确性。通过在onCreate方法中初始化LocationManager并调用getBestLocation方法,实现在不同网络环境下获取最佳位置信息,并利用LocationListener更新UI显示。布局文件与权限声明也一并提供,为开发者实现室内定位功能提供了参考。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在阅读《第一行代码》和《疯狂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"/>


评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值