0

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 ?

Zoe
  • 27,060
  • 21
  • 118
  • 148
ChPr
  • 7
  • 1

1 Answers1

-1

It is indeed the method involving the services which makes it possible to keep the GPS active even if the application is no longer in the foreground.

I finally found out why it wasn't working. It was linked to the fact that I only had one parameter to give in the "NotificationCompat.Builder".

While looking for the doc on notifications, I saw that it was necessary to have this dependency in the "build.gradle" file:

dependencies {
    implementation "com.android.support:support-compat:28.0.0"
}

I did not have it. I replaced the one I had with the one indicated and this time I was able to set the two parameters I wanted and ... I had no more errors.

This afternoon, I went for a hike with my application which I interrupted several times with "PlantNet", "birdNET", this last application also using GPS: no problem of operation and recording of tracks with my application.

desertnaut
  • 57,590
  • 26
  • 140
  • 166
ChPr
  • 7
  • 1