私はアンドロイドアプリでユーザーの現在の場所が変更されたときを検出しようとしています。しかし、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の皆さんの真ん中で、この厄介なバグを解決する手助けはできますか?
変更するには、このキーワードで.. – rafsanahmad007
をMyLocationListenerすることは非常に良いコードと実際にはないこと問題は[this blog post](http://gabesechansoftware.com/location-tracking/)で議論されています。しかし、あなたは基準について正しいかもしれません。 –
ok ..私は感謝を理解する.. – rafsanahmad007