一、布局页面
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.example.testnet.MainActivity" > <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/hello_world" /> <Button android:id="@+id/tstNetwrkBtn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/textView1" android:layout_centerHorizontal="true" android:layout_marginTop="10dp" android:text="@string/btnShow" /> </RelativeLayout>
二、编写Activity类
package com.example.testnet; import android.app.Activity; import android.content.Context; import android.net.ConnectivityManager; import android.net.NetworkInfo; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.TextView; public class MainActivity extends Activity { private Button tstNetwrkBtn; private TextView tv1; private ConnectivityManager cm; private Context context; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //获取button控件 tstNetwrkBtn = (Button) findViewById(R.id.tstNetwrkBtn); context=this; //给button控件绑定单击事件 tstNetwrkBtn.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub //获取Activity Activity c =(Activity)context; //获取TextView控件 tv1 = (TextView)c.findViewById(R.id.textView1); //定义两个属性保存字符串和颜色 String netStatus; int color; //获取网络连接对象 cm=(ConnectivityManager) c.getSystemService(c.CONNECTIVITY_SERVICE); //获取网络连接状态 NetworkInfo netInfo = cm.getActiveNetworkInfo(); if (netInfo==null){ //网络连接失败时赋值文本内容和背景颜色 color = c.getResources().getColor(R.color.red); netStatus = c.getResources().getString(R.string.netStatus0); } else{ //网络连接成功时赋值文本内容和背景颜色 netStatus = c.getResources().getString(R.string.netStatus1); color = c.getResources().getColor(R.color.green); } //设置文本和背景颜色 tv1.setText( netStatus ); tv1.setBackgroundColor(color); } }); } }
三、string文件和colors文件
<?xml version="1.0" encoding="utf-8"?> <resources> <string name="app_name">testNet</string> <string name="hello_world">网络测试</string> <string name="btnShow">test Network</string> <string name="netStatus0">网络不正常</string> <string name="netStatus1">网络正常</string> <string name="action_settings">Settings</string> </resources>
<?xml version="1.0" encoding="utf-8"?> <resources > <color name="red">#ff0000</color> <color name="green">#00ff00</color> </resources>
四、权限(AndroidManifest.xml)
<!-- 访问网络权限 --> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
五、调试
网络正常时:
没网络时: