2017-12-30 10 views
-3

이 줄 밑줄이 나타납니다로 : 코드 조각 아래LocationServices.FusedLocationApi ... 오류

LocationServices.FusedLocationApi.requestLocationUpdates(googleApiClient, locationRequest, this); 

package com.websmithing.gpstracker; 

    import android.app.Service; 
    import android.content.Context; 
    import android.content.Intent; 
    import android.content.SharedPreferences; 
    import android.location.Location; 
    import android.os.Bundle; 
    import android.os.IBinder; 
    import android.util.Log; 

    import com.google.android.gms.common.ConnectionResult; 
    import com.google.android.gms.common.GooglePlayServicesUtil; 
    import com.google.android.gms.common.api.GoogleApiClient; 
    import com.google.android.gms.location.LocationListener; 
    import com.google.android.gms.location.LocationRequest; 
    import com.google.android.gms.location.LocationServices; 
    import com.loopj.android.http.AsyncHttpResponseHandler; 
    import com.loopj.android.http.RequestParams; 

    import java.io.UnsupportedEncodingException; 
    import java.net.URLEncoder; 
    import java.text.DateFormat; 
    import java.text.SimpleDateFormat; 
    import java.util.Date; 
    import java.util.TimeZone; 

    //import com.google.android.gms.common.GooglePlayServicesUtil; 

    public class LocationService extends Service implements 
      GoogleApiClient.ConnectionCallbacks, 
      GoogleApiClient.OnConnectionFailedListener, 
      LocationListener { 

     private static final String TAG = "LocationService"; 

     // use the websmithing defaultUploadWebsite for testing and then check your 
     // location with your browser here: https://www.websmithing.com/gpstracker/displaymap.php 
     private String defaultUploadWebsite; 

     private boolean currentlyProcessingLocation = false; 
     private LocationRequest locationRequest; 
     private GoogleApiClient googleApiClient; 

     @Override 
     public void onCreate() { 
      super.onCreate(); 

      defaultUploadWebsite = getString(R.string.default_upload_website); 
     } 

     @Override 
     public int onStartCommand(Intent intent, int flags, int startId) { 
      // if we are currently trying to get a location and the alarm manager has called this again, 
      // no need to start processing a new location. 
      if (!currentlyProcessingLocation) { 
       currentlyProcessingLocation = true; 
       startTracking(); 
      } 

      return START_NOT_STICKY; 
     } 

     private void startTracking() { 
      Log.d(TAG, "startTracking"); 

      if (GooglePlayServicesUtil.isGooglePlayServicesAvailable(this) == ConnectionResult.SUCCESS) { 

       googleApiClient = new GoogleApiClient.Builder(this) 
         .addApi(LocationServices.API) 
         .addConnectionCallbacks(this) 
         .addOnConnectionFailedListener(this) 
         .build(); 

       if (!googleApiClient.isConnected() || !googleApiClient.isConnecting()) { 
        googleApiClient.connect(); 
       } 
      } else { 
       Log.e(TAG, "unable to connect to google play services."); 
      } 
     } 

     protected void sendLocationDataToWebsite(Location location) { 
      // formatted for mysql datetime format 
      DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 
      dateFormat.setTimeZone(TimeZone.getDefault()); 
      Date date = new Date(location.getTime()); 

      SharedPreferences sharedPreferences = this.getSharedPreferences("com.websmithing.gpstracker.prefs", Context.MODE_PRIVATE); 
      SharedPreferences.Editor editor = sharedPreferences.edit(); 

      float totalDistanceInMeters = sharedPreferences.getFloat("totalDistanceInMeters", 0f); 

      boolean firstTimeGettingPosition = sharedPreferences.getBoolean("firstTimeGettingPosition", true); 

      if (firstTimeGettingPosition) { 
       editor.putBoolean("firstTimeGettingPosition", false); 
      } else { 
       Location previousLocation = new Location(""); 
       previousLocation.setLatitude(sharedPreferences.getFloat("previousLatitude", 0f)); 
       previousLocation.setLongitude(sharedPreferences.getFloat("previousLongitude", 0f)); 

       float distance = location.distanceTo(previousLocation); 
       totalDistanceInMeters += distance; 
       editor.putFloat("totalDistanceInMeters", totalDistanceInMeters); 
      } 

      editor.putFloat("previousLatitude", (float)location.getLatitude()); 
      editor.putFloat("previousLongitude", (float)location.getLongitude()); 
      editor.apply(); 

      final RequestParams requestParams = new RequestParams(); 
      requestParams.put("latitude", Double.toString(location.getLatitude())); 
      requestParams.put("longitude", Double.toString(location.getLongitude())); 

      Double speedInMilesPerHour = location.getSpeed()* 2.2369; 
      requestParams.put("speed", Integer.toString(speedInMilesPerHour.intValue())); 

      try { 
       requestParams.put("date", URLEncoder.encode(dateFormat.format(date), "UTF-8")); 
      } catch (UnsupportedEncodingException e) {} 

      requestParams.put("locationmethod", location.getProvider()); 

      if (totalDistanceInMeters > 0) { 
       requestParams.put("distance", String.format("%.1f", totalDistanceInMeters/1609)); // in miles, 
      } else { 
       requestParams.put("distance", "0.0"); // in miles 
      } 

      requestParams.put("username", sharedPreferences.getString("userName", "")); 
      requestParams.put("phonenumber", sharedPreferences.getString("appID", "")); // uuid 
      requestParams.put("sessionid", sharedPreferences.getString("sessionID", "")); // uuid 

      Double accuracyInFeet = location.getAccuracy()* 3.28; 
      requestParams.put("accuracy", Integer.toString(accuracyInFeet.intValue())); 

      Double altitudeInFeet = location.getAltitude() * 3.28; 
      requestParams.put("extrainfo", Integer.toString(altitudeInFeet.intValue())); 

      requestParams.put("eventtype", "android"); 

      Float direction = location.getBearing(); 
      requestParams.put("direction", Integer.toString(direction.intValue())); 

      final String uploadWebsite = sharedPreferences.getString("defaultUploadWebsite", defaultUploadWebsite); 

      LoopjHttpClient.get(uploadWebsite, requestParams, new AsyncHttpResponseHandler() { 
       @Override 
       public void onSuccess(int statusCode, cz.msebera.android.httpclient.Header[] headers, byte[] responseBody) { 
        LoopjHttpClient.debugLoopJ(TAG, "sendLocationDataToWebsite - success", uploadWebsite, requestParams, responseBody, headers, statusCode, null); 
        stopSelf(); 
       } 
       @Override 
       public void onFailure(int statusCode, cz.msebera.android.httpclient.Header[] headers, byte[] errorResponse, Throwable e) { 
        LoopjHttpClient.debugLoopJ(TAG, "sendLocationDataToWebsite - failure", uploadWebsite, requestParams, errorResponse, headers, statusCode, e); 
        stopSelf(); 
       } 
      }); 
     } 

     @Override 
     public void onDestroy() { 
      super.onDestroy(); 
     } 

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

     @Override 
     public void onLocationChanged(Location location) { 
      if (location != null) { 
       Log.e(TAG, "position: " + location.getLatitude() + ", " + location.getLongitude() + " accuracy: " + location.getAccuracy()); 

       // we have our desired accuracy of 500 meters so lets quit this service, 
       // onDestroy will be called and stop our location uodates 
       if (location.getAccuracy() < 500.0f) { 
        stopLocationUpdates(); 
        sendLocationDataToWebsite(location); 
       } 
      } 
     } 

     private void stopLocationUpdates() { 
      if (googleApiClient != null && googleApiClient.isConnected()) { 
       googleApiClient.disconnect(); 
      } 
     } 

     /** 
     * Called by Location Services when the request to connect the 
     * client finishes successfully. At this point, you can 
     * request the current location or start periodic updates 
     */ 
     @Override 
     public void onConnected(Bundle bundle) { 
      Log.d(TAG, "onConnected"); 

      locationRequest = LocationRequest.create(); 
      locationRequest.setInterval(1000); // milliseconds 
      locationRequest.setFastestInterval(1000); // the fastest rate in milliseconds at which your app can handle location updates 
      locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY); 

// this line of error 
    LocationServices.FusedLocationApi.requestLocationUpdates(googleApiClient, locationRequest, this); 

     } 

     @Override 
     public void onConnectionFailed(ConnectionResult connectionResult) { 
      Log.e(TAG, "onConnectionFailed"); 

      stopLocationUpdates(); 
      stopSelf(); 
     } 

     @Override 
     public void onConnectionSuspended(int i) { 
      Log.e(TAG, "GoogleApiClient connection has been suspend"); 
     } 
    } 

