【问题标题】:LocationManager never triggers, Latitude and LongitudeLocationManager 从不触发,纬度和经度
【发布时间】:2012-09-26 09:53:28
【问题描述】:

我需要获取当前位置,将它们保存到 Bundle 并将它们转发到 BroadCastReceiver,它将每 5 秒触发一次。这就是我构建代码的方式

public class GPSServiceActivity extends Activity {

public Bundle locationBundle;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    locationBundle = new Bundle();
    Calendar cal = Calendar.getInstance();

    getCurrentLocation();

    Intent intent = new Intent(GPSServiceActivity.this, GPSHandler.class);
    intent.putExtra("Latitude", locationBundle.getDouble("Latitude"));
    intent.putExtra("Longitude", locationBundle.getDouble("Longitude"));
    // In reality, you would want to have a static variable for the request
    // code instead of 192837
    PendingIntent sender = PendingIntent.getBroadcast(this, 192837, intent,
            PendingIntent.FLAG_UPDATE_CURRENT);

    // Get the AlarmManager service
    AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
    am.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), sender);
    am.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), 5000,
            sender);
}

public void getCurrentLocation() {
    LocationManager mlocManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    LocationListener mlocListener = new MyLocationListener();
    mlocManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,
            5000, 0, mlocListener);

}

/* Class My Location Listener */
public class MyLocationListener implements LocationListener {



    public void onLocationChanged(Location loc) {

        double Latitude = loc.getLatitude();
        double Longitude = loc.getLongitude();

        locationBundle.putDouble("Longitude", Longitude);
        locationBundle.putDouble("Latitude", Latitude);

    }

    public void onProviderDisabled(String arg0) {

    }

    public void onProviderEnabled(String arg0) {

    }

    public void onStatusChanged(String arg0, int arg1, Bundle arg2) {

    }
}

}

我的Manifest.xml

<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.INTERNET"/>

经过一段时间的代码调试,我发现MyLocationListener 永远不会触发,Bundle locationBundle 只包含两个值为 0.0 和 0.0 的 double。有人可以给我一个关于如何启动和运行的提示吗?

编辑

仅出于测试目的,我编写了此代码

List<String> providers = mlocManager.getAllProviders();

        if(providers.size() > 0) {
            for(int i = 0; i < providers.size(); i++) {
                Log.i("Providers: ", providers.get(i).toString());
            }
        }
        else
            Log.i("Providers: ", "No providers");

这个输出

network
gps
passive

【问题讨论】:

  • 你在模拟器上测试它吗?
  • 是的,正如上面那个人所说,如果你在模拟器上测试它,那么你需要向模拟器发送一个测试位置,否则它就会出错。签出:stackoverflow.com/questions/4950389/…
  • 将您的 LOcation Provider 设置移动到 onStart 而不是 onCreate;检查您的提供商是否已启用:locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
  • @TanjaV 网络提供商已启用。

标签: android broadcastreceiver android-location


【解决方案1】:

您可以检查是否启用了提供程序。如果没有,请用户启用。检查 updateLocationRequest 调用中的最后一个参数。什么是 mlocListener?它是您的位置侦听器类(MyLocationListener)的对象吗?

【讨论】:

  • 用户不需要启用提供者,如果提供者是NETWORK_PROVIDER,就像我的例子一样。
  • 试试 getLastKnownLocation() 吗?我不知道这是否有帮助,只是为了检查您是否有任何位置!
【解决方案2】:
MyLocationOverlay

google maps API中的方法

【讨论】:

  • 这与我的问题无关。
  • 您说:“我需要获取当前位置....” onLocationChanged ...您检查此事件吗? ...我用它来定位... onSensorChanged ...
猜你喜欢
  • 1970-01-01
  • 2016-09-15
  • 1970-01-01
  • 2023-03-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多