【问题标题】:GPS provider enabled but getLastKnownLocation(provider) returns nullGPS 提供程序已启用,但 getLastKnownLocation(provider) 返回 null
【发布时间】:2012-07-30 13:17:29
【问题描述】:

我正在尝试为我的应用使用 GPS 提供程序。

应用使用 GPS 位置提供程序查找设备的当前位置。

该应用程序运行良好。但是,尽管我的 GPS_Provider 已启用,geLastKnownLocation() 返回 null。

MainActivity.java


public class MainActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        LocationManager locationManager;
        String context = Context.LOCATION_SERVICE;
        locationManager = (LocationManager)getSystemService(context);

        String provider = LocationManager.GPS_PROVIDER;
        Location location = locationManager.getLastKnownLocation(provider);

        Log.v("BEFORE", "Location is: " + location);
        updateWithNewLocation(location);
        Log.v("AFTER", "LOCATION FOUND");
    }

    private void updateWithNewLocation(Location location){
        String latLongString;
        TextView myLocationText;
        myLocationText = (TextView)findViewById(R.id.myLocationText);

        if (location != null) {
            double lat = location.getLatitude();
            double lng = location.getLongitude();
            latLongString = "Lat:" + lat + "\nLong:" + lng;
        } 
        else {
            latLongString = "No location found";
        }

        myLocationText.setText("Your Current Position is:\n" + latLongString);
    }

}

Manifest.xml


<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.paad.whereami"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="4"
    android:targetSdkVersion="15" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/title_activity_main" >
            <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>

【问题讨论】:

    标签: android gps provider


    【解决方案1】:

    根据documentation,如果设备不知道最后一个已知位置,则返回null。 GPS可能无法定位您。无论如何,这需要大约一分钟。所以尽量走到外面,在晴朗的天空下,远离高楼,等到 GPS 可以找到你。可能这会有所帮助。并且不要忘记权限。如果你尝试使用GPS_PROVIDER,你不需要互联网访问,也不需要网络状态。

    编辑:

    要让 GPS 在模拟器上工作,请参阅link

    【讨论】:

    • 呵呵问题是我在模拟器上运行应用程序,在我的笔记本电脑上..所以,没有那种舒适:(
    • 我尝试了以上所有方法,但似乎没有任何效果...您是否也遇到过同样的问题,有时?你能告诉我你的结论是什么吗?
    • 好吧,我终于让它工作了 :) 但我必须先添加一些 locationlistener 的东西 :) 无论如何,我很高兴它正在工作 :D
    • 呵呵,np..没想到你会这样做:)
    【解决方案2】:

    对于谁还在尝试理解愚蠢的函数“getLastKnownLocation”。

    此函数仅在 GPS 被其他应用程序使用时返回一些内容。 因此,除非您经常将 GPS 用于其他应用程序,否则它将在 90% 的时间返回 null。 我一直在模拟器和我自己的 Android 手机上对此进行测试。我在使用模拟位置应用程序每 5 秒更改一次坐标时开始获得结果,但是一旦我停止模拟位置应用程序,我就开始得到空结果。

    【讨论】:

      猜你喜欢
      • 2023-03-22
      • 2013-11-06
      • 2013-12-24
      • 1970-01-01
      • 1970-01-01
      • 2012-10-05
      • 2012-05-28
      • 1970-01-01
      相关资源
      最近更新 更多