2017-07-03 73 views
0

我在android應用上工作,我希望用戶在他/她打開特定活動時獲取用戶的位置或用戶的緯度和經度 而不使用谷歌地圖的活動,甚至打開它,我用這個代碼,但沒有結果獲取用戶的當前位置並在firebase數據庫中存儲

LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE); 
    if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { 
     return; 
    } 
    if (lm.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) { 
     lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, new android.location.LocationListener() { 
      @Override 
      public void onLocationChanged(Location location) { 
       latitude = location.getLatitude(); 
       longitude = location.getLongitude(); 
       LatLng latLng = new LatLng(latitude, longitude); 
       Log.i("loc", latitude+"::"+longitude); 

      } 

      @Override 
      public void onStatusChanged(String provider, int status, Bundle extras) { 

      } 

      @Override 
      public void onProviderEnabled(String provider) { 

      } 

      @Override 
      public void onProviderDisabled(String provider) { 

      } 
     }); 
    } else if (lm.isProviderEnabled(lm.GPS_PROVIDER)) { 
     lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, new android.location.LocationListener() { 
      @Override 
      public void onLocationChanged(Location location) { 
       latitude = location.getLatitude(); 
       longitude = location.getLongitude(); 
       LatLng latLng = new LatLng(latitude, longitude); 
       Log.i("loc", latitude+"::"+longitude); 

      } 

      @Override 
      public void onStatusChanged(String provider, int status, Bundle extras) { 

      } 

      @Override 
      public void onProviderEnabled(String provider) { 

      } 

      @Override 
      public void onProviderDisabled(String provider) { 

      } 
     }); 
    } 

我把這個代碼在我的onCreate方法,但價值也提前

回答

0

的位置跟蹤器0.0

感謝服務



    public class LocationTrack extends Service implements LocationListener { 

     private final Context mContext; 


     boolean checkGPS = false; 


     boolean checkNetwork = false; 

     boolean canGetLocation = false; 

     Location loc; 
     double latitude; 
     double longitude; 


     private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 10; 


     private static final long MIN_TIME_BW_UPDATES = 1000 * 60 * 1; 
     protected LocationManager locationManager; 

     public LocationTrack(Context mContext) { 
      this.mContext = mContext; 
      getLocation(); 
     } 

     private Location getLocation() { 

      try { 
       locationManager = (LocationManager) mContext 
         .getSystemService(LOCATION_SERVICE); 

       // get GPS status 
       checkGPS = locationManager 
         .isProviderEnabled(LocationManager.GPS_PROVIDER); 

       // get network provider status 
       checkNetwork = locationManager 
         .isProviderEnabled(LocationManager.NETWORK_PROVIDER); 

       if (!checkGPS && !checkNetwork) { 
        Toast.makeText(mContext, "No Service Provider is available", Toast.LENGTH_SHORT).show(); 
       } else { 
        this.canGetLocation = true; 

        // if GPS Enabled get lat/long using GPS Services 
        if (checkGPS) { 

         if (ActivityCompat.checkSelfPermission(mContext, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(mContext, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { 
          // TODO: Consider calling 
          // ActivityCompat#requestPermissions 
          // here to request the missing permissions, and then overriding 
          // public void onRequestPermissionsResult(int requestCode, String[] permissions, 
          //           int[] grantResults) 
          // to handle the case where the user grants the permission. See the documentation 
          // for ActivityCompat#requestPermissions for more details. 
         } 
         locationManager.requestLocationUpdates(
           LocationManager.GPS_PROVIDER, 
           MIN_TIME_BW_UPDATES, 
           MIN_DISTANCE_CHANGE_FOR_UPDATES, this); 
         if (locationManager != null) { 
          loc = locationManager 
            .getLastKnownLocation(LocationManager.GPS_PROVIDER); 
          if (loc != null) { 
           latitude = loc.getLatitude(); 
           longitude = loc.getLongitude(); 
          } 
         } 


        } 


        /*if (checkNetwork) { 


         if (ActivityCompat.checkSelfPermission(mContext, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(mContext, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { 
          // TODO: Consider calling 
          // ActivityCompat#requestPermissions 
          // here to request the missing permissions, and then overriding 
          // public void onRequestPermissionsResult(int requestCode, String[] permissions, 
          //           int[] grantResults) 
          // to handle the case where the user grants the permission. See the documentation 
          // for ActivityCompat#requestPermissions for more details. 
         } 
         locationManager.requestLocationUpdates(
           LocationManager.NETWORK_PROVIDER, 
           MIN_TIME_BW_UPDATES, 
           MIN_DISTANCE_CHANGE_FOR_UPDATES, this); 

         if (locationManager != null) { 
          loc = locationManager 
            .getLastKnownLocation(LocationManager.NETWORK_PROVIDER); 

         } 

         if (loc != null) { 
          latitude = loc.getLatitude(); 
          longitude = loc.getLongitude(); 
         } 
        }*/ 

       } 


      } catch (Exception e) { 
       e.printStackTrace(); 
      } 

      return loc; 
     } 

     public double getLongitude() { 
      if (loc != null) { 
       longitude = loc.getLongitude(); 
      } 
      return longitude; 
     } 

     public double getLatitude() { 
      if (loc != null) { 
       latitude = loc.getLatitude(); 
      } 
      return latitude; 
     } 

     public boolean canGetLocation() { 
      return this.canGetLocation; 
     } 

     public void showSettingsAlert() { 
      AlertDialog.Builder alertDialog = new AlertDialog.Builder(mContext); 


      alertDialog.setTitle("GPS is not Enabled!"); 

      alertDialog.setMessage("Do you want to turn on GPS?"); 


      alertDialog.setPositiveButton("Yes", new DialogInterface.OnClickListener() { 
       public void onClick(DialogInterface dialog, int which) { 
        Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS); 
        mContext.startActivity(intent); 
       } 
      }); 


      alertDialog.setNegativeButton("No", new DialogInterface.OnClickListener() { 
       public void onClick(DialogInterface dialog, int which) { 
        dialog.cancel(); 
       } 
      }); 


      alertDialog.show(); 
     } 


     public void stopListener() { 
      if (locationManager != null) { 

       if (ActivityCompat.checkSelfPermission(mContext, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(mContext, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { 
        // TODO: Consider calling 
        // ActivityCompat#requestPermissions 
        // here to request the missing permissions, and then overriding 
        // public void onRequestPermissionsResult(int requestCode, String[] permissions, 
        //           int[] grantResults) 
        // to handle the case where the user grants the permission. See the documentation 
        // for ActivityCompat#requestPermissions for more details. 
        return; 
       } 
       locationManager.removeUpdates(LocationTrack.this); 
      } 
     } 

     @Override 
     public IBinder onBind(Intent intent) { 
      return null; 
     } 

     @Override 
     public void onLocationChanged(Location location) { 

     } 

     @Override 
     public void onStatusChanged(String s, int i, Bundle bundle) { 

     } 

     @Override 
     public void onProviderEnabled(String s) { 

     } 

     @Override 
     public void onProviderDisabled(String s) { 

     } 
    } 

使用位置跟蹤服務活動像下面



    locationTrack = new LocationTrack(MainActivity.this); 


        if (locationTrack.canGetLocation()) { 


         double longitude = locationTrack.getLongitude(); 
         double latitude = locationTrack.getLatitude(); 

         Toast.makeText(getApplicationContext(), "Longitude:" + Double.toString(longitude) + "\nLatitude:" + Double.toString(latitude), Toast.LENGTH_SHORT).show(); 
        } else { 

         locationTrack.showSettingsAlert(); 
        } 

 

請參閱本link能夠解釋這一點。

相關問題