无法获取用户位置 [英] Couldn't get user location
问题描述
我试图获取用户的位置无论是网络位置或 GPS
位置或两者兼而有之。对于我运行它实现 LocationListener的
后台服务。我添加了所有需要的权限在manifest文件还。这里是我的服务
I am trying to get user location either network location or gps
location or both. for that i am running a background service which implements LocationListener
. i added all the require permissions in the manifest file also. here is my service
public class GetLocation extends Service implements LocationListener {
LocationManager lm;
Location networkLocation, gpsLocation;
@Override
public void onCreate() {
super.onCreate();
lm = (LocationManager) getSystemService(LOCATION_SERVICE);
if (lm.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
gpsLocation = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
Toast.makeText(getApplicationContext(), "Getting Location Via GPS",
Toast.LENGTH_LONG).show();
}
if (lm.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) {
lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0,
this);
networkLocation = lm
.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
Toast.makeText(getApplicationContext(),
"Getting location Via Network", Toast.LENGTH_LONG).show();
}
if (gpsLocation != null) {
Toast.makeText(
getApplicationContext(),
"GPSLOCATION: " + gpsLocation.getLatitude() + " , "
+ gpsLocation.getLongitude(), Toast.LENGTH_LONG)
.show();
}
if (networkLocation != null) {
Toast.makeText(
getApplicationContext(),
"NETWORKLOCATION: " + networkLocation.getLatitude() + " , "
+ networkLocation.getLongitude(), Toast.LENGTH_LONG)
.show();
}
if (gpsLocation == null && networkLocation == null) {
Toast.makeText(getApplicationContext(),
"Couldn't get user location", Toast.LENGTH_LONG).show();
}
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
new Thread() {
public void run() {
Looper.prepare();
if (gpsLocation != null) {
Log.d("Usman", "GPSLOCATION: " + gpsLocation.getLatitude()
+ " , " + gpsLocation.getLongitude());
}
if (networkLocation != null) {
Log.d("Usman",
"NETWORKLOCATION: " + networkLocation.getLatitude()
+ " , " + networkLocation.getLongitude());
}
if (gpsLocation == null && networkLocation == null) {
Log.d("Usman", "Couldn't get user lcoation");
}
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
Looper.loop();
}
}.start();
return START_STICKY;
}
@Override
public IBinder onBind(Intent arg0) {
return null;
}
@Override
public void onLocationChanged(Location location) {
// do something with this location
Toast.makeText(
getApplicationContext(),
location.getProvider() + " Location Changed:"
+ location.getLatitude() + " , "
+ location.getLongitude(), Toast.LENGTH_LONG).show();
}
@Override
public void onProviderDisabled(String arg0) {
Toast.makeText(getApplicationContext(), arg0 + " Disabled",
Toast.LENGTH_LONG).show();
}
@Override
public void onProviderEnabled(String provider) {
Toast.makeText(getApplicationContext(), provider + " Enabled",
Toast.LENGTH_LONG).show();
}
@Override
public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
}
}
在的onCreate()方法,GPS和网络位置为空。 任何机构可以告诉我,我错了。
in onCreate() method, both gps and network locations are null. can any body tell me where am i wrong
推荐答案
正如你所说,你甚至没有得到获取位置通过网络的敬酒。所以你的执行是不会的,如果块中
As you said, you are not even getting toast of "Getting location Via Network". So your execution is not going inside the if block of
if (lm.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) {
}
因此,请确保您已检查
So make sure you have checked
谷歌位置服务 - 经过
Google's location services - Checked
在您的设备设置。
设置>定位服务>谷歌的定位服务和访问位置。
Settings > location services > Google's location services and Access Location.
那么你应该从 LocationManager
位置。你的code否则其余看起来不错。
Then you should get location from LocationManager
. Else rest of your code looks fine.
希望这有助于。
<一个href="http://stackoverflow.com/questions/10154937/isproviderenabledlocationmanager-network-provider-return-false/15993514#15993514">Reference链接
这篇关于无法获取用户位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!