Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/370.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/3/android/197.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
androidjava中的foregroundService_Java_Android - Fatal编程技术网

androidjava中的foregroundService

androidjava中的foregroundService,java,android,Java,Android,这是源代码。正如我们所看到的,startForegroundService在25 API级别以上工作。但是,我的问题是,Facebook、Instagram和WhatsApp如何在每个API级别上使用它?如果您使用的是androidx(在我看来您应该是),那么您可以在所有API版本上使用它 如果没有,可以在调用之前检查API版本: if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { startForegroundSer

这是源代码。正如我们所看到的,startForegroundService在25 API级别以上工作。但是,我的问题是,Facebook、Instagram和WhatsApp如何在每个API级别上使用它?

如果您使用的是androidx(在我看来您应该是),那么您可以在所有API版本上使用它

如果没有,可以在调用之前检查API版本:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        startForegroundService(new Intent(this,service.class));
    }
请注意,这是从API 5开始提供的,这意味着您的服务可以是前台服务,而无需在这些API版本上调用
startForegroundService

如果您想知道为什么API 26中会有这样的变化,请引用以下内容:

在Android 8.0之前,创建前台服务的常用方法是创建后台服务,然后将该服务升级到前台。安卓8.0有一个复杂的问题;系统不允许后台应用程序创建后台服务。因此,安卓8.0引入了新方法,在前台启动新服务。系统创建服务后,应用程序有五秒钟的时间调用服务的方法以显示新服务的用户可见通知。如果应用程序未在时间限制内调用,系统将停止服务并声明该应用程序为

如果您使用的是androidx(您应该是哪个IMO),那么您可以使用适用于所有API版本的androidx

如果没有,可以在调用之前检查API版本:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        startForegroundService(new Intent(this,service.class));
    }
请注意,这是从API 5开始提供的,这意味着您的服务可以是前台服务,而无需在这些API版本上调用
startForegroundService

如果您想知道为什么API 26中会有这样的变化,请引用以下内容:

在Android 8.0之前,创建前台服务的常用方法是创建后台服务,然后将该服务升级到前台。安卓8.0有一个复杂的问题;系统不允许后台应用程序创建后台服务。因此,安卓8.0引入了新方法,在前台启动新服务。系统创建服务后,应用程序有五秒钟的时间调用服务的方法以显示新服务的用户可见通知。如果应用程序未在时间限制内调用,系统将停止服务并声明该应用程序为