简体   繁体   中英

Weird bug when using BroadcastReceiver for connection changes

I implemented Volley library in one of my apps! 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.

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. This happens only when the data check is triggered by the BroadcastReceiver.

When it's manually triggered everything goes fine....

CheckTimestamp Method

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 when it triggers manually (get is also executed before but has the correct response) 在此处输入图片说明

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). 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.

In particular, you may not show a dialog or bind to a service from within a BroadcastReceiver. For the former, you should instead use the NotificationManager API. For the latter, you can use Context.startService() to send a command to the service.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM