private LinearLayout linear1,linear2;
private Button btn1,btn2;
linear1=(LinearLayout)findViewById(R.id.linearLayout1);
//activity启动时隐藏
linear1.setVisibility(View.GONE);
linear2=(LinearLayout)findViewById(R.id.linearLayout2);
linear2.setVisibility(View.GONE);
btn1=(Button)findViewById(R.id.btn_login);
btn2=(Button)findViewById(R.id.btn_exit);
btn1.setOnClickListener(new btn1Onclick());
btn2.setOnClickListener(new btn2Onclick());
------------------------------------------------------------------>>>>>>>>
//打开网络连接设置
class btn1Onclick implements OnClickListener{
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
NetWorkStatus();
}
}
//退出
class btn2Onclick implements OnClickListener{
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
System.exit(0);
}
}
public void Connect(){
Context contexts = MainActivity.this.getApplicationContext();
// 得到本Activity
if (!isConnect(contexts)) {
// 如果网络不好使给出提示
//isConnect是自定义的检测网络状态的函数
Toast.makeText(MainActivity.this, "温馨提示:请检查网络连接!",
Toast.LENGTH_SHORT).show();
//网络没有连接时显示布局按钮
linear1.setVisibility(View.VISIBLE);
linear2.setVisibility(View.VISIBLE);
return;
}else{
System.out.println("网络已经连接上啦!!");
}
}
public static boolean isConnect(Context context) {
// 获取手机所有连接管理对象(包括对 Wi-Fi,net等连接的管理)
ConnectivityManager connectivity = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
if (connectivity != null) {
// 获取网络连接管理的对象
NetworkInfo info = connectivity.getActiveNetworkInfo();
if (info != null) {
// 判断当前网络是否已经连接
if (info.getState() == NetworkInfo.State.CONNECTED) {
return true;
}
}
}
return false;
}
//打开网络设置
private void NetWorkStatus() {
ConnectivityManager connectivity = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
connectivity.getActiveNetworkInfo();
Builder b = new AlertDialog.Builder(this).setTitle("没有可用的网络").setMessage("是否对网络进行设置?");
b.setPositiveButton("是", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
Intent mIntent = new Intent("/");
ComponentName comp = new ComponentName( "com.android.settings", "com.android.settings.WirelessSettings");
mIntent.setComponent(comp);
mIntent.setAction("android.intent.action.VIEW");
startActivityForResult(mIntent,0); // 如果在设置完成后需要再次进行操作,可以重写操作代码,在这里不再重写
System.exit(0);
}
}
).setNeutralButton("否", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
dialog.cancel();
System.exit(0);
}
}
).show();
}
}
---------------------------------------------------------->>>>>>>>>>>>>>>>>>>>
布局文件
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/h"
android:orientation="vertical" >
<LinearLayout
android:id="@+id/linearLayout1"
android:layout_width="fill_parent"
android:layout_height="50dip"
android:layout_marginTop="250dip"
>
<Button
android:id="@+id/btn_login"
android:layout_width="fill_parent"
android:layout_height="50dip"
android:background="#555555"
android:text="@string/open" >
</Button>
</LinearLayout>
<LinearLayout
android:id="@+id/linearLayout2"
android:layout_width="fill_parent"
android:layout_height="50dip"
>
<Button
android:id="@+id/btn_exit"
android:layout_width="fill_parent"
android:layout_height="50dip"
android:layout_marginTop="1dip"
android:background="#555555"
android:text="@string/exit" >
</Button>
</LinearLayout>
</LinearLayout>
》》》》》》》》》》》》》》》》》》》》》》上图《《《《《《《《《《《《《《《《《《《《《《《
QQ群:230901717
转载:http://blog.sina.com.cn/s/blog_8be86ad301010xb6.html