2013-12-13 4 views
-1

지난 주부터 셀 타워의 도움으로 사용자 위치를 얻으려고 고민하고 있으므로 location-client 즉, getlastLocation();을 기반으로합니다. 하지만 내 문제는 응용 프로그램을 다시 열 때 (심지어 백그라운드에서 닫힙니다) 나는 위치를 얻을 수 없습니다. 이제 센서와 셀 타워를 사용하여 사용자 위치를 얻으 려 할 때마다 사용자 위치를 얻습니다 ... (심지어 저는 이러한 방식으로 융합 된 위치 제공 업체 API)Android - 센서 (모션)를 사용하여 locatioClient를 기반으로 셀 타워로 위치 업데이트

을 검색 내가 사전에 코드를

private void createLocReq() { 
     if (mLocationRequest == null) { 
      // Create a new global location parameters object 
      mLocationRequest = LocationRequest.create(); 
      // Set the update interval 
      mLocationRequest.setInterval(LocationServices.UPDATE_INTERVAL_IN_MILLISECONDS); 
      // Use high accuracy 
      mLocationRequest.setPriority(LocationRequest.PRIORITY_LOW_POWER); 
      // Set the interval ceiling to one minute 
      mLocationRequest.setFastestInterval(LocationServices.FAST_INTERVAL_CEILING_IN_MILLISECONDS); 
     } 
    } 

감사를 적용한, .............. 어느 날

을 도울 수

답변

0

위치를 알아내는 코드 :

public class GPSTracker extends Service implements LocationListener { 

    private final Context mContext; 

    // flag for GPS status 
    boolean isGPSEnabled = false; 

    // flag for network status 
    boolean isNetworkEnabled = false; 

    // flag for GPS status 
    boolean canGetLocation = false; 

    Location location; // location 
    double latitude; // latitude 
    double longitude; // longitude 

    // The minimum distance to change Updates in meters 
    private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 10; // 10 meters 

    // The minimum time between updates in milliseconds 
    private static final long MIN_TIME_BW_UPDATES = 1000 * 60 * 1; // 1 minute 

    // Declaring a Location Manager 
    protected LocationManager locationManager; 

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

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

      // getting GPS status 
      isGPSEnabled = locationManager 
        .isProviderEnabled(LocationManager.GPS_PROVIDER); 

      // getting network status 
      isNetworkEnabled = locationManager 
        .isProviderEnabled(LocationManager.NETWORK_PROVIDER); 

      if (!isGPSEnabled && !isNetworkEnabled) { 
       // no network provider is enabled 
       turnGPSOn(); 
      } else { 
       this.canGetLocation = true; 
       if (isNetworkEnabled) { 
        locationManager.requestLocationUpdates(
          LocationManager.NETWORK_PROVIDER, 
          MIN_TIME_BW_UPDATES, 
          MIN_DISTANCE_CHANGE_FOR_UPDATES, this); 
        Log.d("Network", "Network"); 
        if (locationManager != null) { 
         location = locationManager 
           .getLastKnownLocation(LocationManager.NETWORK_PROVIDER); 
         if (location != null) { 
          latitude = location.getLatitude(); 
          longitude = location.getLongitude(); 
         } 
        } 
       } 
       // if GPS Enabled get lat/long using GPS Services 
       if (isGPSEnabled) { 
        if (location == null) { 
         locationManager.requestLocationUpdates(
           LocationManager.GPS_PROVIDER, 
           MIN_TIME_BW_UPDATES, 
           MIN_DISTANCE_CHANGE_FOR_UPDATES, this); 
         Log.d("GPS Enabled", "GPS Enabled"); 
         if (locationManager != null) { 
          location = locationManager 
            .getLastKnownLocation(LocationManager.GPS_PROVIDER); 
          if (location != null) { 
           latitude = location.getLatitude(); 
           longitude = location.getLongitude(); 
          } 
         } 
        } 
       } 
      } 

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

