获取位置 Android
Get Location Android
我有一项服务可以获取我 phone 的位置。当我关闭 GPS 并再次打开它时,我无法再获取位置。
有时 return 将纬度和经度设为空但 GPS 开启...
我选中它时有一个复选框,它开始向我的服务器发送我设备的纬度和经度。
这是代码。有人可以帮我优化我的代码吗?
服务
public class LocalizationService extends Service implements Runnable {
private static final int tempo = (30 * 1000); // 1800
private LocationManager locationManager = null;
private Thread t;
@Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
}
@Override
public void onCreate() {
super.onCreate();
}
@Override
public void onStart(Intent intent, int startId) {
}
@Override
public void onDestroy() {
super.onDestroy();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
if (t == null || !t.isAlive()) {
t = new Thread(this, "ServicoId: " + startId);
t.start();
}
return Service.START_STICKY;
}
@Override
public void run() {
// TODO Auto-generated method stub
while (true) {
try {
sendLocation();
Thread.sleep(tempo);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public void sendLocation() {
LatLng latlong = getLocation();
PolifreteApplication manager = (PolifreteApplication) getApplicationContext();
Veiculo veiculo = manager.getTransportador().getVeiculo();
int user = manager.getTransportador().getCodigo();
if (latlong == null) {
} else {
veiculo.setLatitude(latlong.latitude + "");
veiculo.setLongitude(latlong.longitude + "");
VeiculoDAL.SendLocationVeiculo(veiculo, user);
}
}
public LatLng getLocation() {
// Get the location manager
LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
Criteria criteria = new Criteria();
String bestProvider = locationManager.getBestProvider(criteria, false);
Location location = locationManager.getLastKnownLocation(bestProvider);
Double lat, lon;
try {
lat = location.getLatitude();
lon = location.getLongitude();
return new LatLng(lat, lon);
} catch (NullPointerException e) {
e.printStackTrace();
return null;
}
}
}
ACTIVITY 方法
public void onCheckboxClicked(View view) {
if (WebService.conexaoOk()) {
CheckBox cblocalizacao = (CheckBox) findViewById(R.id.checkboxlocalizacao);
boolean checked = ((CheckBox) view).isChecked();
locationManager = (LocationManager) this
.getSystemService(Context.LOCATION_SERVICE);
boolean isGPSEnabled = locationManager
.isProviderEnabled(LocationManager.GPS_PROVIDER);
switch (view.getId()) {
case R.id.checkboxlocalizacao:
if (checked && !isGPSEnabled) {
cblocalizacao.setChecked(false);
showSettingsAlert();
} else if (checked && isGPSEnabled) {
Intent i = new Intent(this, LocalizationService.class);
this.startService(i);
} else {
cblocalizacao.setChecked(false);
}
}
}
}
如果您想从 Gps 获取位置,请参阅此 link,http://www.androidhive.info/2012/07/android-Gps-location-manager-tutorial/,从 Gps 获取位置不是即时的,您需要实现位置侦听器并等待 Gps 的回调提供商:
locationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER,
MIN_TIME_BW_UPDATES,
MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
或者您可以使用网络提供商
locationManager.requestLocationUpdates(
LocationManager.NETWORK_PROVIDER,
MIN_TIME_BW_UPDATES,
MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
只需访问 link,您就会找到获得最准确位置的最佳策略..
Udacity 的 This video 提供了有关在 GoogleApiClient 中使用定位服务的想法。
由于您的 LocationListener 尝试仅使用网络而非 GPS 获取位置,因此使用以下代码片段:locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER)
可能您正在获取纬度和经度值,但有延迟。我遇到了同样的问题,然后我开始使用 new/latest 定位服务 API 并使用:
谷歌 API 客户端。
首先你需要实现
GoogleApiClient.ConnectionCallbacks,
GoogleApiClient.OnConnectionFailedListener,
LocationListener
你activity想要获取位置数据。
定义
private GoogleApiClient mGoogleApiClient;
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addApi(LocationServices.API)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.build();
如果你还没有添加
<uses-permission `android:name="android.permission.ACCESS_FINE_LOCATION"/>`
在清单文件中添加。
更多文档:https://developers.google.com/android/reference/com/google/android/gms/common/api/GoogleApiClient
关于 GoogleClientApi 的更全面的回答:
我有一项服务可以获取我 phone 的位置。当我关闭 GPS 并再次打开它时,我无法再获取位置。
有时 return 将纬度和经度设为空但 GPS 开启...
我选中它时有一个复选框,它开始向我的服务器发送我设备的纬度和经度。
这是代码。有人可以帮我优化我的代码吗?
服务
public class LocalizationService extends Service implements Runnable {
private static final int tempo = (30 * 1000); // 1800
private LocationManager locationManager = null;
private Thread t;
@Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
}
@Override
public void onCreate() {
super.onCreate();
}
@Override
public void onStart(Intent intent, int startId) {
}
@Override
public void onDestroy() {
super.onDestroy();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
if (t == null || !t.isAlive()) {
t = new Thread(this, "ServicoId: " + startId);
t.start();
}
return Service.START_STICKY;
}
@Override
public void run() {
// TODO Auto-generated method stub
while (true) {
try {
sendLocation();
Thread.sleep(tempo);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public void sendLocation() {
LatLng latlong = getLocation();
PolifreteApplication manager = (PolifreteApplication) getApplicationContext();
Veiculo veiculo = manager.getTransportador().getVeiculo();
int user = manager.getTransportador().getCodigo();
if (latlong == null) {
} else {
veiculo.setLatitude(latlong.latitude + "");
veiculo.setLongitude(latlong.longitude + "");
VeiculoDAL.SendLocationVeiculo(veiculo, user);
}
}
public LatLng getLocation() {
// Get the location manager
LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
Criteria criteria = new Criteria();
String bestProvider = locationManager.getBestProvider(criteria, false);
Location location = locationManager.getLastKnownLocation(bestProvider);
Double lat, lon;
try {
lat = location.getLatitude();
lon = location.getLongitude();
return new LatLng(lat, lon);
} catch (NullPointerException e) {
e.printStackTrace();
return null;
}
}
}
ACTIVITY 方法
public void onCheckboxClicked(View view) {
if (WebService.conexaoOk()) {
CheckBox cblocalizacao = (CheckBox) findViewById(R.id.checkboxlocalizacao);
boolean checked = ((CheckBox) view).isChecked();
locationManager = (LocationManager) this
.getSystemService(Context.LOCATION_SERVICE);
boolean isGPSEnabled = locationManager
.isProviderEnabled(LocationManager.GPS_PROVIDER);
switch (view.getId()) {
case R.id.checkboxlocalizacao:
if (checked && !isGPSEnabled) {
cblocalizacao.setChecked(false);
showSettingsAlert();
} else if (checked && isGPSEnabled) {
Intent i = new Intent(this, LocalizationService.class);
this.startService(i);
} else {
cblocalizacao.setChecked(false);
}
}
}
}
如果您想从 Gps 获取位置,请参阅此 link,http://www.androidhive.info/2012/07/android-Gps-location-manager-tutorial/,从 Gps 获取位置不是即时的,您需要实现位置侦听器并等待 Gps 的回调提供商:
locationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER,
MIN_TIME_BW_UPDATES,
MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
或者您可以使用网络提供商
locationManager.requestLocationUpdates(
LocationManager.NETWORK_PROVIDER,
MIN_TIME_BW_UPDATES,
MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
只需访问 link,您就会找到获得最准确位置的最佳策略..
This video 提供了有关在 GoogleApiClient 中使用定位服务的想法。
由于您的 LocationListener 尝试仅使用网络而非 GPS 获取位置,因此使用以下代码片段:locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER)
可能您正在获取纬度和经度值,但有延迟。我遇到了同样的问题,然后我开始使用 new/latest 定位服务 API 并使用:
谷歌 API 客户端。
首先你需要实现
GoogleApiClient.ConnectionCallbacks,
GoogleApiClient.OnConnectionFailedListener,
LocationListener
你activity想要获取位置数据。
定义
private GoogleApiClient mGoogleApiClient;
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addApi(LocationServices.API)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.build();
如果你还没有添加
<uses-permission `android:name="android.permission.ACCESS_FINE_LOCATION"/>`
在清单文件中添加。
更多文档:https://developers.google.com/android/reference/com/google/android/gms/common/api/GoogleApiClient
关于 GoogleClientApi 的更全面的回答: