0
画面に入るとGPSがオンになっているかどうかをチェックし、そうでなければGPSを有効にするためのダイアログを表示します。ユーザーが[はい]をクリックすると、onActivityResult - > GPSがオンになり、場所を取得しようとしましたが、これは常にnullを返します。現在の場所はアンドロイドでヌルになる
GPSを使用して画面に入ると、私は数日間このことに苦しんでいて、リソースを見つけることができないようです。
私は何を間違えたのですか? uはLOGを取得していない場合
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
@Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.recyler_list, container, false);
initializeView(view);
registerGoogleClient()
return view;
}
private void registerGoogleClient() {
try {
// Create an instance of GoogleAPIClient.
if (mGoogleApiClient == null) {
mGoogleApiClient = new GoogleApiClient.Builder(getActivity())
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.build();
LocationRequest locationRequest = LocationRequest.create();
LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder();
builder.addLocationRequest(locationRequest);
builder.setAlwaysShow(true);
locationSettingsRequest = builder.build();
connectGoogleApiClient();
}
} catch (Throwable throwable) {
throwable.printStackTrace();
}
}
@Override
public void onStart() {
super.onStart();
try {
connectGoogleApiClient();
} catch (Throwable throwable) {
throwable.printStackTrace();
}
}
private void connectGoogleApiClient() {
try {
if (mGoogleApiClient != null) {
mGoogleApiClient.connect();
checkDeviceGps();
}
} catch (Throwable throwable) {
throwable.printStackTrace();
}
}
@Override
public void onStop() {
super.onStop();
disConnectGoogleApiClient();
}
private void disConnectGoogleApiClient() {
try {
if (mGoogleApiClient != null && mGoogleApiClient.isConnected()) {
mGoogleApiClient.disconnect();
}
} catch (Throwable throwable) {
throwable.printStackTrace();
}
}
@Override
public void onConnected(Bundle connectionHint) {
try {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
System.out.println("if block");
if (getActivity().checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED
&& getActivity().checkSelfPermission(Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
// mLastLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
}
mLastLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
} else {
mLastLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
}
System.out.println("mLastLocation is====>" + mLastLocation);
if (mLastLocation != null) {
latitude = mLastLocation.getLatitude();
longtude = mLastLocation.getLongitude();
System.out.println("Latitude====>" + mLastLocation.getLatitude());
System.out.println("Longitude====>" + mLastLocation.getLongitude());
}
} catch (Throwable throwable) {
throwable.printStackTrace();
}
}
private void checkDeviceGps() {
try {
PendingResult<LocationSettingsResult> pendingResult =
LocationServices.SettingsApi.checkLocationSettings(mGoogleApiClient, locationSettingsRequest);
pendingResult.setResultCallback(this);
} catch (Throwable throwable) {
throwable.printStackTrace();
}
}
@Override
public void onResult(@NonNull LocationSettingsResult locationSettingsResult) {
final Status status = locationSettingsResult.getStatus();
switch (status.getStatusCode()) {
case LocationSettingsStatusCodes.SUCCESS:
break;
case LocationSettingsStatusCodes.RESOLUTION_REQUIRED:
try {
status.startResolutionForResult(getActivity(), 100);
} catch (IntentSender.SendIntentException e) {
e.printStackTrace();
}
break;
}
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case 100:
getLatLong();
break;
}
}
private void getLatLong() {
try {
if (mLastLocation != null) {
latitude = mLastLocation.getLatitude();
longtude = mLastLocation.getLongitude();
System.out.println("Latitude====>" + mLastLocation.getLatitude());
System.out.println("Longitude====>" + mLastLocation.getLongitude());
}
} catch (Throwable throwable) {
throwable.printStackTrace();
}
}
あなたはGPSを許可した後にデータを探すために20秒〜30秒前に数秒待つようにしました – Ak9637
どうすればこのことができますか? – AbhiRam
場所を取得するには数秒待つ必要があります。これにスレッドを使用することができます。 –