     return location; 
    } 

    /** 
    * Stop using GPS listener 
    * Calling this function will stop using GPS in your app 
    * */ 
    public void stopUsingGPS(){ 
     if(locationManager != null){ 
      locationManager.removeUpdates(GPSTracker.this); 
     }  
    } 

    /** 
    * Function to get latitude 
    * */ 
    public double getLatitude(){ 
     if(location != null){ 
      latitude = location.getLatitude(); 
     } 

     // return latitude 
     return latitude; 
    } 

    /** 
    * Function to get longitude 
    * */ 
    public double getLongitude(){ 
     if(location != null){ 
      longitude = location.getLongitude(); 
     } 

     // return longitude 
     return longitude; 
    } 

    /** 
    * Function to check GPS/wifi enabled 
    * @return boolean 
    * */ 
    public boolean canGetLocation() { 
     return this.canGetLocation; 
    } 

    /** 
    * Function to show settings alert dialog 
    * On pressing Settings button will lauch Settings Options 
    * */ 
    public void showSettingsAlert(){ 
     AlertDialog.Builder alertDialog = new AlertDialog.Builder(mContext); 

     // Setting Dialog Title 
     alertDialog.setTitle("GPS is settings"); 

     // Setting Dialog Message 
     alertDialog.setMessage("GPS is not enabled. Do you want to go to settings menu?"); 

     // On pressing Settings button 
     alertDialog.setPositiveButton("Settings", new DialogInterface.OnClickListener() { 
      @Override 
      public void onClick(DialogInterface dialog,int which) { 
       Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS); 
       mContext.startActivity(intent); 
      } 
     }); 

     // on pressing cancel button 
     alertDialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { 
      @Override 
      public void onClick(DialogInterface dialog, int which) { 
      dialog.cancel(); 
      } 
     }); 

     // Showing Alert Message 
     alertDialog.show(); 
    } 

    @Override 
    public void onLocationChanged(Location location) { 
    } 

    @Override 
    public void onProviderDisabled(String provider) { 
    } 

    @Override 
    public void onProviderEnabled(String provider) { 
    } 

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

    @Override 
    public IBinder onBind(Intent arg0) { 
     return null; 
    } 
    public void turnGPSOn() 
    { 
     Intent intent = new Intent("android.location.GPS_ENABLED_CHANGE"); 
     intent.putExtra("enabled", true); 
     this.mContext.sendBroadcast(intent); 

     String provider = Settings.Secure.getString(mContext.getContentResolver(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED); 
     if(!provider.contains("gps")){ //if gps is disabled 
      final Intent poke = new Intent(); 
      poke.setClassName("com.android.settings", "com.android.settings.widget.SettingsAppWidgetProvider"); 
      poke.addCategory(Intent.CATEGORY_ALTERNATIVE); 
      poke.setData(Uri.parse("3")); 
      this.mContext.sendBroadcast(poke); 


     } 
    } 
    // automatic turn off the gps 
    public void turnGPSOff() 
    { 
     String provider = Settings.Secure.getString(mContext.getContentResolver(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED); 
     if(provider.contains("gps")){ //if gps is enabled 
      final Intent poke = new Intent(); 
      poke.setClassName("com.android.settings", "com.android.settings.widget.SettingsAppWidgetProvider"); 
      poke.addCategory(Intent.CATEGORY_ALTERNATIVE); 
      poke.setData(Uri.parse("3")); 
      this.mContext.sendBroadcast(poke); 
     } 
    } 
} 
+0

위치 관리자와 함께 위치 클라이언트를 사용하고 싶습니다 ... –

+0

왜? 위치 관리자를 사용하지 않고 장치에서 다른 응용 프로그램을 사용하지 않고 위치 수신을 시작하면 업데이트 된 위치를 얻지 못할 것입니다. 따라서 LocationClient 만 사용하고 다른 사람이 업데이트를 수행하기를 기대하지 않습니까? – jboi