Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/20.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
Java 后台更新位置的后台服务_Java_Android_Background Service - Fatal编程技术网

Java 后台更新位置的后台服务

Java 后台更新位置的后台服务,java,android,background-service,Java,Android,Background Service,它杀死了后台服务,为了解决您的问题,您应该使用前台服务 我的后台粘性服务是oreo和heigher设备上的kill任何在后台服务上获取位置的解决方案当活动在后台时您必须显示ForegroundService的通知 public class ForegroundService extends Service { public static final String CHANNEL_ID = "ForegroundServiceChannel"; @Override

它杀死了后台服务,为了解决您的问题,您应该使用前台服务


我的后台粘性服务是oreo和heigher设备上的kill任何在后台服务上获取位置的解决方案当活动在后台时

您必须显示ForegroundService的通知

    public class ForegroundService extends Service {
    public static final String CHANNEL_ID = "ForegroundServiceChannel";
    @Override
    public void onCreate() {
        super.onCreate();
    }
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        String input = intent.getStringExtra("inputExtra");
        createNotificationChannel();
        Intent notificationIntent = new Intent(this, MainActivity.class);
        PendingIntent pendingIntent = PendingIntent.getActivity(this,
                0, notificationIntent, 0);
        Notification notification = new NotificationCompat.Builder(this, CHANNEL_ID)
                .setContentTitle("Foreground Service")
                .setContentText(input)
                .setSmallIcon(R.drawable.ic_stat_name)
                .setContentIntent(pendingIntent)
                .build();
        startForeground(1, notification);
        //do heavy work on a background thread
        //stopSelf();
        return START_NOT_STICKY;
    }
    @Override
    public void onDestroy() {
        super.onDestroy();
    }
    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }
}
并添加权限

<uses-permissionandroid:name=”android.permission.FOREGROUND_SERVICE” />

您将无法在Oreo中长期运行后台服务,因为存在行为更改,现在Oreo要优化系统内存、电池等,它会终止后台服务,要解决您的问题,您应该使用前台服务

看看后台执行限制

我的一个建议是,如果你能使用FCM,那就去吧,因为微信、Facebook等应用程序都使用FCM来发送通知,它们不会面临任何问题

由于客户要求在后台运行更新位置的服务,我选择了不使用FCM的替代解决方案。以下是步骤:-

  • 您需要启动后台服务,在Oreo和更高版本中显示通知
  • 之后,你们需要记住,一段时间后,手机会进入打瞌睡模式,所以你们也必须解决这个问题
  • 此外,应用程序也必须禁用电池优化
  • 在一些自定义ROM中,如果服务被android终止,您需要管理自动启动权限以重新启动服务
  • 最重要的是,如果服务被android系统终止,那么向广播接收器发送一条广播消息,再次重新启动服务
希望你能做更多的研发工作。
我分享了我在后台运行服务的经验和过程。

这是因为Android Oreo在后台执行时的行为发生了变化。建议的替代方案包括:

1)前台服务。

如果您确定在尝试使用location API检索位置时向用户显示通知,请使用此选项。这将是可靠的,并在文件中提出

文档中的示例应用程序: GitHub上的项目

允许应用程序继续用户启动的应用程序示例 无需始终请求访问后台位置的操作

2)工作经理


这种方法不太可靠,因为您无法控制何时调用它,但如果您根本不想向用户显示通知,则可以使用这种方法。

终止后,应用程序不工作后台服务。它只是前台和打开的应用程序,而它在所有设备上正常工作。它在所有设备上正常工作设备尝试这个androidwave.com/foreground-service-android-example我喜欢一个使用farground服务的oro和pie更新位置解决方案你找到真正的解决方案了吗?如果服务在后台运行,卸载应用程序调用哪个服务功能如果服务在后台运行,卸载应用程序可以调用哪个服务功能not@asif:-没有将被调用的方法,当用户卸载app@asif:-但是您可以注册一个广播接收器,当用户单击卸载按钮时,它将为您监听并触发一个侦听器。有关注册广播接收器的更多详细信息,请参阅此链接。您在服务onDestroy()和onTaskRemoved()中有两个方法,但这两个方法都不确定,这意味着每次终止服务时都会调用这些函数这一点不是固定的???这不是强制性的。。。我经历了很多困难,根据我的经验,在所有情况下调用onDestroy()并不是强制性的
References
https://developer.android.com/training/location/receive-location-updates