
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;
}
}
}
home键在KeyEvent中的键值为3
ublic static final int KEYCODE_HOME = 3;
当用户按下home键的时候(包括长按),程序会进入到PhoneWindowManagerjava类中的public boolean interceptKeyBeforeDispatching(WindowState win, int action, int flags,int keyCode, int scanCode, int metaState, int repeatCount, int policyFlags)这个方法中进行处理。如果用户是连续点击home,此时就要执行长按home事件了。
即执行mHandlerpostDelayed(mHomeLongPress,ViewConfigurationgetGlobalActionKeyTimeout());对应的代码。也就会跳转到mHomeLongPress这个Runnable接着往下执行。
interceptKeyBeforeDispatching这个方法位于PhoneWindowManagerjava中。
位置为:\frameworks\base\policy\src\com\android\internal\policy\impl\PhoneWindowManagerjava
public boolean interceptKeyBeforeDispatching(WindowState win, int action, int flags,int keyCode, int scanCode, int metaState, int repeatCount, int policyFlags) {
final boolean down = (action == KeyEventACTION_DOWN);
//4、用户按下home,然后马上释放。此时这个条件成立。将之前postDelayed的事件remove掉。此时就不会执行长按home事件。
if ((keyCode == KeyEventKEYCODE_HOME) && !down) {
mHandlerremoveCallbacks(mHomeLongPress);
}
//5、第一次按下home,mHomePressed为false。
if (mHomePressed) {
if (keyCode == KeyEventKEYCODE_HOME) {
//a、如果用户连续按下home,此时暂时没有up事件。所以就不走这里。
//b、如果用户没有连续按下home,此时过来的是up(move或者>
以上就是关于Android怎么实现点击按钮获取当前位置的信息,没有地图的情况下。全部的内容,包括:Android怎么实现点击按钮获取当前位置的信息,没有地图的情况下。、android HOME长按之后的键值是多少、等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)