Any way to receive a broadcast before the network goes down

别来无恙 提交于 2020-02-02 19:59:09

问题


I already have a few receivers running, including one for reboot and entering flight mode. However I'm not always getting the Intent in time for me to do what I need to do, send a quick message to an external server.

Even setting the intent filter to priority 1000 doesn't guarantee that I get to transmit before it is too late, though it does help.

As stated, it is not that I do not receive my broadcasts, I just get them too late, after the network is already down.

Any pointers to where I may find some hints to this is most welcome, I've spend the past few days scouring the net with no luck.

Edit: I'm presently using the following actions split between 4 broadcast listeners

            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />

            <action android:name="android.intent.action.BOOT_COMPLETED" />

            <action android:name="android.intent.action.PHONE_STATE" />
            <action android:name="android.intent.action.NEW_OUTGOING_CALL" />
            <action android:name="android.intent.action.PROVIDER_CHANGED" />

            <action android:name="android.intent.action.ACTION_SHUTDOWN" />
            <action android:name="android.intent.action.AIRPLANE_MODE" />

Edit 2: I realized that I hadn't made my intentions entirely clear. I'm not talking about the Network state itself, but user actions to enter flight mode, or turning off the device.


回答1:


Refer to this code:

IntentFilter monitorInternetConnectivityFilter = new IntentFilter(
            ConnectivityManager.CONNECTIVITY_ACTION);
MonitorInternetConnectivity monitorInternetConnectivityReciever;
monitorInternetConnectivityReciever = new MonitorInternetConnectivity();
class MonitorInternetConnectivity extends BroadcastReceiver {

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

            if (!NetworkHelper.isOnline(context)) {
                //Toast.maketext("Network went down");
            }

        }

    }

@Override
    protected void onPause() {
        // TODO Auto-generated method stub
        super.onPause();

        unregisterReceiver(monitorInternetConnectivityReciever);

    }
@Override
    protected void onResume() {
        // TODO Auto-generated method stub
        super.onPause();

        registerReceiver(monitorInternetConnectivityReciever,
                monitorInternetConnectivityFilter);

    }

////////////////////// NetworkHelper

public static boolean isOnline(Context cxt) {
        ConnectivityManager cm = (ConnectivityManager) cxt
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo netInfo = cm.getActiveNetworkInfo();
        if (netInfo != null && netInfo.isConnectedOrConnecting() && canHit()) {

            Logger.debug(NetworkHelper.class, "mode is online");
            return true;
        }
        return false;
}

public static boolean canHit() {

        try {
            URL url = new URL("http://www.google.com/");
            HttpURLConnection urlConnection = (HttpURLConnection) url
                    .openConnection();
            urlConnection.setConnectTimeout(3000);
            urlConnection.connect();
            urlConnection.disconnect();
            return true;
        } catch (Exception e) {
            Logger.error(NetworkHelper.class, e.getMessage());
            return false;
        } 

    }

This code, will notify you when the network is down,not sure if it notifies you just before it



来源:https://stackoverflow.com/questions/11083553/any-way-to-receive-a-broadcast-before-the-network-goes-down

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!