在我的Android应用程序,我想同时使用GPS提供商和网络提供商的位置更新,持续5秒。首先,我想用GPS提供商(因为它给了精确的结果),如果GPS提供商返回经度和纬度为0.0和0.0,那么我想用网络供应商,但我不希望同时使用的供应商同时。有什么建议?
解决方案 包loca.loca;
进口java.util.Timer中;
进口java.util.TimerTask中;
进口android.app.Activity;
进口android.content.Context;
进口android.location.Location;
进口android.location.LocationListener;
进口android.location.LocationManager;
进口android.os.Bundle;
进口android.widget.Toast;
公共类LocationActivity延伸活动{
双X,Y;
定时器定时;
LocationManager流明;
布尔gps_enabled = FALSE;
布尔network_enabled = FALSE;
/ **第一次创建活动时调用。 * /
@覆盖
公共无效的onCreate(包savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.main);
LM =(LocationManager)this.getSystemService(Context.LOCATION_SERVICE);
gps_enabled = lm.isProviderEnabled(LocationManager.GPS_PROVIDER);
network_enabled = lm.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
如果(gps_enabled&安培;!&安培;!network_enabled){语境上下文= getApplicationContext();
INT持续时间= Toast.LENGTH_SHORT;
吐司面包= Toast.makeText(背景下,没有什么是启用,持续时间);
toast.show();
}
如果(gps_enabled)
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER,0,0,
locationListenerGps);
如果(network_enabled)
lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,0,0,
locationListenerNetwork);
定时器=新的Timer();
timer.schedule(新GetLastLocation(),20000);
}
LocationListener的locationListenerGps =新LocationListener的(){
公共无效onLocationChanged(位置定位){
timer.cancel();
X = location.getLatitude();
Y = location.getLongitude();
lm.removeUpdates(本);
lm.removeUpdates(locationListenerNetwork);
上下文的背景下= getApplicationContext();
INT持续时间= Toast.LENGTH_SHORT;
吐司面包= Toast.makeText(背景下,GPS功能的+ X +\ N+ Y,持续时间);
toast.show();
}
公共无效onProviderDisabled(字符串提供商){
}
公共无效onProviderEnabled(字符串提供商){
}
公共无效onStatusChanged(字符串商,INT地位,捆绑演员){
}
};
LocationListener的locationListenerNetwork =新LocationListener的(){
公共无效onLocationChanged(位置定位){
timer.cancel();
X = location.getLatitude();
Y = location.getLongitude();
lm.removeUpdates(本);
lm.removeUpdates(locationListenerGps);
上下文的背景下= getApplicationContext();
INT持续时间= Toast.LENGTH_SHORT;
吐司面包= Toast.makeText(背景下,启用网络+ X +\ N+ Y,持续时间);
toast.show();
}
公共无效onProviderDisabled(字符串提供商){
}
公共无效onProviderEnabled(字符串提供商){
}
公共无效onStatusChanged(字符串商,INT地位,捆绑演员){
}
};
类GetLastLocation扩展的TimerTask {
@覆盖
公共无效的run(){
lm.removeUpdates(locationListenerGps);
lm.removeUpdates(locationListenerNetwork);
位置net_loc = NULL,gps_loc = NULL;
如果(gps_enabled)
gps_loc = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
如果(network_enabled)
net_loc = lm.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
//如果有两个值,用最新一期
如果(gps_loc = NULL和放大器;!&安培;!net_loc = NULL){
如果(gps_loc.getTime()> net_loc.getTime())
{X = gps_loc.getLatitude();
Y = gps_loc.getLongitude();
上下文的背景下= getApplicationContext();
INT持续时间= Toast.LENGTH_SHORT;
吐司面包= Toast.makeText(背景下,GPS lastknown+ X +\ N+ Y,持续时间);
toast.show();
}
其他
{X = net_loc.getLatitude();
Y = net_loc.getLongitude();
上下文的背景下= getApplicationContext();
INT持续时间= Toast.LENGTH_SHORT;
吐司面包= Toast.makeText(背景下,网络lastknown+ X +\ N+ Y,持续时间);
toast.show();
}
}
如果(gps_loc!= NULL){
{X = gps_loc.getLatitude();
Y = gps_loc.getLongitude();
上下文的背景下= getApplicationContext();
INT持续时间= Toast.LENGTH_SHORT;
吐司面包= Toast.makeText(背景下,GPS lastknown+ X +\ N+ Y,持续时间);
toast.show();
}
}
如果(net_loc!= NULL){
{X = net_loc.getLatitude();
Y = net_loc.getLongitude();
上下文的背景下= getApplicationContext();
INT持续时间= Toast.LENGTH_SHORT;
吐司面包= Toast.makeText(背景下,网络lastknown+ X +\ N+ Y,持续时间);
toast.show();
}
}
上下文的背景下= getApplicationContext();
INT持续时间= Toast.LENGTH_SHORT;
吐司面包= Toast.makeText(背景下,没有最后知道即时拍摄,持续时间);
toast.show();
}
}}
In my Android application, I want to use both the GPS provider and the network provider for location updates for 5 seconds. First, I would like to use GPS provider (because it gives accurate results), and if the GPS provider returns latitude and longitude as 0.0 and 0.0, then I want to use network provider, but I don't want to use both the providers simultaneously. Any suggestions?
解决方案package loca.loca;
import java.util.Timer;
import java.util.TimerTask;
import android.app.Activity;
import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.widget.Toast;
public class LocationActivity extends Activity {
double x,y;
Timer timer;
LocationManager lm;
boolean gps_enabled = false;
boolean network_enabled = false;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
lm = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
gps_enabled = lm.isProviderEnabled(LocationManager.GPS_PROVIDER);
network_enabled = lm.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
if (!gps_enabled && !network_enabled) { Context context = getApplicationContext();
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(context, "nothing is enabled", duration);
toast.show();
}
if (gps_enabled)
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0,
locationListenerGps);
if (network_enabled)
lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0,
locationListenerNetwork);
timer=new Timer();
timer.schedule(new GetLastLocation(), 20000);
}
LocationListener locationListenerGps = new LocationListener() {
public void onLocationChanged(Location location) {
timer.cancel();
x =location.getLatitude();
y = location.getLongitude();
lm.removeUpdates(this);
lm.removeUpdates(locationListenerNetwork);
Context context = getApplicationContext();
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(context, "gps enabled "+x + "\n" + y, duration);
toast.show();
}
public void onProviderDisabled(String provider) {
}
public void onProviderEnabled(String provider) {
}
public void onStatusChanged(String provider, int status, Bundle extras) {
}
};
LocationListener locationListenerNetwork = new LocationListener() {
public void onLocationChanged(Location location) {
timer.cancel();
x = location.getLatitude();
y = location.getLongitude();
lm.removeUpdates(this);
lm.removeUpdates(locationListenerGps);
Context context = getApplicationContext();
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(context, "network enabled"+x + "\n" + y, duration);
toast.show();
}
public void onProviderDisabled(String provider) {
}
public void onProviderEnabled(String provider) {
}
public void onStatusChanged(String provider, int status, Bundle extras) {
}
};
class GetLastLocation extends TimerTask {
@Override
public void run() {
lm.removeUpdates(locationListenerGps);
lm.removeUpdates(locationListenerNetwork);
Location net_loc=null, gps_loc=null;
if(gps_enabled)
gps_loc=lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if(network_enabled)
net_loc=lm.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
//if there are both values use the latest one
if(gps_loc!=null && net_loc!=null){
if(gps_loc.getTime()>net_loc.getTime())
{x = gps_loc.getLatitude();
y = gps_loc.getLongitude();
Context context = getApplicationContext();
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(context, "gps lastknown "+x + "\n" + y, duration);
toast.show();
}
else
{x = net_loc.getLatitude();
y = net_loc.getLongitude();
Context context = getApplicationContext();
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(context, "network lastknown "+x + "\n" + y, duration);
toast.show();
}
}
if(gps_loc!=null){
{x = gps_loc.getLatitude();
y = gps_loc.getLongitude();
Context context = getApplicationContext();
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(context, "gps lastknown "+x + "\n" + y, duration);
toast.show();
}
}
if(net_loc!=null){
{x = net_loc.getLatitude();
y = net_loc.getLongitude();
Context context = getApplicationContext();
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(context, "network lastknown "+x + "\n" + y, duration);
toast.show();
}
}
Context context = getApplicationContext();
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(context, "no last know avilable", duration);
toast.show();
}
}}