广播接收器整个应用程序

时间:2013-12-11 09:57:36

标签: android broadcastreceiver android-broadcast

我在我的活动中使用广播接收器来检查互联网是否已连接,如果它已连接,那么它应该发出http请求,否则会显示一个警告对话框,说没有互联网连接,并且它正常工作。

问题我的整个应用程序使用互联网连接,所有活动。如何通过应用程序使广播接收器可用。

我的Broadcast Receiver class


private BroadcastReceiver mConnReceiver = new BroadcastReceiver() {
    public void onReceive(Context context, Intent intent) {
        boolean noConnectivity = intent.getBooleanExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, false);
        String reason = intent.getStringExtra(ConnectivityManager.EXTRA_REASON);
        boolean isFailover = intent.getBooleanExtra(ConnectivityManager.EXTRA_IS_FAILOVER, false);

        currentNetworkInfo = (NetworkInfo) intent.getParcelableExtra(ConnectivityManager.EXTRA_NETWORK_INFO);
        otherNetworkInfo = (NetworkInfo) intent.getParcelableExtra(ConnectivityManager.EXTRA_OTHER_NETWORK_INFO);

        if(currentNetworkInfo.isConnected())
        {

            Toast.makeText(getApplicationContext(), "Internet Connected", Toast.LENGTH_LONG).show();

        }
        else
        {

            Toast.makeText(getApplicationContext(), "Internet Not Connected", Toast.LENGTH_LONG).show();

        }
    }
};



onCreate

    this.registerReceiver(this.mConnReceiver,
            new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION));

4 个答案:

答案 0 :(得分:2)

我建议您将接收器解压缩到顶级课程,将其安装在所有入口点(每个活动用户可以在您的应用程序之外启动)。然后使用单例设置对象来保持状态,所有活动可能只是访问此设置对象以检查连接。

由于您的所有活动都需要它,甚至需要在连接更改时获得通知,您可以定义一个包含所有连接处理逻辑的基本活动,并让所有其他活动继承它。

答案 1 :(得分:1)

ConnectivityManager connMgr = (ConnectivityManager) 
        context.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();
    if (networkInfo != null && networkInfo.isConnected()) {
        // connected to internet
    } else {
        // no internet
    }

答案 2 :(得分:1)

为此制作接收器:

public class NetReceiver extends BroadcastReceiver {

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


      ConnectivityManager connectivityManager = (ConnectivityManager) 
                                   context.getSystemService(Context.CONNECTIVITY_SERVICE );
      NetworkInfo activeNetInfo = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
      boolean isConnected = activeNetInfo != null && activeNetInfo.isConnectedOrConnecting();   
      if (isConnected)       
             //Nothing
      else 
          Toast.makeText(context, "Not Connected", Toast.LENGTH_LONG).show(); 
    }
}

现在在androidmanifest.xml中添加它

<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>

如果您已添加以上内容,请检查此代码:

<receiver android:name=".NetReceiver" >
        <intent-filter>
            <action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
        </intent-filter>
    </receiver>

答案 3 :(得分:0)

如果您只是需要检查用户是否已连接到互联网,请检查

        ConnectivityManager conMgr = (ConnectivityManager)                
        context.getSystemService(Context.CONNECTIVITY_SERVICE);

        // ARE WE CONNECTED TO THE NET

        if (conMgr.getActiveNetworkInfo() != null && conMgr.getActiveNetworkInfo().isAvailable() && conMgr.getActiveNetworkInfo().isConnected())
        {
            return true;
        }
        else
        {
            return false;
        }
在开始Http请求之前

。您不需要为此注册BroadcastReceiver