Я надеюсь, что вы в порядке. Я создал приложение market и в макете реестра есть кнопка для определения текущего местоположения покупателя и продавца
RegisterSellerActivity. java
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.constraintlayout.solver.GoalRow;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import android.Manifest;
import android.content.Context;
import android.content.pm.PackageManager;
import android.location.Address;
import android.location.Geocoder;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.Toast;
import java.util.List;
import java.util.Locale;
public class RegisterSellerActivity extends AppCompatActivity implements LocationListener {
// UI Views
private Button registerBtn;
private ImageButton backBtn, gpsBtn;
private ImageView profileIv;
private EditText nameEt, shopNameEt, phoneEt, deliveryFeeEt, mailEt,
countryEt, stateEt, cityEt, addressEt, passwordEt, cPasswordEt;
//permission constants
private static final int LOCATION_REQUEST_CODE = 100;
//permission arrays
private String[] locationPermissions;
private double latitude, longitude;
private LocationManager locationManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register_seller);
// init UI Views
nameEt = findViewById(R.id.nameEt);
shopNameEt = findViewById(R.id.shopNameEt);
phoneEt = findViewById(R.id.phoneEt);
deliveryFeeEt = findViewById(R.id.deliveryFeeEt);
countryEt = findViewById(R.id.countryEt);
stateEt = findViewById(R.id.stateEt);
cityEt = findViewById(R.id.cityEt);
addressEt = findViewById(R.id.addressEt);
mailEt = findViewById(R.id.emailEt);
passwordEt = findViewById(R.id.passwordEt);
cPasswordEt = findViewById(R.id.cPasswordEt);
profileIv = findViewById(R.id.profileIv);
backBtn = findViewById(R.id.backBtn);
gpsBtn = findViewById(R.id.gpsBtn);
registerBtn = findViewById(R.id.registerBtn);
//init permissions array
locationPermissions = new String[]{Manifest.permission.ACCESS_FINE_LOCATION};
backBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
onBackPressed();
}
});
gpsBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// detect the current location
if (checkLocationPermission()) {
// already allowed
detectLocation();
} else {
// not allowed , request
requestLocationPermission();
}
}
});
}
private void detectLocation() {
Toast.makeText(this, "Please wait ... ", Toast.LENGTH_LONG).show();
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
}
private boolean checkLocationPermission(){
boolean result = ContextCompat.checkSelfPermission(this,Manifest.permission.ACCESS_FINE_LOCATION) ==
(PackageManager.PERMISSION_GRANTED);
return result;
}
private void requestLocationPermission(){
ActivityCompat.requestPermissions(this,locationPermissions,LOCATION_REQUEST_CODE);
}
@Override
public void onLocationChanged(Location location) {
// location detected
latitude = location.getLatitude();
longitude = location.getLongitude();
findAddress(); //1
}
private void findAddress() { //2
// find address , country, state, city
Geocoder geocoder;
List<Address> addresses;
geocoder = new Geocoder(this, Locale.getDefault());
try {
addresses = geocoder.getFromLocation(latitude,longitude,1);
String address = addresses.get(0).getAddressLine(0); // complete address
String city = addresses.get(0).getLocality();
String state = addresses.get(0).getAdminArea();
String country = addresses.get(0).getCountryName();
// set addresses EditText
countryEt.setText(country);
stateEt.setText(state);
cityEt.setText(city);
addressEt.setText(address);
}
catch (Exception e){
Toast.makeText(this, ""+e.getMessage(), Toast.LENGTH_SHORT).show();
}
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onProviderDisabled(String provider) {
// gps location disabled //6
Toast.makeText(this, "Please turn on GPS location ... ", Toast.LENGTH_SHORT).show();
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
switch (requestCode)
{
case LOCATION_REQUEST_CODE:
{
if(grantResults.length>0)
{
boolean locationAccepted = grantResults[0] == PackageManager.PERMISSION_GRANTED;
if(locationAccepted)
{
//permission allowed
detectLocation();
}
else
{
// permission denied
Toast.makeText(this, "Location permission is necessary ... !", Toast.LENGTH_SHORT).show();
}
}
}
break;
}
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
}
}
и манифеста
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
android:installLocation="auto">
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher_marcato"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_marcato"
android:supportsRtl="true"
android:theme="@style/AppTheme.NoActionBar">
<activity android:name=".ForgotPasswordActivity"></activity>
<activity android:name=".RegisterUserActivity" />
<activity android:name=".RegisterSellerActivity" /> <!-- new -->
<activity android:name=".SpalshActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".LoginActivity" />
<activity android:name="com.theartofdev.edmodo.cropper.CropImageActivity"
android:theme="@style/Base.Theme.AppCompat"/>
</application>
</manifest>
как я уже говорил, он работает в эмуляторе, но телефоны (Galaxy J3, Galaxy A10, Grand Prime) не работают, так в чем проблема