Android onLocationChanged() 从未调用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11933918/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
onLocationChanged() never called
提问by abhishek ameta
I am writing a code that brings current location but it is not giving me the location because it never calls onLocationChanged()
.Is there any way to find the location.
我正在编写一个带当前位置的代码,但它没有给我位置,因为它从不调用onLocationChanged()
。有没有办法找到位置。
mobileLocation is coming 0 for this case, so the execution goes to the else block.
对于这种情况, mobileLocation 将变为 0,因此执行转到 else 块。
My code is
我的代码是
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();
}
}
}
采纳答案by EPSG31468
Do you never get a location or only as you write in your comment "sometimes it gets location but not at the time of click."?
您是否永远不会获得位置,或者仅在您在评论中写道“有时它会获得位置但不是在点击时”?
Might be that your code is faster than LocationManager, which might not yet have called onLocationChanged(). Change your code in order to get the last known location, or wait until your locListener was called:
可能是您的代码比 LocationManager 更快,后者可能尚未调用 onLocationChanged()。更改您的代码以获取最后一个已知位置,或等到您的 locListener 被调用:
locManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER, 1000, 1, locListener);
mobileLocation = locManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (mobileLocation != null) {
回答by Gal Rom
i run my app on real device . i use network instead of GPS and onLocationChanged is called:
我在真实设备上运行我的应用程序。我使用网络而不是 GPS 并且 onLocationChanged 被称为:
locMan.requestSingleUpdate(LocationManager.NETWORK_PROVIDER, this, null);
回答by ckpatel
I thing you forgot permission
我想你忘记了许可
<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
或者
Declare Proper Class like
声明适当的类
public class MyLocationListener implements LocationListener {
@Override
public void onLocationChanged(Location arg0) {
latitude = arg0.getLatitude();
longitude = arg0.getLongitude();
}
}
回答by Tom
LocationManager.NETWORK_PROVIDER
need network, but it is real, not precise; if you want to use LocationManager.GPS_PROVIDER
, the situation must be outdoor instead of indoor, because GPS location need satellite, if you are in any building, the satellite cannot find you!
LocationManager.NETWORK_PROVIDER
需要网络,但它是真实的,而不是精确的;如果你想使用LocationManager.GPS_PROVIDER
,必须在室外而不是室内,因为GPS定位需要卫星,如果你在任何建筑物里,卫星都找不到你!
回答by Ayaz Alifov
Directly after locManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 1, locListener)
, add the following code:
直接在 之后locManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 1, locListener)
,添加以下代码:
locManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 1000, 1, locListener);
EDIT: corrected the code thanks to the comment by @Fabian Streitel.
编辑:由于@Fabian Streitel 的评论更正了代码。
回答by ViramP
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();
}
}
Manifest:
显现:
<service android:name=".services.AndroidLocationServices"/>
Start Service before use:
使用前启动服务:
startService(new Intent(this, AndroidLocationServices.class));
回答by Adil Malik
In my case the app was not asking for permissions. Although I have added the permissions in menifest file. So I was not able to allow my app to access the location.
在我的情况下,应用程序没有请求权限。虽然我在menifest文件中添加了权限。所以我无法允许我的应用访问该位置。
As a fix, I manually went to: Settings->Permissions->Your location
and enabled the location permission for my under-development app. And that fixed my problem.
作为修复,我手动转到:Settings->Permissions->Your location
并为我正在开发的应用程序启用了位置权限。这解决了我的问题。
I assume that this issue is only when the app is installed through Android Studio. I hope it will not occur when the app is installed from Google Play.
我假设只有当应用程序是通过 Android Studio 安装时才会出现这个问题。我希望从 Google Play 安装该应用程序时不会发生这种情况。