网络出现故障之前,任何方式接收广播出现故障、方式、网络

2023-09-08 08:50:44 作者:最好的人

我已经有几个接收器上运行,其中包括一个重新启动,并进入飞行模式。不过,我并不总是得到意向及时为我做什么,我需要做的,发送快速消息给外部服务器。

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.

编辑:我是presently使用4个广播监听器之间的分裂以下操作

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" />

编辑2:我意识到,我还没有作出我的意图完全明确。我不是在谈论网络状态本身,而是用户操作进入飞行模式,或关闭设备。

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.

推荐答案

请参阅本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

////////////////////// 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;
        } 

    }

这code,如果它只是之前通知您将通知您,当网络出现故障,不知道它

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