BroadcastReceiver无法访问互联网 [英] BroadcastReceiver has no internet access

本文介绍了BroadcastReceiver无法访问互联网的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的广播接收器遇到问题.

I'm facing a problem with my broadcast receiver.

我已将闹钟设置为上午6点,它必须触发我的广播接收器,广播接收器只需从互联网下载一些数据并进行处理即可.

I have set an alarm for 6am, it has to fire my broadcast receiver, which only has to download some data from internet and process it.

例如,如果我将闹钟设置为下午3点,则可以正常工作.但是问题出在上午6点,由于没有网络连接,下载失败.

If I set the alarm for 3pm, for example, it works fine. But the problem is at 6am, it fails downloading because it does not have network connectivity.

在尝试下载之前,我执行了部分唤醒锁定.可以与此相关吗?手机是否进入深度睡眠并且部分唤醒锁还不够?

I perform a partial wake lock before attempting download. Can it be related to this? Does the phone enter some deep sleep and partial wake lock is not enough?

那还能是什么?我已经仔细检查过,让手机保持启用的网络数据功能,并且在夜间确实会收到电子邮件和whatsapp.

What else can it be? I have double checked to leave the phone with Network Data enabled, and I do receive emails and whatsapp during night time.

有没有办法让android恢复该连接?

Is there a way to make android recover that connectivity?

欢迎任何提示或帮助!

最诚挚的问候, 费德里科.

Best regards, Federico.

我的代码:

来自BroadcastReceiver的OnReceive方法:

OnReceive method from BroadcastReceiver:

@Override
public void onReceive(Context context, Intent intent) {
    ...
    // acquire partial wake lock
    _PowerManager.acquire();

    // check internet access
    if (!_Utils.isDataEnabled()){
        // here is where it enters at 6am, isDataEnabled return false, so it enters here
        _Log.d("_BroadcastReceiver_Synchronize:onReceive","No internet, cancel sinc");
         // release partial wake lock
        _PowerManager.release();
        return;
    }

    // excecute async task that downloads data
    _WebServicesGet ws = new _WebServicesGet(null, null, null);
    ws.syncAll(this, false);
    return;
}

_Utils.isDataEnabled:

_Utils.isDataEnabled:

public static Boolean isDataEnabled() {
    // this method returns false at 6am
    ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
    return activeNetworkInfo != null && activeNetworkInfo.isConnected();
}

推荐答案

CommonsWare使我处于正确的轨道.

CommonsWare put me in the right track.

答案是打ze睡模式:Android 6及更高版本可以将手机置于打mode睡模式,在该模式下,应用程序将无法访问互联网(除其他外).因此,如果您设置了警报和唤醒锁,则可以访问CPU,但不能访问Internet.该文档说,如果插入设备,它将不会进入打ze模式,但就我而言,尽管已插入设备,但它确实会进入:

The answer was the doze mode: android 6 and up can put the phone into a doze mode, in which apps won't receive internet access (among other things). So if you have set an alarm and a wake lock, you will get CPU access but no Internet access. The documentation says that if the device is plugged in it will not enter doze mode, but in my case it did enter despite the plug status:

来自官方文档:

如果用户在断开屏幕电源的状态下将设备拔下电源并保持静止一段时间,则设备将进入打ze模式.

If a user leaves a device unplugged and stationary for a period of time, with the screen off, the device enters Doze mode.

那令人困惑.

无论哪种方式,我都尝试将我的应用列入白名单,并且该应用开始正常运行.警报在上午6点响起,并且广播接收器现在可以访问Internet.

Any way, I tried whitelisting my app, and it started working just fine. Alarm goes off at 6am, and the broadcast receiver now has internet access.

再次官方文档:

用户可以在设置">电池">手动"中配置白名单. 电池优化.另外,系统提供了应用程序的方法 要求用户将其列入白名单.

Users can manually configure the whitelist in Settings > Battery > Battery Optimization. Alternatively, the system provides ways for apps to ask users to whitelist them.

希望我能说清楚,这对别人有帮助.

Hope I made myself clear, and that this helps someone else.

感谢CommonsWare.

Thanks to CommonsWare.

这篇关于BroadcastReceiver无法访问互联网的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