android用什么替代service,android-使用JobScheduler代替BroadcastReceiver和Service

本文探讨了如何在设备连接到互联网时,通过后台服务上传数据库记录。目前采用的方式是BroadcastReceiver监听网络状态变化,当有网络且数据库有未上传记录时启动服务。然而,从Android 7.0开始,隐式广播的使用受到限制。作者提出了疑问,是否可以改用JobScheduler实现相同功能,并认为JobScheduler是更优的选择,因为它提供了一种在网络条件满足时调度任务的可靠机制。
摘要由CSDN通过智能技术生成

我正在开发一个应用程序,当设备连接到互联网时,该应用程序将在后台将所有数据库记录顺序上传到服务器.

为此,我编写了一个BroadcastReceiver,它将监听网络连接.触发此接收器后,我将启动后台服务以上传记录.

这是我的代码.

public class NetworkChangeReceiver extends BroadcastReceiver {

@Override

public void onReceive(final Context context, final Intent intent) {

AppUtils.checkInternetConnection(context));

//If the device has the internet connection and if there are any pending records to upload to server then start the service for uploading the records.

if (AppUtils.checkInternetConnection(context)) {

if (Database.getInstance().getTotalRecordsCount() > 0) {

context.startService(new Intent(context, SurveyUploadService.class));

}

} else {

context.stopService(new Intent(context, SurveyUploadService.class));

}

}

}

现在我的疑问是

1.我可以使用JobScheduler做同样的事情吗?

2.有什么更好的方法(我的还是使用JobScheduler的方法)?为什么?

解决方法:

我不知道您要对BroadcastReceiver使用哪个操作,但是我想它是CONNECTIVITY_CHANGE操作.如果使用它,请从大约Android 7.0 Behavior Changes的一侧阅读以下文本:

To alleviate these issues, Android 7.0 applies the following optimizations:

Apps targeting Android 7.0 do not receive CONNECTIVITY_ACTION broadcasts, even if they have manifest entries to request notification of these events. Apps that are running can still listen for CONNECTIVITY_CHANGE on their main thread if they request notification with a BroadcastReceiver.

Apps cannot send or receive ACTION_NEW_PICTURE or ACTION_NEW_VIDEO broadcasts. This optimization affects all apps, not only those targeting Android 7.0.

If your app uses any of these intents, you should remove dependencies on them as soon as possible so that you can target Android 7.0 devices properly. The Android framework provides several solutions to mitigate the need for these implicit broadcasts. For example, the JobScheduler API provides a robust mechanism to schedule network operations when specified conditions, such as connection to an unmetered network, are met. You can even use JobScheduler to react to changes to content providers.

