开发者

How to monitor connectivity changes when nothing else is running

开发者 https://www.devze.com 2023-03-15 04:47 出处:网络
I have several timers that poll information off the internet. When I get an error trying to do so, I check the network connectivity, and if it is not available, then I stop the timers.My issue is that

I have several timers that poll information off the internet. When I get an error trying to do so, I check the network connectivity, and if it is not available, then I stop the timers. My issue is that I want to restart the timers when the network becomes available again.

The issue is that if the network goes away and doesn't come back for a long time, then probably every part of my app that was running has now been killed or 开发者_高级运维so. My question is...where do I create my network connectivity changes receiver so that it will always be able to respond to those events? Obviously not in an activity.

I had originally put it in an extended Application class. I'm not sure if that was correct, and I'm finding now that even that goes away, but gets created again any time some part of your app gets utilized. So is that still the correct place?

I'm also a bit confused...I had thought to create a standalone receiver class to monitor the network changes and then restart the timers if it got a connection message. But then I thought....what would instantiate this? If nothing did, then how would it work? But the same question then goes to my OnBootReceived class....I do nothing to instantiate that class anywhere and register it, yet it still works! (Other than putting it in the AndroidManifest and the BOOT_COMPLETED intent filter). Is that kind of receiver special? I tried doing the same thing with my network change receiver, but it never responded to any network changes. When I finally put it in my application class, instantiated it, and registered it, THEN it received the broadcasts.

I have code, but this is kind of a code-independent question. I'm trying to clear up WHERE my code goes. IF I get past that and it still doesn't work, I can post it.

So, I'm almost understanding. If I do nothing more than have this in my code, will I receive messages when the connectivity state changes? Because if so, I'm not receiving any and this is what I have.

Or do I need to register this on boot somehow? The video mentions using the manifest intent filter to get updates when your app isn't running, but doesn't show if it still needs to be registered somehow somewhere (and if you DO register, aren't you registering an instance, and if so, won't that instance disappear when whatever object you were registering it in goes away?)

Code:

public class NetworkConnectivityReceiver extends BroadcastReceiver {

    Context context;

    @Override
    public void onReceive(Context context, Intent intent) {



        Log.d(TAG, "Connection message received.");

    }

}


    <receiver android:name=".NetworkConnectivityReceiver" >
        <intent-filter>
            <action android:name="android.net.ConnectivityManager.CONNECTIVITY_ACTION"></action>
        </intent-filter>
    </receiver>


Take a look at the video below from google io 2011, around about 28mins it covers connectivity changes, and helped me out alot when i was doing this.

http://youtu.be/twmuBbC_oB8

You would probably need to use an Intent to monitor the connectivity changes, setup a pending intent to broadcast whenever the connectivity changes then setup a broadcast receiver to process the change. (the video show something similar but with location changes at around 22 mins)

0

精彩评论

暂无评论...
验证码 换一张
取 消