onLocationChanged()从未调用过

时间:2012-08-13 12:01:02

标签: android gps locationmanager

我正在编写一个带来当前位置的代码,但它没有给我这个位置,因为它从不调用onLocationChanged()。是否有任何方法可以找到该位置。

对于这种情况,

mobileLocation将为0,因此执行将转到else块。

我的代码是

public class FindLocation {
private LocationManager locManager;
private LocationListener locListener;
private Location mobileLocation;
private String provider;

public FindLocation(Context ctx){
    locManager = (LocationManager) ctx.getSystemService(Context.LOCATION_SERVICE);
    locListener = 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(Location location) {
            System.out.println("mobile location is in listener="+location);
            mobileLocation = location;
        }
    };
    locManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 1, locListener);              
    if (mobileLocation != null) {
        locManager.removeUpdates(locListener); 
        String londitude = "Londitude: " + mobileLocation.getLongitude();
        String latitude = "Latitude: " + mobileLocation.getLatitude();
        String altitiude = "Altitiude: " + mobileLocation.getAltitude();
        String accuracy = "Accuracy: " + mobileLocation.getAccuracy();
        String time = "Time: " + mobileLocation.getTime();
        Toast.makeText(ctx, "Latitude is = "+latitude +"Longitude is ="+londitude, Toast.LENGTH_LONG).show();
    } else {
        System.out.println("in find location 4");
        Toast.makeText(ctx, "Sorry location is not determined", Toast.LENGTH_LONG).show();
    }
}
   }

7 个答案:

答案 0 :(得分:10)

我在真实设备上运行我的应用程序。 我使用网络而不是GPS,并且onLocationChanged被称为:

locMan.requestSingleUpdate(LocationManager.NETWORK_PROVIDER, this, null);

答案 1 :(得分:6)

您是否从未获得过某个位置或只是在您的评论中写下“有时它会获得位置但不会在点击时”。

可能是你的代码比LocationManager更快,而LocationManager可能还没有调用onLocationChanged()。更改代码以获取最后的已知位置,或者等到调用locListener:

locManager.requestLocationUpdates(
  LocationManager.GPS_PROVIDER, 1000, 1, locListener);
mobileLocation = locManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (mobileLocation != null) {

答案 2 :(得分:4)

我忘记了你的许可

<uses-permission android:name="android.permission.ACCESS_GPS" />
        <uses-permission android:name="android.permission.ACCESS_LOCATION" />
        <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" />

OR

声明适当的类,如

public class MyLocationListener implements LocationListener {
@Override
  public void onLocationChanged(Location arg0) {
    latitude = arg0.getLatitude();
    longitude = arg0.getLongitude();
  }

}

答案 3 :(得分:0)

locManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 1, locListener)之后,添加以下代码:

locManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 1000, 1, locListener);

编辑:感谢@Fabian Streitel的评论更正了代码。

答案 4 :(得分:0)

import android.app.ActivityManager;
import android.app.Service;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.media.AudioManager;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.os.IBinder;
import android.os.PowerManager;
import android.util.Log;

import org.json.JSONObject;

import java.util.List;


public class AndroidLocationServices  extends Service {

    PowerManager.WakeLock wakeLock;
    String MODULE="AndroidLocationServices",TAG="";
    private LocationManager locationManager;
    double getLattitude=0,getLogitude=0;
    private static final long MIN_TIME_BW_UPDATES = 1; // 1 minute
    private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 0;


    public AndroidLocationServices() {
        // TODO Auto-generated constructor stub
    }

    @Override
    public IBinder onBind(Intent arg0) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public void onCreate() {
        // TODO Auto-generated method stub
        super.onCreate();
        preferences=getSharedPreferences(GlobalConfig.PREF_NAME, MODE_PRIVATE);
        PowerManager pm = (PowerManager) getSystemService(this.POWER_SERVICE);

        wakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "DoNotSleep");

        // Toast.makeText(getApplicationContext(), "Service Created",
        // Toast.LENGTH_SHORT).show();

        Log.e("Google", "Service Created");

    }

    @Override
    @Deprecated
    public void onStart(Intent intent, int startId) {
        // TODO Auto-generated method stub
        super.onStart(intent, startId);

        Log.e("Google", "Service Started");

        locationManager = (LocationManager) getApplicationContext().getSystemService(Context.LOCATION_SERVICE);

        //locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 2500, 0, listener);
        locationManager.requestLocationUpdates( LocationManager.NETWORK_PROVIDER,  MIN_TIME_BW_UPDATES,  MIN_DISTANCE_CHANGE_FOR_UPDATES, listener);


    }



    public LocationListener listener = new LocationListener() {

        @Override
        public void onLocationChanged(Location location) {
            // TODO Auto-generated method stub

            Log.e("Google", "Location Changed");
            location.setAccuracy(100);

            if (location == null)
                return;
                    getLattitude=location.getLatitude();
                    getLogitude=location.getLongitude();

                    Log.e("latitude", getLattitude + "");
                    Log.e("longitude", getLogitude + "");


        }

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

        }

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

        }

        @Override
        public void onStatusChanged(String provider, int status, Bundle extras) {
            // TODO Auto-generated method stub

        }
    };

    @Override
    public void onDestroy() {
        // TODO Auto-generated method stub
        super.onDestroy();

//        wakeLock.release();

    }


}

清单:

  <service android:name=".services.AndroidLocationServices"/>

使用前启动服务:

 startService(new Intent(this, AndroidLocationServices.class));

答案 5 :(得分:0)

在我的情况下,应用程序不是要求权限。虽然我在menifest文件中添加了权限。所以我无法允许我的应用访问该位置。

作为修复,我手动转到:Settings->Permissions->Your location并启用了我的欠开发应用的位置权限。这解决了我的问题。

我认为只有在通过Android Studio安装应用时才会出现此问题。我希望从Google Play安装应用时不会发生这种情况。

答案 6 :(得分:0)

LocationManager.NETWORK_PROVIDER需要网络,但这是真实的,并不精确;如果您想使用LocationManager.GPS_PROVIDER,则情况必须是室外而不是室内,因为GPS位置需要卫星,如果您在任何建筑物中,卫星都找不到您!