2016-05-28 16 views
0

私はアンドロイドアプリでユーザーの現在の場所が変更されたときを検出しようとしています。しかし、onLocationChangedは起動されません。私はこれをフラグメントbtwに実装しようとしました。Androidの場所のリスナーが起動しない

フラグメント:

public class Fragment_Map extends Fragment { 
    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
     View rootView = inflater.inflate(R.layout.activity_fragment__live_map, container, false); 
     rootView.setOnKeyListener(new View.OnKeyListener() { 
      @Override 
      public boolean onKey(View v, int keyCode, KeyEvent event) { 
       if (keyCode == KeyEvent.KEYCODE_BACK) { 

        return true; 
       } 
       return false; 
      } 
     }); 

     // The minimum time (in miliseconds) the system will wait until checking if the location changed 
     int minTime = 1000; 
     // The minimum distance (in meters) traveled until you will be notified 
     float minDistance = 1; 
     // Create a new instance of the location listener 
     MyLocationListener myLocListener = new MyLocationListener(); 
     // Get the location manager from the system 
     LocationManager locationManager = (LocationManager) getActivity().getSystemService(Context.LOCATION_SERVICE); 
     // Get the criteria you would like to use 
     Criteria criteria = new Criteria(); 
     criteria.setPowerRequirement(Criteria.POWER_LOW); 
     criteria.setAccuracy(Criteria.ACCURACY_FINE); 
     criteria.setAltitudeRequired(false); 
     criteria.setBearingRequired(false); 
     criteria.setCostAllowed(true); 
     criteria.setSpeedRequired(false); 
     // Get the best provider from the criteria specified, and false to say it can turn the provider on if it isn't already 
     String bestProvider = locationManager.getBestProvider(criteria, false); 
     // Request location updates 
     locationManager.requestLocationUpdates(bestProvider, minTime, minDistance, myLocListener); 

     return rootView; 
} 

private class MyLocationListener implements LocationListener { 
     @Override 
     public void onLocationChanged(Location loc) { 
      if (loc != null) { 
       Log.d("CHECKLOCA", "fired"); 
       Toast.makeText(getActivity(), "fired", Toast.LENGTH_LONG).show(); 
      } 
     } 

     @Override 
     public void onProviderDisabled(String arg0) { 
      // Do something here if you would like to know when the provider is disabled by the user 
     } 

     @Override 
     public void onProviderEnabled(String arg0) { 
      // Do something here if you would like to know when the provider is enabled by the user 
     } 

     @Override 
     public void onStatusChanged(String arg0, int arg1, Bundle arg2) { 
      // Do something here if you would like to know when the provider status changes 
     } 
    } 

場所がリスナーの焼成に変更されていないのはなぜ?それは断片なのか? Hackathonの皆さんの真ん中で、この厄介なバグを解決する手助けはできますか?

答えて

0

たぶんお使いのデバイスは、uは場所を見つけるために設定された基準を提供することはできません。.. は(onCreateViewにこのコードを使用することができます):

// flag for GPS status 
boolean isGPSEnabled = false; 
// flag for network status 
boolean isNetworkEnabled = false; 
// flag for GPS status 
boolean canGetLocation = false; 

try { 
     locationManager = (LocationManager) getActivity() 
       .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 
     } else { 
      this.canGetLocation = true; 
      if (isNetworkEnabled) { 
       //updates will be send according to these arguments 
       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(); 
    } 
+0

変更するには、このキーワードで.. – rafsanahmad007

+0

をMyLocationListenerすることは非常に良いコードと実際にはないこと問題は[this blog post](http://gabesechansoftware.com/location-tracking/)で議論されています。しかし、あなたは基準について正しいかもしれません。 –

+0

ok ..私は感謝を理解する.. – rafsanahmad007

1

あなたはPRIORITY_HIGH_ACCURACYに位置し、設定された優先度を要求するためにGoogleApiClientを使用することができます。あなたのFragmentGoogelApiClientためcallbacksを実装することができます - そして、このようなGoogleAPIClientをインスタンス化し、接続します

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

次に、あなたのクライアントは、位置サービスAPIに接続されると、あなたはその後、更新を要求することができます

public void onConnected(Bundle bundle){ 
    ... 
    LocationRequest locationRequest = LocationRequest.create(); 
    locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY); 
    locationRequest.setInterval(180000); // update interval - eg (3) minutes 
       LocationServices.FusedLocationApi.requestLocationUpdates(googleApiClient, locationRequest, myLocListener); 

    ... 

} 

こちらがお役に立てば幸いです。

1
  1. このようなインターフェイスを作成します。この2.Put

    public interface OnLocationReceived 
    { 
    public void onLocationReceived(LatLng latlong); 
    public void onLocationReceived(Location location); 
    public void onConntected(Bundle bundle); 
    public void onConntected(Location location); 
    } 
    

    MyLocationListener

    @Override 
    public void onLocationChanged(Location location) 
    { 
        if (!isLocationReceived) { 
        mLocationReceived.onConntected(location); 
        isLocationReceived = true; 
        } 
        if (mLocationReceived != null) { 
        mLocationReceived.onLocationReceived(location); 
        } 
        latLong = getLatLng(location); 
        if (mLocationReceived != null && latLong != null) { 
        mLocationReceived.onLocationReceived(latLong); 
        } 
    } 
    
関連する問題