2017-09-13 95 views
0

我想在我的android片段中使用locationListener。但我得到一個錯誤。我的代碼在下面。如何在android片段中使用locationListener

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    LocationListener locationListener = new LocationListener() { 
     @Override 
     public void onLocationChanged(Location location) { 

     } 
    }; 

    locationManager = (LocationManager) getActivity().getSystemService(Context.LOCATION_SERVICE); 
    if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) { 
     locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 2000, 1, locationListener); 
    } 
    else { 

    } 
} 

我收到此錯誤。

無法解析方法requestLocationUpdates

+0

做您已編譯'com.google。 android.gms:play-services-location:10.2.4'在你的gradle中? –

+0

是........................ – byteC0de

+0

你可以發佈你的進口聲明 –

回答

0

創建這個類:

public class GetCurrentLocation implements 
      ConnectionCallbacks, OnConnectionFailedListener, LocationListener { 

     private static final String TAG = "location-updates-sample"; 
     public static final long UPDATE_INTERVAL_IN_MILLISECONDS = 0; 
     public static final long FASTEST_UPDATE_INTERVAL_IN_MILLISECONDS = 
       UPDATE_INTERVAL_IN_MILLISECONDS/2; 
     private final String REQUESTING_LOCATION_UPDATES_KEY = "requesting-location-updates-key"; 
     private final String LOCATION_KEY = "location-key"; 
     private final String LAST_UPDATED_TIME_STRING_KEY = "last-updated-time-string-key"; 
     private GoogleApiClient mGoogleApiClient; 
     private LocationRequest mLocationRequest; 

     private Context mContext; 
     private getLocation mGetCurrentLocation; 

     public GetCurrentLocation(Context context) { 
      mContext = context; 

      buildGoogleApiClient(); 
     } 

     private synchronized void buildGoogleApiClient() { 
      Log.i(TAG, "Building GoogleApiClient"); 
      mGoogleApiClient = new GoogleApiClient.Builder(mContext) 
        .addConnectionCallbacks(this) 
        .addOnConnectionFailedListener(this) 
        .addApi(LocationServices.API) 
        .build(); 
      createLocationRequest(); 
     } 

     public interface getLocation{ 
      public void onLocationChanged(Location location); 
     } 

     public void startGettingLocation(getLocation location) { 
      mGetCurrentLocation = location; 
      connect(); 
     } 

     public void stopGettingLocation() { 
      stopLocationUpdates(); 
      disconnect(); 
     } 

     private void createLocationRequest() { 
      mLocationRequest = new LocationRequest(); 
      mLocationRequest.setInterval(UPDATE_INTERVAL_IN_MILLISECONDS); 
      mLocationRequest.setFastestInterval(FASTEST_UPDATE_INTERVAL_IN_MILLISECONDS); 
      mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY); 
     } 

     private void startLocationUpdates() { 
      if (mGoogleApiClient.isConnected()) { 
       LocationServices.FusedLocationApi.requestLocationUpdates(
         mGoogleApiClient, mLocationRequest, this); 
      } 
     } 
    private void stopLocationUpdates() { 
     LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, this); 
    } 



    private void connect() { 
     mGoogleApiClient.connect(); 
    } 

    private void disconnect() { 
     if (mGoogleApiClient.isConnected()) { 
      mGoogleApiClient.disconnect(); 
     } 
    } 

    @Override 
    public void onConnected(Bundle connectionHint) { 
     Log.i(TAG, "Connected to GoogleApiClient"); 
     startLocationUpdates(); 

    } 

    @Override 
    public void onLocationChanged(Location location) { 
     mGetCurrentLocation.onLocationChanged(location); 
    } 

    @Override 
    public void onConnectionSuspended(int cause) { 
     Log.i(TAG, "Connection suspended"); 
     mGoogleApiClient.connect(); 
    } 

    @Override 
    public void onConnectionFailed(ConnectionResult result) { 
     Log.i(TAG, "Connection failed: ConnectionResult.getErrorCode() = " + result.getErrorCode()); 
    } 
} 

在您的片段類:

private GetCurrentLocation mListen; 
mListen = new GetCurrentLocation(this); 
      mListen.startGettingLocation(new GetCurrentLocation.getLocation() { 
       @Override 
       public void onLocationChanged(Location location) { 
        // Here is my working with location object 
       } 

      }); 

確保你已經增加了最新的googleplayservice庫你的gradle文件。

0

LocationListener

@Override 
     public View onCreateView(LayoutInflater inflater, ViewGroup container, 
           Bundle savedInstanceState) { 
      TextView textView = new TextView(getActivity()); 
      textView.setText(R.string.hello_blank_fragment); 

      LocationManager locationManager = (LocationManager) getActivity().getSystemService(Context.LOCATION_SERVICE); 
      if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) { 
       if (new Utility().checkAndGrantPermission(getActivity(), Manifest.permission.ACCESS_FINE_LOCATION, 101, "your custom message")) 
        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 2000, 1, locationListener); 
      } 

      return textView; 
     } 

之外的所有方法實現從onCreate

LocationListener locationListener = new LocationListener() { 
     @Override 
     public void onLocationChanged(Location location) { 
        //Todo your code 
     } 

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

     @Override 
     public void onProviderEnabled(String provider) { 
         //Todo your code 
     } 

     @Override 
     public void onProviderDisabled(String provider) { 
         //Todo your code 
     } 
    }; 

而且checkAndGrantPermission方法是

public boolean checkAndGrantPermission(final Context context, final String permissionString, final int requestCode, String permissoinTypeMessage) 
    { 
     int currentAPIVersion = Build.VERSION.SDK_INT; 
     if(currentAPIVersion>=android.os.Build.VERSION_CODES.M) 
     { 
      if (ContextCompat.checkSelfPermission(context, permissionString) != PackageManager.PERMISSION_GRANTED) { 

       if (ActivityCompat.shouldShowRequestPermissionRationale((Activity) context, permissionString)) { 
        // your custom dialog screen when user deny permission 
       } else { 
        ActivityCompat.requestPermissions((Activity) context, new String[]{permissionString}, requestCode); 
       } 
       return false; 
      } else { 
       return true; 
      } 
     } else { 
      return true; 
     } 
    } 

在活動手柄onRequestPermissionsResult

@Override 
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { 
     super.onRequestPermissionsResult(requestCode, permissions, grantResults); 

     if (requestCode == 101) { 
      yourFragmentInstance.onRequestPermissionsResult(requestCode,permissions,grantResults); 
     } 
    } 

在你Framgent覆蓋onRequestPermissionsResult

@Override 
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { 
     super.onRequestPermissionsResult(requestCode, permissions, grantResults); 

     if (requestCode == 101) { 

      if (requestCode == AppConstants.runtimePermissionCode) { 
       if (permissions[0].equals(Manifest.permission.ACCESS_FINE_LOCATION) && grantResults[0] == PackageManager.PERMISSION_GRANTED) { 
        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 2000, 1, locationListener); 
       } 
      } 
     } 
    } 
0

收到你的問題。從

com.google.android.gms.location.LocationListener; 

你必須從

android.location.Location; 

導入LocationListener的

您已經使用位置監聽器試試這個:

locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 2000, 1, new android.location.LocationListener() { 
      @Override 
      public void onLocationChanged(Location location) { 

      } 

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

      } 

      @Override 
      public void onProviderEnabled(String provider) { 

      } 

      @Override 
      public void onProviderDisabled(String provider) { 

      } 
     }); 

希望它能幫助:)

相關問題