Android: check if GPS is searching, has fix or is not in use(Android:检查 GPS 是否正在搜索、已修复或未使用)
问题描述
I want to know if there is a way in Android (GingerBread) to know if at the moment the GPS is doing something or not. Let me be a bit more clear: I basically want to call some method or api that will tell me wheter the GPS is:
1)Fixed (GPS icon in statusbar on)
2)Searching for fix (GPS icon on statusbar blinking)
3)Inactive (No app is using location services at the moment, no icon on statusbar)
Now: I know that you can use a LocationListener to be notified of such changes BUT this is not good for me because I don't want my code to remain running waiting waiting for events, my code runs periodically at scheduled times, does something and then terminates, so I need a way to check the status of the GPS service in that precise moment, rather than wait for notifications of when it changes.
After doing lot of testing on GPS, finally I found the solution. When android app calls location manager and GPS starts searching, one event is triggered and also when gps is locked another event is triggered. Following code shows how to do this.
locationManager = (LocationManager)mContext.getSystemService(LOCATION_SERVICE);
isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
if (isGPSEnabled) {
if (locationManager != null) {
// Register GPSStatus listener for events
locationManager.addGpsStatusListener(mGPSStatusListener);
gpslocationListener = new LocationListener() {
public void onLocationChanged(Location loc) {}
public void onStatusChanged(String provider, int status, Bundle extras) {}
public void onProviderEnabled(String provider) {}
public void onProviderDisabled(String provider) {}
};
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,
MIN_TIME_BW_UPDATES_GPS, MIN_DISTANCE_CHANGE_FOR_UPDATES_GPS,
gpslocationListener);
}
}
/* * This is GPSListener function invoked when various events occurs like * GPS started, GPS stopped, GPS locked */
public Listener mGPSStatusListener = new GpsStatus.Listener() {
public void onGpsStatusChanged(int event) {
switch(event) {
case GpsStatus.GPS_EVENT_STARTED:
Toast.makeText(mContext, "GPS_SEARCHING", Toast.LENGTH_SHORT).show();
System.out.println("TAG - GPS searching: ");
break;
case GpsStatus.GPS_EVENT_STOPPED:
System.out.println("TAG - GPS Stopped");
break;
case GpsStatus.GPS_EVENT_FIRST_FIX:
/*
* GPS_EVENT_FIRST_FIX Event is called when GPS is locked
*/
Toast.makeText(mContext, "GPS_LOCKED", Toast.LENGTH_SHORT).show();
Location gpslocation = locationManager
.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if(gpslocation != null) {
System.out.println("GPS Info:"+gpslocation.getLatitude()+":"+gpslocation.getLongitude());
/*
* Removing the GPS status listener once GPS is locked
*/
locationManager.removeGpsStatusListener(mGPSStatusListener);
}
break;
case GpsStatus.GPS_EVENT_SATELLITE_STATUS:
// System.out.println("TAG - GPS_EVENT_SATELLITE_STATUS");
break;
}
}
};
It is better to put GPS code in and as service to get GPS location information.
Each time if you call GPS function, GPSStatus listener is registered. GPS_SEARCHING toast comes only once when GPS is started to search and GPS_LOCKED toast displays when GPS is locked. If we call GPS function again, GPS_EVENT_FIRST_FIX event is triggered if it is locked(displays GPS_LOCKED toast) and if GPS is already started to search it won't display GPS_SEARCHING toast(i.e GPS_STARTED event won't trigger). After GPS_EVENT_FIRST_FIX event is triggered i'm removing the GPSstatus listener updates.
When GPS_EVENT_FIRST_FIX event is triggered, its better to call gpslastknownlocation() function to get fresh latest GPS fix.(Its better to look into Android developers site for more info).
I hope this will help others....
这篇关于Android:检查 GPS 是否正在搜索、已修复或未使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Android:检查 GPS 是否正在搜索、已修复或未使用
基础教程推荐
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01
- android 应用程序已发布,但在 google play 中找不到 2022-01-01
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01
- 如何在 UIImageView 中异步加载图像? 2022-01-01
- Android:对话框关闭而不调用关闭 2022-01-01