GpsStatus.Listener works only if GPS is on(GpsStatus.Listener 仅在 GPS 开启时有效)
问题描述
在我的应用程序中,我有一个 GpsStatus.Listener当用户启用或禁用 GPS 时接收事件.如果 在我启动应用程序之前打开 GPS,一切正常.在这种情况下,我会收到 GPS_EVENT_STARTED 或 GPS_EVENT_STOPPED 每次我打开或关闭 GPS 时.
in my App I have a GpsStatus.Listener to receive events when the user enables or disables GPS. Everything works fine if GPS is on before I start the app. In this case I receive a GPS_EVENT_STARTED or a GPS_EVENT_STOPPED everytime I toggle GPS on or off.
问题是如果应用程序启动时 GPS 关闭.在这种情况下,如果我打开或关闭 GPS,我不会收到任何事件.
The problem is if GPS is off while app is starting. In this case I don't receive an event if I toggle GPS on or off.
谁能给我解释一下?
这是我的代码:
public class GPSTracker implements android.location.GpsStatus.Listener {
private final Context context;
private final LocationListener locListener;
private LocationManager locationManager;
private boolean isGPSEnabled = false;
public GPSTracker(Context context, LocationListener locListener) {
this.context = context;
this.locListener = locListener;
setupGPS();
}
private void setupGPS() {
locationManager = (LocationManager) getContext().getSystemService(Context.LOCATION_SERVICE);
locationManager.addGpsStatusListener(this);
isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
if(!isGPSEnabled) {
Toast.makeText(getContext(), "GPS disabled", Toast.LENGTH_SHORT).show();
} else {
locationManager.removeUpdates(locListener);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, MIN_TIME, MIN_DISTANCE, locListener);
}
}
@Override
public void onGpsStatusChanged(int event) {
Log.e("onGpsStatusChanged", event+"");
switch(event) {
case GpsStatus.GPS_EVENT_STARTED:
Log.e("onGpsStatusChanged", "GPS_EVENT_STARTED");
break;
case GpsStatus.GPS_EVENT_STOPPED:
Log.e("onGpsStatusChanged", "GPS_EVENT_STOPPED");
break;
}
}
所以,我的 logcat 输出是(如果 GPS 在启动时打开):
So, my logcat output is (if GPS is on while starting):
- GpsStatusChanged(24638): 1//应用启动
- GpsStatusChanged(24638):GPS_EVENT_STARTED
- GpsStatusChanged(24638): 4//搜索修复
- GpsStatusChanged(24638):4
- GpsStatusChanged(24638): 2//关闭 GPS
- GpsStatusChanged(24638):GPS_EVENT_STOPED
关闭 GPS 时的输出:无.
Output with GPS off: nothing.
推荐答案
嗯,LocationListener
已经提供了:
onProviderDisabled()
,onProviderEnabled()
和 onStatusChanged()
正是为了这个目的.
onProviderDisabled()
,onProviderEnabled()
and onStatusChanged()
for exactly this purpose.
GpsStatus.Listener
提供有关 GPS 服务内部运作的信息.它不能用于告诉 GPS Provider 的状态.
GpsStatus.Listener
delivers info about GPS service's inner workings. It is not to be used for telling the status pf GPS Provider.
LocationListener
提供有关提供者的信息.当您向位置提供者注册 LocationListener
时,会相应地调用 onProviderEnabled()
/onProviderDisabled()
,并且您的应用程序总能判断 GPS 何时启动打开或关闭.
LocationListener
delivers info about providers. The moment you register LocationListener
with location provider, onProviderEnabled()
/onProviderDisabled()
is called accordingly, and your app can always tell when GPS is turned on or off.
试试这个:
public class test extends Activity implements LocationListener{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER,1000,10,this);
}
@Override
public void onLocationChanged(Location location) {
}
@Override
public void onStatusChanged(String s, int i, Bundle bundle) {
}
@Override
public void onProviderEnabled(String s) {
if(LocationManager.GPS_PROVIDER.equals(s)){
Toast.makeText(this,"GPS on",Toast.LENGTH_SHORT).show();
}
}
@Override
public void onProviderDisabled(String s) {
if(LocationManager.GPS_PROVIDER.equals(s)){
Toast.makeText(this,"GPS off",Toast.LENGTH_SHORT).show();
}
}
}
这篇关于GpsStatus.Listener 仅在 GPS 开启时有效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:GpsStatus.Listener 仅在 GPS 开启时有效
基础教程推荐
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01
- 如何在 UIImageView 中异步加载图像? 2022-01-01
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01
- Android:对话框关闭而不调用关闭 2022-01-01
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01
- android 应用程序已发布,但在 google play 中找不到 2022-01-01
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01