0
我目前正在做一個項目,應用程序將獲取用戶的當前位置並將其顯示在textview中的片段內。我沒有得到任何錯誤,但由於某種原因,它不工作..Android使用LocationListener獲取用戶當前位置片段內
這是我的代碼的一部分。 在清單和進口的所有權限都很好..
另一個奇怪的事情是我的應用程序不會顯示我的logcat中的任何東西,當我點擊按鈕。
注:所有代碼片段裏面
希望有人可以給我一個提示或兩個的感謝!
import android.content.pm.PackageManager;
import android.location.Location;
import android.location.LocationManager;
import android.location.LocationListener;
private LocationManager locationManager;
private LocationListener listener;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View myinflate = inflater.inflate(R.layout.fragment_home, container, false);
final TextView cords = (TextView) myinflate.findViewById(R.id.welcome);
final Button butt = (Button) myinflate.findViewById(R.id.button1);
locationManager = (LocationManager) getActivity().getSystemService(getActivity().LOCATION_SERVICE);
listener = new LocationListener() {
@Override
public void onLocationChanged(Location location) {
cords.setText(location.getLatitude() +" "+location.getLongitude());
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onProviderDisabled(String provider) {
}
};
butt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
return;
}else {
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, listener);
}
}
});
return myinflate;
}
謝謝回覆! – Duckbenok
兩個變量LocationSurface和AppConstants無法解析? – Duckbenok
LocationSurface&AppConstants是自定義的變量在我的應用程序 公共靜態最終字符串[] PERMISSIONS_LOCATION = { Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION }; public static final int REQUEST_CODE_PERMISSIONS = 2;請參考: https://developer.android.com/training/permissions/requesting.html – ahmedmsvb