手机网络判断及进入设置
2017-09-04 22:31
381 查看
掌握网络是否连接以及网络类型的判断,掌握无网络情况下,跳转设置网络设置界面
(切记注册权限)
<uses-permissionandroid:name="android.permission.INTERNET"/>
1.自定义广播类
2.完成注册
3.编写网络判断的工具类
(1).判断手机网络是否连接
(2).判断是否是wifi
(3).判断是否是手机流量
4.使用网络判断的工具类,判断当前用户手机的网络情况
(1).
(2).
(3).
5.无网络时,跳转网络设置界面
下面是自己编写的类:
一:网络判断工具类:
[java]
view plain
copy
/**
* 网络判断的工具类
*/
public class NetWorkUtils {
//判断网络是否连接
public static boolean isNetWorkAvailable(Context context) {
//网络连接管理器
ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
//网络信息
NetworkInfo info = connectivityManager.getActiveNetworkInfo();
if (info != null) {
return true;
}
return false;
}
//判断是否是wifi
public static boolean isWifi(Context context) {
//网络连接管理器
ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
//网络信息
NetworkInfo info = connectivityManager.getActiveNetworkInfo();
if (info != null && info.getType() == connectivityManager.TYPE_WIFI) {
return true;
}
return false;
}
//判断是否是手机流量
public static boolean isMobile(Context context) {
//网络连接管理器
ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
//网络信息
NetworkInfo info = connectivityManager.getActiveNetworkInfo();
if (info != null && info.getType() == connectivityManager.TYPE_MOBILE) {
return true;
}
return false;
}
}
[java]
view plain
copy
二:ManActivity
[java]
view plain
copy
<pre name="code" class="java">/**
* 掌握网络是否连接以及网络类型的判断,掌握无网络情况下,跳转设置网络设置界面
* 1.自定义广播类
* 2.完成注册
* 3.编写网络判断的工具类
* 4.使用网络判断的工具类,判断当前用户手机的网络情况
* 5.无网络时,跳转网络设置界面
*/
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//注册广播
MyRecever recever = new MyRecever();
IntentFilter filter = new IntentFilter();
filter.addAction("com.bawei.net");
registerReceiver(recever, filter);
//使用自己编写的工具类,判断网络是否连接
boolean available = NetWorkUtils.isNetWorkAvailable(this);
if (available) {
Toast.makeText(MainActivity.this, "网络连接成功", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(MainActivity.this, "网络连接失败", Toast.LENGTH_SHORT).show();
}
//使用自己编写的工具类,判断是否是wifi
boolean wifi = NetWorkUtils.isWifi(this);
if (wifi) {
Toast.makeText(MainActivity.this, "wifi网络连接成功", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(MainActivity.this, "wifi网络连接失败", Toast.LENGTH_SHORT).show();
}
//使用自己编写的工具类,判断是否是手机流量
boolean mobile = NetWorkUtils.isMobile(this);
//有网做对应的操作
if (mobile) {
Toast.makeText(MainActivity.this, "手机流量网络连接成功", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(MainActivity.this, "手机流量网络连接失败", Toast.LENGTH_SHORT).show();
//无网络时,跳转网络设置界面
Intent intent = new Intent("com.bawei.net");
intent.putExtra("net", "亲,断网了,应该去设置网络了");
sendBroadcast(intent);
Intent wifiSettingsIntent = new Intent("android.settings.WIFI_SETTINGS");
startActivity(wifiSettingsIntent);
}
}
private class MyRecever extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if(action.equals("com.bawei.net")){
String net = intent.getStringExtra("net");
Toast.makeText(MainActivity.this, "接收广播成功:"+net, Toast.LENGTH_SHORT).show();
}
}
}
}
</pre><br>
<br>
<pre></pre>
<br>
(切记注册权限)
<uses-permissionandroid:name="android.permission.INTERNET"/>
1.自定义广播类
2.完成注册
//注册广播 MyRecever recever = new MyRecever(); IntentFilter filter = new IntentFilter(); filter.addAction("com.bawei.net"); registerReceiver(recever, filter); |
private class MyRecever extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { String action = intent.getAction(); if(action.equals("com.bawei.net")){ String net = intent.getStringExtra("net"); Toast.makeText(MainActivity.this, "接收广播成功:"+net, Toast.LENGTH_SHORT).show(); } } } |
(1).判断手机网络是否连接
//判断网络是否连接 public static boolean isNetWorkAvailable(Context context) { //网络连接管理器 ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); //网络信息 NetworkInfo info = connectivityManager.getActiveNetworkInfo(); if (info != null) { return true; } return false; } |
//判断是否是wifi public static boolean isWifi(Context context) { //网络连接管理器 ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); //网络信息 NetworkInfo info = connectivityManager.getActiveNetworkInfo(); if (info != null && info.getType() == connectivityManager.TYPE_WIFI) { return true; } return false; } |
//判断是否是手机流量 public static boolean isMobile(Context context) { //网络连接管理器 ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); //网络信息 NetworkInfo info = connectivityManager.getActiveNetworkInfo(); if (info != null && info.getTyp e316 e() == connectivityManager.TYPE_MOBILE) { return true; } return false; } |
(1).
//使用自己编写的工具类,判断网络是否连接 boolean available = NetWorkUtils.isNetWorkAvailable(this); if (available) { Toast.makeText(MainActivity.this, "网络连接成功", Toast.LENGTH_SHORT).show(); } else { Toast.makeText(MainActivity.this, "网络连接失败", Toast.LENGTH_SHORT).show(); } |
//使用自己编写的工具类,判断是否是wifi boolean wifi = NetWorkUtils.isWifi(this); if (wifi) { Toast.makeText(MainActivity.this, "wifi网络连接成功", Toast.LENGTH_SHORT).show(); } else { Toast.makeText(MainActivity.this, "wifi网络连接失败", Toast.LENGTH_SHORT).show(); } |
//使用自己编写的工具类,判断是否是手机流量 boolean mobile = NetWorkUtils.isMobile(this); //有网做对应的操作 if (mobile) { Toast.makeText(MainActivity.this, "手机流量网络连接成功", Toast.LENGTH_SHORT).show(); } else { Toast.makeText(MainActivity.this, "手机流量网络连接失败", Toast.LENGTH_SHORT).show(); } } |
//无网络时,跳转网络设置界面 Intent intent = new Intent("com.bawei.net"); intent.putExtra("net", "亲,断网了,应该去设置网络了"); sendBroadcast(intent); Intent wifiSettingsIntent = new Intent("android.settings.WIFI_SETTINGS"); startActivity(wifiSettingsIntent); |
一:网络判断工具类:
[java]
view plain
copy
/**
* 网络判断的工具类
*/
public class NetWorkUtils {
//判断网络是否连接
public static boolean isNetWorkAvailable(Context context) {
//网络连接管理器
ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
//网络信息
NetworkInfo info = connectivityManager.getActiveNetworkInfo();
if (info != null) {
return true;
}
return false;
}
//判断是否是wifi
public static boolean isWifi(Context context) {
//网络连接管理器
ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
//网络信息
NetworkInfo info = connectivityManager.getActiveNetworkInfo();
if (info != null && info.getType() == connectivityManager.TYPE_WIFI) {
return true;
}
return false;
}
//判断是否是手机流量
public static boolean isMobile(Context context) {
//网络连接管理器
ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
//网络信息
NetworkInfo info = connectivityManager.getActiveNetworkInfo();
if (info != null && info.getType() == connectivityManager.TYPE_MOBILE) {
return true;
}
return false;
}
}
[java]
view plain
copy
二:ManActivity
[java]
view plain
copy
<pre name="code" class="java">/**
* 掌握网络是否连接以及网络类型的判断,掌握无网络情况下,跳转设置网络设置界面
* 1.自定义广播类
* 2.完成注册
* 3.编写网络判断的工具类
* 4.使用网络判断的工具类,判断当前用户手机的网络情况
* 5.无网络时,跳转网络设置界面
*/
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//注册广播
MyRecever recever = new MyRecever();
IntentFilter filter = new IntentFilter();
filter.addAction("com.bawei.net");
registerReceiver(recever, filter);
//使用自己编写的工具类,判断网络是否连接
boolean available = NetWorkUtils.isNetWorkAvailable(this);
if (available) {
Toast.makeText(MainActivity.this, "网络连接成功", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(MainActivity.this, "网络连接失败", Toast.LENGTH_SHORT).show();
}
//使用自己编写的工具类,判断是否是wifi
boolean wifi = NetWorkUtils.isWifi(this);
if (wifi) {
Toast.makeText(MainActivity.this, "wifi网络连接成功", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(MainActivity.this, "wifi网络连接失败", Toast.LENGTH_SHORT).show();
}
//使用自己编写的工具类,判断是否是手机流量
boolean mobile = NetWorkUtils.isMobile(this);
//有网做对应的操作
if (mobile) {
Toast.makeText(MainActivity.this, "手机流量网络连接成功", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(MainActivity.this, "手机流量网络连接失败", Toast.LENGTH_SHORT).show();
//无网络时,跳转网络设置界面
Intent intent = new Intent("com.bawei.net");
intent.putExtra("net", "亲,断网了,应该去设置网络了");
sendBroadcast(intent);
Intent wifiSettingsIntent = new Intent("android.settings.WIFI_SETTINGS");
startActivity(wifiSettingsIntent);
}
}
private class MyRecever extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if(action.equals("com.bawei.net")){
String net = intent.getStringExtra("net");
Toast.makeText(MainActivity.this, "接收广播成功:"+net, Toast.LENGTH_SHORT).show();
}
}
}
}
</pre><br>
<br>
<pre></pre>
<br>
相关文章推荐
- 手机网络判断及进入设置
- 判断-网络链接-进入-手机设置
- 网络判断——没网点击进入设置(ConnectivityManager)
- 进入一个APP时对手机网络判断(使用广播和不使用广播)
- Android判断网络连接状态并进入网络设置页面
- Android(判断wifi是否开启,手机屏幕状态,sdcard是否被拔出,设置全屏)
- android手机网络状态监听,判断手机连接的是移动网络还是wifi等
- Fiddler将笔记本设置代理,抓取手机网络请求包
- 判断android手机当前是否接入网络
- ios检测网络,无网络提示并直接进入设置页面
- iOS开发 - 如何获取设备的总容量和可用容量 网络运营商 3g/wifi判断 手机型号
- Android 判断网络状态,并且在没有网络的时候,打开网络设置对话框
- 判断手机网络是否连接
- 判断是否有网络,如果没有网络,弹出对话框,然后可以跳转到设置页面
- 判断网络状态,成功吐司网络可用,没有网络弹框设置网络,判断网络可用请求数据,接口回调
- 判断网络状态 | 跳转至设置Activity
- iOS 判断手机的网络状况
- AFNetworking判断当前手机的网络状态
- 网络判断跳转设置界面
- Android开发_判断手机网络是否可用