android中 检查 网络连接状态的变化,无网络时跳转到设置界面
2014-04-25 23:45
656 查看
转自:http://blog.sina.com.cn/s/blog_67d95f400100pcro.html
1:
在AndroidManifest.xml中加一个声明
<receiver android:name="NetCheckReceiver">
<intent-filter>
<action
android:name="android.net.conn.CONNECTIVITY_CHANGE" />
</intent-filter>
</receiver>
NetCheckReceive.java文件如下
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
public class NetCheckReceiver extends BroadcastReceiver{
//android
中网络变化时所发的Intent的名字
private
static final String netACTION="android.net.conn.CONNECTIVITY_CHANGE";
@Override
public
void onReceive(Context context, Intent intent) {
if(intent.getAction().equals(netACTION)){
//Intent中ConnectivityManager.EXTRA_NO_CONNECTIVITY这个关键字表示着当前是否连接上了网络
//true
代表网络断开 false
代表网络没有断开
if(intent.getBooleanExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY,
false)){
aActivity.stopFlag
= true;
}else{
aActivity.stopFlag
= false;
}
}
}
}
aActivity.stopFlag 是一个static布尔值 可以出现在程序需要判断网络情况的地方。
2顺便加一个不需要broadcastReceiver就可以查看网络的例子。
这个方法可以放在需要检查网络通讯情况的地方。返回值为true代表网络畅通。
private boolean checkNetwork() {
ConnectivityManager conn = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo net = conn.getActiveNetworkInfo();
if (net != null && net.isConnected()) {
return true;
}
return false;
}
以下的这个判断可以使用户在失去网络链接的情况下,自动跳转到设置无线网界面。
if (!checkNetwork()) {
Toast.makeText(this, R.string.lose_network, Toast.LENGTH_LONG).show();
Intent intent = new Intent("android.settings.WIRELESS_SETTINGS");
startActivity(intent);
return;
}
1:
在AndroidManifest.xml中加一个声明
<receiver android:name="NetCheckReceiver">
<intent-filter>
<action
android:name="android.net.conn.CONNECTIVITY_CHANGE" />
</intent-filter>
</receiver>
NetCheckReceive.java文件如下
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
public class NetCheckReceiver extends BroadcastReceiver{
//android
中网络变化时所发的Intent的名字
private
static final String netACTION="android.net.conn.CONNECTIVITY_CHANGE";
@Override
public
void onReceive(Context context, Intent intent) {
if(intent.getAction().equals(netACTION)){
//Intent中ConnectivityManager.EXTRA_NO_CONNECTIVITY这个关键字表示着当前是否连接上了网络
//true
代表网络断开 false
代表网络没有断开
if(intent.getBooleanExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY,
false)){
aActivity.stopFlag
= true;
}else{
aActivity.stopFlag
= false;
}
}
}
}
aActivity.stopFlag 是一个static布尔值 可以出现在程序需要判断网络情况的地方。
2顺便加一个不需要broadcastReceiver就可以查看网络的例子。
这个方法可以放在需要检查网络通讯情况的地方。返回值为true代表网络畅通。
private boolean checkNetwork() {
ConnectivityManager conn = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo net = conn.getActiveNetworkInfo();
if (net != null && net.isConnected()) {
return true;
}
return false;
}
以下的这个判断可以使用户在失去网络链接的情况下,自动跳转到设置无线网界面。
if (!checkNetwork()) {
Toast.makeText(this, R.string.lose_network, Toast.LENGTH_LONG).show();
Intent intent = new Intent("android.settings.WIRELESS_SETTINGS");
startActivity(intent);
return;
}
相关文章推荐
- android中 检查 网络连接状态的变化,无网络时跳转到设置界面
- android检查网络连接状态的变化,无网络时跳转到设置界面
- Android中检查网络连接状态的变化,无网络时跳转到设置界面
- Android中检查网络连接状态的变化无网络时跳转到设置界面
- Android中 检查网络连接状态的变化,无网络时跳转到设置界面
- android中 检查网络连接状态的变化,无网络时跳转到设置界面
- android中 检查 网络连接状态的变化,无网络时跳转到设置界面
- (转)android中 检查网络连接状态的变化,无网络时跳转到设置界面
- android中 检查 网络连接状态的变化,无网络时跳转到设置界面
- 【Android进阶】判断网络连接状态并自动界面跳转
- 【Android进阶】判断网络连接状态并自动界面跳转
- android判断当前网络状态及跳转到设置界面
- android判断当前网络状态及跳转到设置界面
- android 检查网络连接状态实现步骤
- Android编程获取网络连接状态及调用网络配置界面
- android 检查网络连接状态实现步骤
- 判断网络连接,在没有开启网络的时候自动跳转到设置界面
- android 检查网络连接状态实现步骤<一>
- android 检查网络连接状态实现步骤
- Android 判断当前网络连接是否可用、打开设置网络界面