I am working on android studio. I am getting live GPS coordinates on a mobile app. On every mobile device, the coordinates are generated but a single mobile is not working and giving zero(0)
coordinates having android version 9
. Below is my code
@SuppressLint("MissingPermission")
public Location getLocation() {
try {
locationManager = (LocationManager) mContext
.getSystemService(LOCATION_SERVICE);
// getting GPS status
isGPSEnabled = locationManager
.isProviderEnabled(LocationManager.GPS_PROVIDER);
// getting network status
isNetworkEnabled = locationManager
.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
if (!isGPSEnabled && !isNetworkEnabled) {
// no network provider is enabled
} else {
this.canGetLocation = true;
if (isNetworkEnabled) {
locationManager.requestLocationUpdates(
LocationManager.NETWORK_PROVIDER,
MIN_TIME_BW_UPDATES,
MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
Log.d("Network", "Network");
if (locationManager != null) {
location = locationManager
.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if (location != null) {
latitude = location.getLatitude();
longitude = location.getLongitude();
}
}
}
// if GPS Enabled get lat/long using GPS Services
if (isGPSEnabled) {
if (location == null) {
locationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER,
MIN_TIME_BW_UPDATES,
MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
Log.d("GPS Enabled", "GPS Enabled");
if (locationManager != null) {
location = locationManager
.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (location != null) {
latitude = location.getLatitude();
longitude = location.getLongitude();
}
}
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
return location;
}
Gradle
android {
compileSdkVersion 33
defaultConfig {
applicationId "com.example.wrflhr.wrfnanbookings"
minSdkVersion 19
targetSdkVersion 33
versionCode 3
versionName "3.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
vectorDrawables.useSupportLibrary = true
}
buildTypes {
release {
minifyEnabled false
debuggable false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
How to get rid of this issue? I have been stuck for so many days Any help would be highly appreciated.
CodePudding user response:
android.permission.INSTALL_LOCATION_PROVIDER
permission falls under the system or signed permissions which means only system applications can request this permission.
Only OEMs are allowed to install a new location provider. A third-party developer cannot grant to his/her application the permission required to install a new location provider (android.permission.INSTALL_LOCATION_PROVIDER). Read Thread
According to the documentation
Allows an application to install a location provider into the Location Manager.
Not for use by third-party applications.
Constant Value: "android.permission.INSTALL_LOCATION_PROVIDER"
In order to request this permission you have to sign your app as a system app. See How
In order to install a new location provider either you have to root your android device or you have to develop a whole new firmware. Read Thread