Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/image-processing/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/meteor/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 在后台使用GPS_Android_Android Gps - Fatal编程技术网

Android 在后台使用GPS

Android 在后台使用GPS,android,android-gps,Android,Android Gps,在我正在开发的GPS应用程序中,我希望我的GPS继续向我提供信息,即使该应用程序不再在前台 但我觉得当我的应用程序在后台时,我不再收到它的信息 我的智能手机是三星A41和安卓10 不过,我认为我做了正确的事情。以下是我所做的: 在“build.gradle(Module GPSNav.app)文件中: 在“androidManifest.xml”文件中,我有: <uses-permission android:name="android.permission.ACCESS

在我正在开发的GPS应用程序中,我希望我的GPS继续向我提供信息,即使该应用程序不再在前台

但我觉得当我的应用程序在后台时,我不再收到它的信息

我的智能手机是三星A41和安卓10

不过,我认为我做了正确的事情。以下是我所做的:

在“build.gradle(Module GPSNav.app)文件中:

在“androidManifest.xml”文件中,我有:

    <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" />"
显然,这还不够


应该做什么?

事实上,正是这种涉及服务的方法使得GPS保持活动状态成为可能,即使应用程序不再在前台

我终于找到了它不起作用的原因,这与我在“NotificationCompat.Builder”中只给出一个参数有关

在查找通知上的文档时,我发现有必要在“build.gradle”文件中包含此依赖项:

我没有。我用指示的参数替换了我的参数,这次我可以设置我想要的两个参数,并且…我没有更多的错误


今天下午,我带着我的应用程序去远足,我用“PlantNet”、“birdNET”中断了几次“,这是最后一个同样使用GPS的应用程序:我的应用程序在操作和录制曲目方面没有问题。

您考虑过使用前台服务吗?在现代Android版本中,您需要使用上述建议的前台服务,并且用户还需要在设备的电池节省设置中将您的应用程序列入白名单。
    <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" />"
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();
        }
    }
}
dependencies {
    implementation "com.android.support:support-compat:28.0.0"
}