Stack Overflow на русском Asked by KingKoval on February 4, 2021
При первом запуске все работает хорошо. Если разрешаешь показывает координаты, если нет то приложения закрывается. Но если разрешил и потом повторно зашел в приложения то не показывает координаты
import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import android.Manifest;
import android.annotation.SuppressLint;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Build;
import android.os.Bundle;
import android.provider.Settings;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity implements LocationListener {
LocationManager locationManager;
private static final int REQUEST_CODE = 1;
private double latitude, longtitude;
TextView textView;
@SuppressLint("MissingPermission")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = findViewById(R.id.textView);
checkPermission();
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
if(grantResults.length > 0 && grantResults[0] != PackageManager.PERMISSION_GRANTED){
finish();
} else {
getLocation();
}
}
@SuppressLint("MissingPermission")
private void getLocation(){
locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, this);
Location location = locationManager.getLastKnownLocation(locationManager.NETWORK_PROVIDER);
onLocationChanged(location);
textView.setText(String.valueOf(location.getLatitude()) + "n" + String.valueOf(location.getLongitude()));
}
private void checkPermission(){
int FineLocation = ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION);
int CoarseLocation = ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION);
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M){
if(FineLocation != PackageManager.PERMISSION_GRANTED && CoarseLocation != PackageManager.PERMISSION_GRANTED){
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_COARSE_LOCATION,
Manifest.permission.ACCESS_FINE_LOCATION}, REQUEST_CODE);
} else{
}
} else{
}
}
@Override
public void onLocationChanged(@NonNull Location location) {
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
@Override
public void onProviderEnabled(@NonNull String provider) {
}
@Override
public void onProviderDisabled(@NonNull String provider) {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("GPS disable!");
builder.setMessage("Do you want enable GPS?");
builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//start settings window when user can enable GPS
startActivity(new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS));
}
});
builder.setNegativeButton("No", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//close AlertDialog
dialog.cancel();
}
});
AlertDialog enableGps = builder.create();
enableGps.show();
}
}```
У вас в коде ничего не происходит, если разрешения уже выданы. Вызовите getLocation();
из обоих else
в методе checkPermission
Answered by ЮрийСПб on February 4, 2021
Get help from others!
Recent Questions
Recent Answers
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP