用于在 Android 应用程序中检查互联网连接的广播接收器 [英] Broadcast receiver for checking internet connection in android app

查看:20
本文介绍了用于在 Android 应用程序中检查互联网连接的广播接收器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个用于检查互联网连接的安卓广播接收器.

I am developing an android broadcast receiver for checking internet connection.

问题是我的广播接收器被调用了两次.我希望它仅在网络可用时才被调用.如果它不可用,我不想通知.

The problem is that my broadcast receiver is being called two times. I want it to get called only when the network is available. If it is unavailable, I don't want notified.

这是广播接收器

public class NetworkChangeReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(final Context context, final Intent intent) {
        final ConnectivityManager connMgr = (ConnectivityManager) context
                .getSystemService(Context.CONNECTIVITY_SERVICE);

        final android.net.NetworkInfo wifi = connMgr
                .getNetworkInfo(ConnectivityManager.TYPE_WIFI);

        final android.net.NetworkInfo mobile = connMgr
                .getNetworkInfo(ConnectivityManager.TYPE_MOBILE);

        if (wifi.isAvailable() || mobile.isAvailable()) {
            // Do something

            Log.d("Network Available ", "Flag No 1");
        }
    }
}

这是 manifest.xml

This is the manifest.xml

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.broadcastreceiverforinternetconnection"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <receiver android:name=".NetworkChangeReceiver" >
            <intent-filter>
                <action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
                <action android:name="android.net.wifi.WIFI_STATE_CHANGED" />
            </intent-filter>
        </receiver>
    </application>

</manifest>

推荐答案

回答您的第一个问题:您的广播接收器被调用两次,因为

Answer to your first question: Your broadcast receiver is being called two times because

您添加了两个

  1. 网络连接改变:
    <action android:name="android.net.conn.CONNECTIVITY_CHANGE"/>

WiFi 状态的变化:
<action android:name="android.net.wifi.WIFI_STATE_CHANGED"/>

Change in WiFi state:
<action android:name="android.net.wifi.WIFI_STATE_CHANGED" />

只需使用一个:
<action android:name="android.net.conn.CONNECTIVITY_CHANGE"/>.

它只会响应一个动作而不是两个动作.请参阅此处了解更多信息.

It will respond to only one action instead of two. See here for more information.

回答您的第二个问题(如果互联网连接可用,您希望接收方仅拨打一次电话):

Answer to your second question (you want receiver to call only one time if internet connection available):

你的代码很完美;您仅在互联网可用时通知.

Your code is perfect; you notify only when internet is available.

更新

如果您只想检查移动设备是否连接到互联网,可以使用此方法检查您的连接.

You can use this method to check your connectivity if you want just to check whether mobile is connected with the internet or not.

public boolean isOnline(Context context) {

    ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo netInfo = cm.getActiveNetworkInfo();
    //should check null because in airplane mode it will be null
    return (netInfo != null && netInfo.isConnected());
}

这篇关于用于在 Android 应用程序中检查互联网连接的广播接收器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