因此,最好使用JobScheduler API.

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在Linux系统中,当服务启动失败时,通常会提示“Job for xxx.service failed because the control process exited with error code. See 'systemctl status xxx.service' and 'journalctl -xe' for details.”这个错误信息。这个错误信息提示我们可以通过运行'systemctl status xxx.service'和'journalctl -xe'来查看详细的错误信息。 对于'Job for openstack-nova-scheduler.service failed because the control process exited with error code.'这个错误,我们可以按照以下步骤来解决: 1. 运行'systemctl status openstack-nova-scheduler.service'命令来查看服务的状态和错误信息。 2. 运行'journalctl -xe'命令来查看系统日志,找到与服务启动相关的错误信息。 3. 根据错误信息来修复问题。可能的解决方案包括重新安装服务、更新配置文件、检查依赖项等。 以下是一个例子,演示如何通过运行'systemctl status'和'journalctl -xe'来查看错误信息并解决问题: ```bash $ systemctl status openstack-nova-scheduler.service ● openstack-nova-scheduler.service - OpenStack Nova Scheduler Server Loaded: loaded (/usr/lib/systemd/system/openstack-nova-scheduler.service; enabled; vendor preset: disabled) Active: failed (Result: exit-code) since Tue 2021-11-02 10:00:00 CST; 5s ago Process: 1234 ExecStart=/usr/bin/nova-scheduler (code=exited, status=1/FAILURE) Main PID: 1234 (code=exited, status=1/FAILURE) Nov 02 10:00:00 server systemd[1]: Starting OpenStack Nova Scheduler Server... Nov 02 10:00:00 server nova-scheduler[1234]: ERROR: Unable to connect to AMQP server: [Errno 111] ECONNREFUSED. Trying again in 1 seconds. Nov 02 10:00:01 server nova-scheduler[1234]: ERROR: Unable to connect to AMQP server: [Errno 111] ECONNREFUSED. Trying again in 2 seconds. Nov 02 10:00:03 server nova-scheduler[1234]: ERROR: Unable to connect to AMQP server: [Errno 111] ECONNREFUSED. Trying again in 4 seconds. Nov 02 10:00:07 server nova-scheduler[1234]: ERROR: Unable to connect to AMQP server: [Errno 111] ECONNREFUSED. Trying again in 8 seconds. Nov 02 10:00:15 server nova-scheduler[1234]: ERROR: Unable to connect to AMQP server: [Errno 111] ECONNREFUSED. Trying again in 16 seconds. Nov 02 10:00:31 server nova-scheduler[1234]: ERROR: Unable to connect to AMQP server: [Errno 111] ECONNREFUSED. Trying again in 32 seconds. Nov 02 10:01:03 server nova-scheduler[1234]: ERROR: Unable to connect to AMQP server: [Errno 111] ECONNREFUSED. Trying again in 64 seconds. Nov 02 10:02:07 server systemd[1]: openstack-nova-scheduler.service: main process exited, code=exited, status=1/FAILURE Nov 02 10:02:07 server systemd[1]: Failed to start OpenStack Nova Scheduler Server. ``` 从上面的输出中,我们可以看到服务启动失败,错误信息是“Unable to connect to AMQP server: [Errno 111] ECONNREFUSED.”。这个错误提示我们无法连接到AMQP服务器,可能是由于AMQP服务器未启动或配置错误导致的。 接下来,我们可以运行'journalctl -xe'命令来查看系统日志,找到与服务启动相关的错误信息: ```bash $ journalctl -xe ... Nov 02 10:00:00 server nova-scheduler[1234]: ERROR: Unable to connect to AMQP server: [Errno 111] ECONNREFUSED. Trying again in 1 seconds. Nov 02 10:00:01 server nova-scheduler[1234]: ERROR: Unable to connect to AMQP server: [Errno 111] ECONNREFUSED. Trying again in 2 seconds. Nov 02 10:00:03 server nova-scheduler[1234]: ERROR: Unable to connect to AMQP server: [Errno 111] ECONNREFUSED. Trying again in 4 seconds. Nov 02 10:00:07 server nova-scheduler[1234]: ERROR: Unable to connect to AMQP server: [Errno 111] ECONNREFUSED. Trying again in 8 seconds. Nov 02 10:00:15 server nova-scheduler[1234]: ERROR: Unable to connect to AMQP server: [Errno 111] ECONNREFUSED. Trying again in 16 seconds. Nov 02 10:00:31 server nova-scheduler[1234]: ERROR: Unable to connect to AMQP server: [Errno 111] ECONNREFUSED. Trying again in 32 seconds. Nov 02 10:01:03 server nova-scheduler[1234]: ERROR: Unable to connect to AMQP server: [Errno 111] ECONNREFUSED. Trying again in 64 seconds. ... ``` 从上面的输出中,我们可以看到与服务启动相关的错误信息是“Unable to connect to AMQP server: [Errno 111] ECONNREFUSED.”,这个错误信息与'systemctl status'命令输出的错误信息是一致的。 因此,我们可以得出结论,服务启动失败是由于无法连接到AMQP服务器导致的。我们需要检查AMQP服务器是否已启动,并确保配置文件中的AMQP服务器地址和端口号正确。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值