Android如何查看gps是否连接到卫星?

标签 android gps

我得到了这个 gps 方法:

// GPS
    private void addGPSListener() {

        globalconstant.db.setVersion(1);
        globalconstant.db.setLocale(Locale.getDefault());
        globalconstant.db.setLockingEnabled(true);

        final String gps =

        "CREATE TABLE IF NOT EXISTS GPS_Values ("

                + "id INTEGER PRIMARY KEY AUTOINCREMENT, Latitude float(10, 8), Longitude float(10, 8),Accuracy INTEGER,Speed INTEGER,City TEXT,timestamp TIMESTAMP);";
        globalconstant.db.execSQL(gps);

        Log.d("FESTIVALE :: ", "Frissítési idő: "
                + globalconstant.gps_update_value);
        float f = Float.valueOf(globalconstant.gps_update_value.trim())
                .floatValue();
        float update = f * 1000;

        globalconstant.mlocManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        globalconstant.mlocListener = new MyLocationListener();
        globalconstant.mlocManager.requestLocationUpdates(
                LocationManager.GPS_PROVIDER, (long) update, 5f,
                globalconstant.mlocListener);

        // if(Global.getInstance().currentGPSLocation != null){
        //
        // }


    }

    public class MyLocationListener implements LocationListener {

        public void onLocationChanged(Location loc) {

            float szel = (float) loc.getLatitude();
            float hossz = (float) loc.getLongitude();
            int horiAcc = (int) (loc.getAccuracy());
            // int speed=(int) ((loc.getSpeed()*3600)/1000); //sebesség km/h-ban
            int speed = 0;

            if (loc.hasSpeed()) {
                speed = (int) ((loc.getSpeed() * 3600) / 1000); // sebesség
                                                                // km/h-ban
            } else {
                speed = 0;
            }

            String test = String.format("%.08f", szel);
            String test2 = String.format("%.08f", hossz);

            Geocoder geocoder = new Geocoder(main.this, Locale.getDefault());
            try {
                List<Address> addresses = geocoder.getFromLocation(szel, hossz,
                        1);
                city = addresses.get(0).getLocality();
            } catch (IOException e) {
                e.printStackTrace();
            }

            ContentValues gps_values = new ContentValues();

            gps_values.put("Latitude", test);
            gps_values.put("Longitude", test2);
            gps_values.put("Accuracy", horiAcc);
            gps_values.put("Speed", speed);
            gps_values.put("City", city);

            SimpleDateFormat dateFormat = new SimpleDateFormat(
                    "yyyy-MM-dd HH:mm:ss");
            Date date = new Date(System.currentTimeMillis());

            gps_values.put("timestamp", dateFormat.format(date));

            try {
                globalconstant.db.beginTransaction();
                globalconstant.db.insert("GPS_Values", null, gps_values);
                globalconstant.db.setTransactionSuccessful();
            } finally {
                globalconstant.db.endTransaction();
            }

            Log.d("FESTIVALE :: ",
                    "Hely " + test + ", " + test2 + " , " + horiAcc + " , "
                            + speed + " , " + city + ","
                            + dateFormat.format(date));
            // String Text = "My current location is: " + "Latitude = "
            // + loc.getLatitude() + "\nLongitude = " + loc.getLongitude();

            // Toast.makeText(getApplicationContext(), "Hely" +test + "\n" +
            // test2 + "\n" + horiAcc + "\n" +speed + "\n" +city,
            // Toast.LENGTH_SHORT)
            // .show();

        }

        public void onProviderDisabled(String provider) {
            Toast.makeText(getApplicationContext(), "Gps Disabled",
                    Toast.LENGTH_SHORT).show();
            DialogInterface.OnClickListener dialogClickListener = new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    switch (which) {
                    case DialogInterface.BUTTON_POSITIVE:
                        // show gps otions
                        Intent gpsOptionsIntent = new Intent(
                                android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS);
                        startActivity(gpsOptionsIntent);
                        break;

                    case DialogInterface.BUTTON_NEGATIVE:
                        dialog.cancel();
                        break;
                    }
                }
            };

            AlertDialog.Builder builder = new AlertDialog.Builder(main.this);
            builder.setMessage("A GPS nincs aktiválva!\nAktiválja most?")
                    .setPositiveButton("Aktivál", dialogClickListener)
                    .setNegativeButton("Nem", dialogClickListener).show();
        }

        public void onProviderEnabled(String provider) {
            Toast.makeText(getApplicationContext(), "Gps Enabled",
                    Toast.LENGTH_SHORT).show();

        }

        public void onStatusChanged(String provider, int status, Bundle extras) {
            /* This is called when the GPS status alters */
            switch (status) {
            case LocationProvider.OUT_OF_SERVICE:
                Log.v(tag, "Status Changed: Out of Service");
                Toast.makeText(main.this, "Status Changed: Out of Service",
                        Toast.LENGTH_SHORT).show();
                break;
            case LocationProvider.TEMPORARILY_UNAVAILABLE:
                Log.v(tag, "Status Changed: Temporarily Unavailable");
                Toast.makeText(main.this,
                        "Status Changed: Temporarily Unavailable",
                        Toast.LENGTH_SHORT).show();
                break;
            case LocationProvider.AVAILABLE:
                Log.v(tag, "Status Changed: Available");
                Toast.makeText(main.this, "Status Changed: Available",
                        Toast.LENGTH_SHORT).show();
                break;
            }

        }
        public void onGpsStatusChanged(int event) {
            if (event == GpsStatus.GPS_EVENT_FIRST_FIX) {
                // showMessageDialog("GPS fixed");
                Toast.makeText(getApplicationContext(), "GPS fixed",
                        Toast.LENGTH_SHORT).show();
            }
        }
    }// gps vége

我想制作一个简单的加载程序对话框,直到它连接到卫星。 我该如何管理?在调用此方法之前,我如何检查它是否先连接到卫星? 感谢您的回答!

最佳答案

您需要为此实现GpsStatus.Listener

例如,

public class MyActivity extends Activity implements GpsStatus.Listener
{
         ...
         ...
         ...
         // add listener into locationManager
        locationManager.addGpsStatusListener(this);

       @Override
       public void onGpsStatusChanged(int) 
       {

          switch (event) 
         {
            case GpsStatus.GPS_EVENT_SATELLITE_STATUS:
            break;
            case GpsStatus.GPS_EVENT_FIRST_FIX:   // this means you  found GPS Co-ordinates                          
            break;
            case GpsStatus.GPS_EVENT_STARTED:
            break;
            case GpsStatus.GPS_EVENT_STOPPED:
            break;
          }
       }
}

关于Android如何查看gps是否连接到卫星?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12567492/

相关文章:

java - 索引 1 处的绑定(bind)值为空

android - margin 不影响 "include"

java - 模拟器: emulator: ERROR: emulation currently requires hardware acceleration

iphone - 基于位置的本地通知

Android,在 GPS 上工作,但我不知道真实世界的坐标是如何工作的

android - 如何在 Android 中重置 GPS

android - 使用 Google Play App Signing 时上传 key 丢失怎么办?

android - 我怎样才能找出用户的谷歌播放国家?

gps - 用于 GPS 跟踪的微 Controller 无线通信

安卓 nGPS : get location based on magnetic field instead of gps or cell triangulation