enter image description here

+0

'LocationServices.FusedLocationApi ... error' 여기 오류 로그를 추가하십시오있는 오류 – Prem

+0

... –

+0

이 줄 LocationServices.FusedLocationApi.requestLocationUpdates (googleApiClient, locationRequest 밑줄이 나타납니다, 이); –

답변

0

눌러 Alt-Enter 및 빠른 수정을 찾아 Lint에게이 문제에 대해 불평을하지 말 것을 알리기 위해 @SuppressLint 주석을 추가했습니다.

if (ContextCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { 

    ActivityCompat.requestPermissions(this, new String[] { android.Manifest.permission.ACCESS_COARSE_LOCATION }, 
               LocationService.MY_PERMISSION_ACCESS_COURSE_LOCATION); 
} 

확인할 수 this answer for details

+0

입니다. https://stackoverflow.com/questions/33327984/call-requires-permissions를 확인하십시오. - 사용자별로 거부 될 수 있습니다. –

+0

@Andres Arango가 문제를 해결하지 못했습니까? –

+0

죄송합니다, 저는 이것에 초보자입니다. 나는 저에게 말해주는 라인을 넣었습니다. 저는 activitymain에 넣었지만, 저에게 이것을 보여 줍니 다. https://i.stack.imgur.com/0cd2Z.jpg –