简体   繁体   English

使用BroadcastReceiver进行连接更改时出现奇怪的错误

[英]Weird bug when using BroadcastReceiver for connection changes

I implemented Volley library in one of my apps! 我在其中一个应用程序中实现了Volley库! I have a data load button and when i click it, it checks for a timestamp and then accordingly downloads the data, if there are new or notifies the user that he already has the latest data available. 我有一个数据加载按钮,当我单击它时,它会检查时间戳记,然后相应地下载数据,如果有新数据或通知用户他已经拥有最新数据。

Though i wanted to also implement a receiver for Connectivity changes in order to download the data or update them, when the app starts. 虽然我还想为连接性更改实现一个接收器,以便下载数据或更新它们,但是在应用程序启动时。 Everything goes fine till the time it reaches the StringRequest for the timestamp value. 一切正常,直到到达时间戳值的StringRequest为止。

At this point something really weird happens. 在这一点上,确实发生了一些奇怪的事情。 Although the Request responses back with the correct timestamp, the code first executes the get method for the timestamp and then the set method which makes the timestamp variable be null. 尽管请求以正确的时间戳返回,但代码首先执行时间戳的get方法,然后执行set方法,该方法使timestamp变量为null。 This happens only when the data check is triggered by the BroadcastReceiver. 仅当数据检查由BroadcastReceiver触发时才会发生。

When it's manually triggered everything goes fine.... 手动触发后,一切都会好起来...

CheckTimestamp Method CheckTimestamp方法

private void checkTimestamp(){
    mTimestampRequest = new StringRequest(DatabaseSharer.TIMESTAMP_URL,
            new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {
                    Log.d(Menu.class.getSimpleName(), "Responsed back -- " + response);
                    setmTimeStamp(response);
                }
            },new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                }
            }
    );
    mRequestQueue.add(mTimestampRequest);
}

Order of method calls 方法调用顺序

        checkTimestamp();
        Log.d(Menu.class.getSimpleName(),"dataStaff-1st else entered");
        String lastTimestamp = shared.getString("time", "none");
        final String temp = getmTimeStamp();
        Log.d(Menu.class.getSimpleName(), "temp= " + temp + " -- lasttimestamp = " + lastTimestamp);

Receiver Logcat (see that the get is null and executed before the set) 接收器Logcat(请参阅get为null并在设置之前执行) 在此处输入图片说明

Logcat when it triggers manually (get is also executed before but has the correct response) Logcat手动触发时(get之前也已执行但响应正确) 在此处输入图片说明

I am out of ideas so if anyone can help me i would be thankful!! 我没有主意,所以如果有人可以帮助我,我将很感激!

Receiver Lifecycle 接收者生命周期

A BroadcastReceiver object is only valid for the duration of the call to onReceive(Context, Intent). BroadcastReceiver对象仅在对onReceive(Context,Intent)的调用期间有效。 Once your code returns from this function, the system considers the object to be finished and no longer active. 一旦您的代码从该函数返回,系统将认为该对象已完成并且不再处于活动状态。

This has important repercussions to what you can do in an onReceive(Context, Intent) implementation: anything that requires asynchronous operation is not available, because you will need to return from the function to handle the asynchronous operation, but at that point the BroadcastReceiver is no longer active and thus the system is free to kill its process before the asynchronous operation completes. 这对您在onReceive(Context,Intent)实现中可以执行的操作具有重要影响:要求异步操作的任何内容都不可用,因为您需要从函数中返回以处理异步操作,但是到那时,BroadcastReceiver是不再活动,因此系统可以在异步操作完成之前终止其进程。

In particular, you may not show a dialog or bind to a service from within a BroadcastReceiver. 特别是,您可能不会显示对话框或从BroadcastReceiver内部绑定到服务。 For the former, you should instead use the NotificationManager API. 对于前者,您应该改用NotificationManager API。 For the latter, you can use Context.startService() to send a command to the service. 对于后者,可以使用Context.startService()将命令发送到服务。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 连接状态更改时,BroadcastReceiver使Android应用程序崩溃 - BroadcastReceiver crashes Android application when connection state changes 连接更改时奇怪的应用程序行为 - Weird app behavior when connection changes 使用Intent和BroadcastReceiver获取Android电池电量时的奇怪行为 - Weird behaviour when using intent and BroadcastReceiver to get android battery level 我的数据库中的怪异错误导致什么都不应该发生的更改 - Weird bug in my databases causing changes when none should happen WiFi更改时未调用BroadcastReceiver - BroadcastReceiver not called when WiFi changes 在Fragment中使用ViewPager时,我有一个奇怪的错误 - I've got a weird bug when using a ViewPager inside a Fragment 我的在线状态更改时,Android Broadcastreceiver崩溃 - Android Broadcastreceiver crashes when my onlinestate changes 使用BroadcastReceiver检测状态更改不起作用 - Detecting state changes using BroadcastReceiver is not working 如何使用intentfilter和broadCastReceiver检查WiFi连接? - How to check WiFi connection using intentfilter and broadCastReceiver? 在BroadcastReceiver上使用newInstance时的InstantiationException - InstantiationException when using newInstance on BroadcastReceiver
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM