网络判断状态

来源:互联网 发布:淘宝运营怎么学 编辑:程序博客网 时间:2024/05/13 11:04
public class MainActivity extends Activity
{


private CheckBox mBox;
private Editor edit;


@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);
SharedPreferences sp = getSharedPreferences("isWifi", 0);
edit = sp.edit();
mBox = (CheckBox) findViewById(R.id.checkBox1);
mBox.setOnCheckedChangeListener(new OnCheckedChangeListener()
{

@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
{
edit.putBoolean("wifi", isChecked);
edit.commit();
}
});

boolean isAuto = sp.getBoolean("wifi", true);
if (isAuto)
{
if (ConnectionUtils.isWIFI(this))
{
Toast.makeText(this, "WIFI连接,自动下载中。。。", Toast.LENGTH_SHORT).show();
}else{

Toast.makeText(this, "非WIFI连接,不下载", Toast.LENGTH_SHORT).show();
}
}else{

Toast.makeText(this, "未选中,不自动下载", Toast.LENGTH_SHORT).show();
}
}


public void contection(View v){
if (ConnectionUtils.isConnected(this))
{
if (ConnectionUtils.isWIFI(this))
{

Toast.makeText(this, "WIFI连接", Toast.LENGTH_SHORT).show();;
}
else if (ConnectionUtils.isMobile(this))
{
Toast.makeText(this, "移动网络连接", Toast.LENGTH_SHORT).show();;
}

}
else{
Toast.makeText(this, "网络未连接", Toast.LENGTH_SHORT).show();;
}
}
}
0 0
原创粉丝点击