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

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

问题描述

您好我开发一个Android的广播接收器,用于检查网络连接。

但问题是,我的广播接收机为两次。

其次,我想只有它被称为当网络可用。如果是不可用的,我不希望得到通知。

这是广播接收器

 公共类NetworkChangeReceiver扩展的BroadcastReceiver {

    @覆盖
    公共无效的onReceive(最终上下文的背景下,最终的意图意图){
        最后ConnectivityManager connMgr =(ConnectivityManager)上下文
                .getSystemService(Context.CONNECTIVITY_SERVICE);

        最后android.net.NetworkInfo无线= connMgr
                .getNetworkInfo(ConnectivityManager.TYPE_WIFI);

        最后android.net.NetworkInfo移动= connMgr
                .getNetworkInfo(ConnectivityManager.TYPE_MOBILE);

        如果(wifi.isAvailable()|| mobile.isAvailable()){
            // 做一点事

            Log.d(Netowk可用,旗1号);
        }
    }
}
 

这是manifest.xml的

 <舱单的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    包=com.example.broadcastreceiverforinternetconnection
    安卓版code =1
    机器人:VERSIONNAME =1.0>

    <用途-SDK
        安卓的minSdkVersion =8
        机器人:targetSdkVersion =17/>
    <使用-权限的Andr​​oid:名称=android.permission.ACCESS_NETWORK_STATE/>

    <应用
        机器人:allowBackup =真
        机器人:图标=@可绘制/ ic_launcher
        机器人:标签=@字符串/ APP_NAME
        机器人:主题=@风格/ AppTheme>
        <接收机器人:名称=。NetworkChangeReceiver>
            <意向滤光器>
                <作用机器人:名称=android.net.conn.CONNECTIVITY_CHANGE/>
                <作用机器人:名称=android.net.wifi.WIFI_STATE_CHANGED/>
            &所述; /意图滤光器>
        < /接收器>
    < /用途>

< /舱单>
 

解决方案

你的第一个ANS :你的广播接收器调用两次,因为

您已经添加了两个<意向滤光器> 听的网络连接更改

<作用机器人:名称=android.net.conn.CONNECTIVITY_CHANGE/>

另外支持WiFi状态,变<作用机器人:名称=android.net.wifi.WIFI_STATE_CHANGED/>

所以只用一个&lt;作用机器人:名称=android.net.conn.CONNECTIVITY_CHANGE/&GT; 你的第一个问题将得到解决。 (而不是呼叫接收两次叫一次,当连接的变化)。欲了解更多详细信息,请参阅:<一href="http://stackoverflow.com/a/15546897/1168654">http://stackoverflow.com/a/15546897/1168654

您的第二个答案:(您想接收器只能拨打一次,如果互联网连接可用)

http://stackoverflow.com/a/15546897/1168654 与code参考是完美的。你只通知时,互联网可

更新

您可以使用此方法来检查您的连接,如果你想只检查与移动互联网是否连接。

 公共布尔isOnline(上下文的背景下){

    ConnectivityManager厘米=(ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
    的NetworkInfo的NetInfo = cm.getActiveNetworkInfo();
    //应该检查null,因为空气中的计划模式,这将是空
    返回(NetInfo的= NULL和放大器;!&安培; netInfo.isConnected());

}
 

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

But the problem is that my broadcast receiver for two times.

Secondly, I want to get it called only when network is available. If it is unavailable, I will not want to get notified.

This is the broadcast receiver

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("Netowk Available ", "Flag No 1");
        }
    }
}

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>

解决方案

Your first ans: your broadcast receiver called two times because

You have added two <intent-filter> for listening to change in network connection

<action android:name="android.net.conn.CONNECTIVITY_CHANGE" />

Also for wifi state, change "<action android:name="android.net.wifi.WIFI_STATE_CHANGED" />"

so just use one <action android:name="android.net.conn.CONNECTIVITY_CHANGE" /> your first problem will be solved. (instead of call receiver two times it called one time when connection change). For more detail see: http://stackoverflow.com/a/15546897/1168654

Your second answer: (you want receiver to call only one time if internet connection available.)

Reference from http://stackoverflow.com/a/15546897/1168654 your code is perfect. you notify only when internet is available

UPDATE

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

public boolean isOnline(Context context) {

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

}

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

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