LocationManager requestLocationUpdates not working

匿名 (未验证) 提交于 2019-12-03 08:46:08

问题:

I Have This Creepy problem. i am trying to get the location of my emulator. it working fine when i get the location of my emulator. but when i change my location coordinates nothing happens. gps is working fine.

Here is my code

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> 

回答1:

Samsung phones have this problem. There is hack to this. You need to kick the locationmanager on the butt to get the latest location from the maps cache.

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); 

Call getLastKnownLocation after kicking the requestLocationUpdates with 0's.



回答2:

It happens with emulators, try rebooting the emulator.

Or, close both eclipse and emulator and start both again



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!