1.判断wifi、数据
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (getNetype(this)== -1){
//初始化数据
Toast.makeText(this,"网络未连接,请连接网络",Toast.LENGTH_SHORT).show();
}else {
if (getNetype(this)==1){
Toast.makeText(this,"连接WIFI网络",Toast.LENGTH_SHORT).show();
}else {
Toast.makeText(this,"连接移动网络",Toast.LENGTH_SHORT).show();
}
}
}
/**
* 返回值 -1:没有网络 1:WIFI网络 3:net网络
*/
public static int getNetype(Context context) {
int netType = -1;
ConnectivityManager connMgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();
if (networkInfo == null) {
return netType;
}
int nType = networkInfo.getType();
if (nType == ConnectivityManager.TYPE_MOBILE) {
netType = 2;
} else if (nType == ConnectivityManager.TYPE_WIFI) {
netType = 1;
}
return netType;
}
}
2.简单判断
1.工具类
//判断网络
public static boolean newwork(Context context)
{
ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetworkInfo = manager.getActiveNetworkInfo();
return activeNetworkInfo!=null && activeNetworkInfo.isAvailable();
}
2.主页面
//首页
if(!network.newwork(MainActivity.this))
{
Toast.makeText(this, "无网络", Toast.LENGTH_SHORT).show();
ss.setVisibility(View.VISIBLE);
//动画
objectAnimator = ObjectAnimator.ofFloat(ss, "rotation", 360f);
objectAnimator.setDuration(1000);
objectAnimator.start();
}