我正试图找到如何检查GPS是否还有信号,我该怎么办?我正在使用:
locationManager.addGpsStatusListener(new MyGPSListener());
private class MyGPSListener implements GpsStatus.Listener
{
public void onGpsStatusChanged(int event)
{
switch (event) {
case GpsStatus.GPS_EVENT_SATELLITE_STATUS:
if (mLastLocation != null) {
isGPSFix = (SystemClock.elapsedRealtime() - mLastLocationMillis) < 3000;
}
if (isGPSFix) {
mHandler.obtainMessage(GPS_ENABLED).sendToTarget();
} else {
mHandler.obtainMessage(GPS_DISABLED).sendToTarget();
}
break;
case GpsStatus.GPS_EVENT_FIRST_FIX:
isGPSFix = true;
break;
}
}
}
但这不是解决方案,GPS信号在这里没有丢失。这只是“当设备没有移动3秒= GPS丢失”,但我希望GPS的事件很糟糕,因为我在我的“汽车应用程序”中使用这个...所以我需要知道我是否开车例如,进入隧道。
我正在谷歌搜索一整天,我没有任何解决方案。它是如何工作的?为什么不起作用?当android和其他应用程序知道它时,我也想知道它!
感谢您的回答。
编辑:
我试图将它与最新速度相结合,但它没用......速度必须大于20公里/小时......如果我以1公里/小时的速度进入隧道怎么办?这毫无意义......
答案 0 :(得分:0)
xml:
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
onCreate:
NetState receiver = new NetState();
IntentFilter filter = new IntentFilter();
filter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
this.registerReceiver(receiver, filter);
receiver.onReceive(this, null);
add class:
class NetState extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent arg1) {
ConnectivityManager manager = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo gprs = manager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
NetworkInfo wifi = manager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
if(!gprs.isConnected() && !wifi.isConnected())
{
AlertDialog.Builder ab = new AlertDialog.Builder(context);
ab.setMessage("please check network");
ab.setPositiveButton("yes", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
dialog.dismiss();
}
}).show();
}
else{
AlertDialog.Builder ab = new AlertDialog.Builder(context);
ab.setMessage("Network connect succeed!");
ab.setPositiveButton("yes", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
dialog.dismiss();
}
}).show();
}
}
}
我希望它对你有帮助〜