android sdk中网络事件的意图操作
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2294971/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Intent action for network events in android sdk
提问by mudit
I need to receive broadcasts for network actions like network connected, disconnected etc. I am using a broadcast receiver for this purpose. Can anyone please tell me which intent action I need to capture for network events, right now as per my search on internet I am using android.net.ConnectivityManager.CONNECTIVITY_ACTION.
我需要接收网络操作的广播,例如网络连接、断开连接等。为此我使用了广播接收器。任何人都可以告诉我我需要为网络事件捕获哪个意图操作,现在根据我在互联网上的搜索,我正在使用android.net.ConnectivityManager.CONNECTIVITY_ACTION。
Here is my broadcast receiver class:
这是我的广播接收器类:
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
public class NetworkStateReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
if (intent.getAction().equals(
android.net.ConnectivityManager.CONNECTIVITY_ACTION)) {
// do something..
}
}
}
and I have also added permission for accessing network state:
我还添加了访问网络状态的权限:
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
here is how I have declared this class in manifest file
这是我在清单文件中声明此类的方式
<receiver class=".NetworkStateReceiver" android:name=".NetworkStateReceiver">
<intent-filter>
<action android:name="android.net.ConnectivityManager.CONNECTIVITY_ACTION" />
</intent-filter>
</receiver>
Please suggest me the right intent action if I am wrong OR if there is any other way to catch network events.
如果我错了,或者是否有任何其他方法可以捕获网络事件,请向我建议正确的意图操作。
回答by yanchenko
Here's a working example:
这是一个工作示例:
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<receiver android:name=".receiver.ConnectivityReceiver">
<intent-filter>
<action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
</intent-filter>
</receiver>
.
.
public class ConnectivityReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Log.d(ConnectivityReceiver.class.getSimpleName(), "action: "
+ intent.getAction());
}
}
回答by Ravi K. Sharma
yanchenko's answer is very much useful, i am just simplifying a little bit to get connection status, please modify onReceive as below :
yanchenko 的回答非常有用,我只是简化了一点以获得连接状态,请修改 onReceive 如下:
public class ConnectivityReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Log.d(ConnectivityReceiver.class.getSimpleName(), "action: "
+ intent.getAction());
MyConstants.IS_NETWORK_AVAILABLE = haveNetworkConnection(context);
//IS_NETWORK_AVAILABLE this variable in your activities to check networkavailability.
}
private boolean haveNetworkConnection(Context context) {
boolean haveConnectedWifi = false;
boolean haveConnectedMobile = false;
ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo[] netInfo = cm.getAllNetworkInfo();
for (NetworkInfo ni : netInfo) {
if (ni.getTypeName().equalsIgnoreCase("WIFI"))
if (ni.isConnected())
haveConnectedWifi = true;
if (ni.getTypeName().equalsIgnoreCase("MOBILE"))
if (ni.isConnected())
haveConnectedMobile = true;
}
return haveConnectedWifi || haveConnectedMobile;
}
}