我写的是带来了当前位置的代码,但它是不是给我的位置,因为它从来不打电话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();
}
}
}
你永远不会得到一个位置,或者仅作为您在您的评论写“有时它得到的位置,但没有点击的时间。”?
可能是你的代码比的LocationManager,这可能尚未被称为onLocationChanged()更快。 更改您的代码,以获得最后已知的位置,或者等到locListener被称为:
locManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER, 1000, 1, locListener);
mobileLocation = locManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (mobileLocation != null) {
我跑我在真机上的应用程序。 我使用网络,而不是GPS和onLocationChanged被称为:
locMan.requestSingleUpdate(LocationManager.NETWORK_PROVIDER, this, null);
我的事情你忘了许可
<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" />
要么
声明适当的类象
public class MyLocationListener implements LocationListener {
@Override
public void onLocationChanged(Location arg0) {
latitude = arg0.getLatitude();
longitude = arg0.getLongitude();
}
}
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));
后直接locManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 1, locListener)
中,添加以下代码:
locManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 1000, 1, locListener);
编辑:纠正代码要归功于@Fabian Streitel评论。
在我的情况下,应用程序是没有要求的权限。 虽然我已经添加在menifest文件的权限。 所以,我不能让我的应用程序来访问该位置。
作为一个解决方法,我手动去: Settings->Permissions->Your location
并启用位置同意我欠开发应用程序。 这固定我的问题。
我认为只有当应用程序通过Android Studio中安装了此问题。 我希望当应用程序从谷歌播放安装就不会发生。