1、首先,检查那个供应商是有效的。有些在设备上可能没效,有些可能在应用程序清单中无效。
2、如果有有效的供应商,启动位置监听和路由失效定时器。
3、如果从位置监听器获得更新,用供应的值,停止监听器和定时器。
4、如果没有获得更新,并且时间已经结束,那就用最后一个已知值。
5、从有效供应商获取最后一个已知值并且选择最接近的。
class MyLocation{
/
If GPS is enabled
Use minimal connected satellites count
/
private static final int min_gps_sat_count = 5;
/
Iteration step time
/
private static final int iteration_timeout_step = 500;
LocationResult locationResult;
private Location bestLocation = null;
private Handler handler = new Handler();
private LocationManager myLocationManager;
public Context context;
private boolean gps_enabled = false;
private int counts = 0;
private int sat_count = 0;
private Runnable showTime = new Runnable() {
public void run() {
boolean stop = false;
counts++;
Systemprintln("counts=" + counts);
//if timeout (1 min) exceeded, stop tying
if(counts > 120){
stop = true;
}
//update last best location
bestLocation = getLocation(context);
//if location is not ready or don`t exists, try again
if(bestLocation == null && gps_enabled){
Systemprintln("BestLocation not ready, continue to wait");
handlerpostDelayed(this, iteration_timeout_step);
}else{
//if best location is known, calculate if we need to continue to look for better location
//if gps is enabled and min satellites count has not been connected or min check count is smaller then 4 (2 sec)
if(stop == false && !needToStop()){
Systemprintln("Connected " + sat_count + " sattelites continue waiting");
handlerpostDelayed(this, iteration_timeout_step);
}else{
Systemprintln("#########################################");
Systemprintln("BestLocation finded return result to main sat_count=" + sat_count);
Systemprintln("#########################################");
// removing all updates and listeners
myLocationManagerremoveUpdates(gpsLocationListener);
myLocationManagerremoveUpdates(networkLocationListener);
myLocationManagerremoveGpsStatusListener(gpsStatusListener);
sat_count = 0;
// send best location to locationResult
locationResultgotLocation(bestLocation);
}
}
}
};
/
Determine if continue to try to find best location
/
private Boolean needToStop(){
if(!gps_enabled){
return true;
}
else if(counts <= 4){
return false;
}
if(sat_count < min_gps_sat_count){
//if 20-25 sec and 3 satellites found then stop
if(counts >= 40 && sat_count >= 3){
return true;
}
return false;
}
}
return true;
}
/
Best location abstract result class
/
public static abstract class LocationResult{
public abstract void gotLocation(Location location);
}
/
Initialize starting values and starting best location listeners
@param Context ctx
@param LocationResult result
/
public void init(Context ctx, LocationResult result){
context = ctx;
locationResult = result;
myLocationManager = (LocationManager) contextgetSystemService(ContextLOCATION_SERVICE);
gps_enabled = (Boolean) myLocationManagerisProviderEnabled(LocationManagerGPS_PROVIDER);
bestLocation = null;
counts = 0;
// turning on location updates
myLocationManagerrequestLocationUpdates("network", 0, 0, networkLocationListener);
myLocationManagerrequestLocationUpdates("gps", 0, 0, gpsLocationListener);
myLocationManageraddGpsStatusListener(gpsStatusListener);
// starting best location finder loop
handlerpostDelayed(showTime, iteration_timeout_step);
}
/
GpsStatus listener OnChainged counts connected satellites count
/
public final GpsStatusListener gpsStatusListener = new GpsStatusListener() {
public void onGpsStatusChanged(int event) {
if(event == GpsStatusGPS_EVENT_SATELLITE_STATUS){
try {
// Check number of satellites in list to determine fix state
GpsStatus status = myLocationManagergetGpsStatus(null);
Iterable<GpsSatellite>satellites = statusgetSatellites();
sat_count = 0;
Iterator<GpsSatellite>satI = satellitesiterator();
while(satIhasNext()) {
GpsSatellite satellite = satInext();
Systemprintln("Satellite: snr=" + satellitegetSnr() + ", elevation=" + satellitegetElevation());
sat_count++;
}
} catch (Exception e) {
eprintStackTrace();
sat_count = min_gps_sat_count + 1;
}
Systemprintln("#### sat_count = " + sat_count);
}
}
};
/
Gps location listener
/
public final LocationListener gpsLocationListener = new LocationListener(){
@Override
public void onLocationChanged(Location location){
}
public void onProviderDisabled(String provider){}
public void onProviderEnabled(String provider){}
public void onStatusChanged(String provider, int status, Bundle extras){}
};
/
Network location listener
/
public final LocationListener networkLocationListener = new LocationListener(){
@Override
public void onLocationChanged(Location location){
}
public void onProviderDisabled(String provider){}
public void onProviderEnabled(String provider){}
public void onStatusChanged(String provider, int status, Bundle extras){}
};
/
Returns best location using LocationManagergetBestProvider()
@param context
@return Location|null
/
public static Location getLocation(Context context){
Systemprintln("getLocation()");
// fetch last known location and update it
try {
LocationManager lm = (LocationManager) contextgetSystemService(ContextLOCATION_SERVICE);
Criteria criteria = new Criteria();
criteriasetAccuracy(CriteriaACCURACY_FINE);
criteriasetAltitudeRequired(false);
criteriasetBearingRequired(false);
criteriasetCostAllowed(true);
String strLocationProvider = lmgetBestProvider(criteria, true);
Systemprintln("strLocationProvider=" + strLocationProvider);
Location location = lmgetLastKnownLocation(strLocationProvider);
if(location != null){
return location;
}
return null;
} catch (Exception e) {
eprintStackTrace();
return null;
}
}
}
不知道你是要知道“怎么模拟按钮的点击事件”还是要知道“这个东西是怎么调上来的”1、模拟点击事件,可以用调用Button的onClick的方法。2、这个点击事件是触到屏幕后,触摸屏驱动首先得到这个消息,然后根据WindowManager里面记录的信息分发给ViewRoot,然后就一直发,就像生活委员发信件一样,最终发给那个被点击的View,没人处理就报废。
安卓手机区分屏幕触控,打开多点触控,就可以了。解答如下:
1、进入鲁大师应用界面。点击软件右下方工具箱。工具箱页面下滑可以看到有一个屏幕检测功能项,直接输入号MA的如何查找定位他人位置随时找到他,
2、进入屏幕检测功能,手机屏幕有四个检测项目,多点触控检测排在第三位,击进入,开始检测-点击屏幕开始,
3、在检测窗口,使用五个手指进行屏幕触控,检测结果出来了
以上就是关于Android怎么实现点击按钮获取当前位置的信息,没有地图的情况下。全部的内容,包括:Android怎么实现点击按钮获取当前位置的信息,没有地图的情况下。、android怎么模拟用户在webview中对应的坐标的点击事件、安卓在全局下识别到屏幕触控点等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)