您的位置:首页 > 理论基础 > 计算机网络

手机网络判断及进入设置

2017-09-04 22:31 381 查看
掌握网络是否连接以及网络类型的判断,掌握无网络情况下,跳转设置网络设置界面
(切记注册权限)
<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();

}

}

}
3.编写网络判断的工具类
(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;

}
(2).判断是否是wifi

//判断是否是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;

}
(3).判断是否是手机流量

//判断是否是手机流量

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;

}
4.使用网络判断的工具类,判断当前用户手机的网络情况
(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(); }
(2).

//使用自己编写的工具类,判断是否是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(); }
(3).

//使用自己编写的工具类,判断是否是手机流量

boolean mobile = NetWorkUtils.isMobile(this);

//有网做对应的操作

if (mobile) {

Toast.makeText(MainActivity.this, "手机流量网络连接成功", Toast.LENGTH_SHORT).show(); } else {

Toast.makeText(MainActivity.this, "手机流量网络连接失败", Toast.LENGTH_SHORT).show();

}

}
5.无网络时,跳转网络设置界面

//无网络时,跳转网络设置界面

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> 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: