Android LocationManager从不触发,纬度和经度
我需要获取当前位置,将它们保存到Android LocationManager从不触发,纬度和经度,android,broadcastreceiver,android-location,Android,Broadcastreceiver,Android Location,我需要获取当前位置,将它们保存到捆绑包中,并将它们转发到广播接收器,该接收器将每5秒触发一次。这就是我构建代码的方式 public class GPSServiceActivity extends Activity { public Bundle locationBundle; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setC
捆绑包中
,并将它们转发到广播接收器
,该接收器将每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"/>
这就产生了
network
gps
passive
您可以检查提供程序是否已启用。如果未启用,请要求用户启用。检查updateLocationRequest调用中的最后一个参数。什么是mlocListener?它是位置侦听器类(MyLocationListener)的对象吗
这是中的方法,你在emulator上测试它吗?是的,正如上面的家伙所说,如果你在emulator上测试它,那么你需要向emulator发送一个测试位置,否则它就会出错。签出:将位置提供程序设置移动到onStart,而不是onCreate;检查您的提供程序是否已启用:locationManager.isProviderEnabled(locationManager.NETWORK\u提供程序)@TanjaV网络提供程序已启用。如果提供程序是
network\u provider
,则用户不需要启用该提供程序,就像我的例子一样。请尝试getLastKnownLocation()?我不知道这是否有帮助,只是想看看你是否有任何位置!这与我的问题无关。你说:“我需要获取当前位置…”onLocationChanged
。。。你检查过这个事件吗。。。我用它来定位。。。onSensorChanged。。。
network
gps
passive
MyLocationOverlay