位置管理器请求位置更新无法正常工作

5
我有一个令人毛骨悚然的问题。我正在尝试获取模拟器的位置。当我获取模拟器的位置时,一切正常。但是当我更改位置坐标时,没有任何反应。gps 运行良好。
以下是我的代码: Main.java
import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;

import android.util.Log;
import android.view.Menu;
import android.widget.TextView;

public class Main extends Activity {
TextView tvStatus;
LocationManager lm;
boolean gpsEnabled;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    tvStatus = (TextView) findViewById(R.id.textView1);

    lm = (LocationManager) getSystemService(LOCATION_SERVICE);
    if (lm.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
        Log.d("", "GPS is Active");
        Location l = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
        tvStatus.setText(l.getLatitude()+" , "+l.getLongitude());
        lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0,
                new LocationListener() {

                    @Override
                    public void onStatusChanged(String arg0, int arg1,
                            Bundle arg2) {
                        // TODO Auto-generated method stub

                    }

                    @Override
                    public void onProviderEnabled(String arg0) {
                        // TODO Auto-generated method stub

                    }

                    @Override
                    public void onProviderDisabled(String arg0) {
                        // TODO Auto-generated method stub

                    }

                    @Override
                    public void onLocationChanged(Location arg0) {
                        tvStatus.setText("GPS ENABLED: " + gpsEnabled
                                + "\nLatitude: " + arg0.getLatitude()
                                + "\nLongitude: " + arg0.getLongitude());
                    }
                });
    }

}

    }

Manifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.enabelinglocationprovider"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="5"
        android:targetSdkVersion="10" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
    <uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION"/>
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
    <uses-permission android:name="android.permission.INTERNET"/>

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.enabelinglocationprovider.Main"
            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>

</manifest>

该设备是否已开启位置相关服务? - Pinki
是的,GPS已启用。还有一件事,我一直在Eclipse中切换我的工作区,现在当我重新启动模拟器时,它可以正常工作了。 - Usman Riaz
当我切换工作区时,模拟器会断开连接吗? - Usman Riaz
2个回答

7
三星手机存在这个问题。有一个方法可以解决。你需要踢一下位置管理器,从地图缓存中获取最新的位置信息。
God.locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0,
            new LocationListener() {
                @Override
                public void onStatusChanged(String provider, int status, Bundle extras) {
                }

                @Override
                public void onProviderEnabled(String provider) {
                }

                @Override
                public void onProviderDisabled(String provider) {
                }

                @Override
                public void onLocationChanged(final Location location) {
                }
            });
    currentLocation = God.locationManager
            .getLastKnownLocation(LocationManager.NETWORK_PROVIDER);

在使用 0 的参数启动 requestLocationUpdates 后,调用 getLastKnownLocation

我也不得不为安卓版本为5.1.1的Nexus 5完成这个任务。 - gilsaints88
这和上帝有什么关系? - jack

1

这种情况通常出现在模拟器中,尝试重新启动模拟器。

或者,关闭Eclipse和模拟器,再重新启动两者。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接