2012-10-05 40 views
0

我需要获取当前位置,将它们保存到Bundle,并将它们转发到BroadCastReceiver,每隔5秒触发一次。这就是我如何组织我的代码LocationManager从不触发,纬度和经度

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。有人可以给我一个关于如何启动和运行的提示吗?

编辑

只是用于测试目的,我写了这个代码

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 
+0

方法你在模拟器中进行测试呢? –

+0

是的,就像上面的人说的那样,如果你在模拟器上测试它,那么你需要发送一个测试位置给模拟器,否则它就会出错。检查出:http://stackoverflow.com/questions/4950389/android-emulator-finding-mock-user-location-coordinates-having-problems –

+0

我正在测试它的电话。 –

回答

0

如果启用提供商可以检查。要求用户启用,如果没有。检查updateLocationRequest调用中的最后一个参数。什么是mlocListener?它是你的位置监听器类(MyLocationListener)的对象吗?

+1

用户不需要启用提供程序,如果提供程序是“NETWORK_PROVIDER”,就像我的情况一样。 –

+0

试试getLastKnownLocation()?我不知道这是否有帮助,但只是为了检查你是否有任何位置! – Stashi

0
MyLocationOverlay 

它是在google maps API

+0

这与我的问题无关。 –

+0

你说:“我需要得到当前的位置....”onLocationChanged' ...你检查这个事件吗? ...我用它来命名... onSensorChanged ... –