In the GPS application that I am developing, I want my GPS to continue giving me its information even if the application is no longer in the foreground.
But I feel like I no longer receive its information when my application is in the background.
My smartphone is a Samsung A41 with android 10.
Still, I think I did the right thing. Here is what I did:
In the "build.gradle (Module GPSNav.app) file):
apply plugin: 'com.android.application'
android {
compileSdkVersion 29
buildToolsVersion "29.0.3"
defaultConfig {
applicationId "com.example.gpsnav"
minSdkVersion 23
targetSdkVersion 29
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
//noinspection GradleCompatible
implementation 'com.android.support:support-v4:24.2.1'
}
In the "androidManifest.xml" file I have:
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION" />"
In my application I have:
private static final int PERMISSION_REQUEST_GPS = 100;
...
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_BACKGROUND_LOCATION) != PackageManager.PERMISSION_GRANTED
&& ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED
&& ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(Main.this, new String[]{
Manifest.permission.ACCESS_BACKGROUND_LOCATION,
Manifest.permission.ACCESS_COARSE_LOCATION,
Manifest.permission.ACCESS_FINE_LOCATION}, PERMISSION_REQUEST_GPS);
return;
}
...
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
if (requestCode == PERMISSION_REQUEST_GPS) {
if (grantResults[0] == PackageManager.PERMISSION_DENIED) { // Ferme l'application
Toast.makeText(Main.this, "Désolé !!! vous ne pouvez pas utiliser cette application sans permission", Toast.LENGTH_LONG).show();
finish();
}
}
}
Apparently that is not enough.
What should be done ?