2012-06-15 28 views
0

有沒有簡單的方法,如何確定,如果GPS信號已被獲取?我正在計算所需位置和當前位置之間的距離,如果沒有GPS定位我想顯示錯誤消息。問題是 - 如何簡單檢查我有沒有位置?Android - 如何確定GPS是固定的

感謝

回答

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(); 
    } 

} 

玩得開心!