Android - 如何确定GPS是固定的

时间:2012-06-15 08:46:58

标签: android gps location

有没有简单的方法,如何判断GPS信号是否被采集?我计算所需位置和我当前位置之间的距离,如果没有GPS修复,我想显示错误信息。问题是 - 如何简单检查我有位置?

由于

1 个答案:

答案 0 :(得分:1)

像这样听取修复:

    MyLocationOverlay myLocationOverlay = new MyLocationOverlay(this, mapView);
    myLocationOverlay.enableMyLocation();
    myLocationOverlay.runOnFirstFix(new Runnable() {
        public void run() {
        }
    });

这是检查gps是否已启用的方法。如果不是,则要求用户启用它。

private void checkGPS() {
    try {
        isGPSEnabled = locationManager
                .isProviderEnabled(LocationManager.GPS_PROVIDER);
    } catch (Exception e) {
        Log.d("GPS", "GPS koll misslyckades");
        e.printStackTrace();
    }

    if (!isGPSEnabled) {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle(getString(R.string.alert_start_gps_title))
                .setMessage(getString(R.string.alert_start_gps_message))
                .setCancelable(false)
                .setPositiveButton("Aktivera",
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog,
                                    int id) {

                                // Sent user to GPS settings screen
                                final ComponentName toLaunch = new ComponentName(
                                        "com.android.settings",
                                        "com.android.settings.SecuritySettings");
                                final Intent intent = new Intent(
                                        Settings.ACTION_LOCATION_SOURCE_SETTINGS);
                                intent.addCategory(Intent.CATEGORY_LAUNCHER);
                                intent.setComponent(toLaunch);
                                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                                startActivityForResult(intent, 1);

                                dialog.dismiss();
                                return;

                            }
                        })
                .setNegativeButton("Avbryt",
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog,
                                    int id) {
                                dialog.cancel();
                                finish();
                                return;
                            }
                        });
        AlertDialog gpsAlert = builder.create();
        gpsAlert.show();
    }

}

玩得开心!