一、打开Eclipse并新建一个Android app项目
如图:
在布局文件中添加一个Button控件
并配置ID和I18N
如图:
布局文件代码如下:
<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.networktest.MainActivity" > <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/tv1" /> <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/textView1" android:layout_centerHorizontal="true" android:layout_marginTop="133dp" android:text="@string/but1" /> </RelativeLayout>
I18N资源文件代码如下:
<?xml version="1.0" encoding="utf-8"?> <resources> <string name="app_name">NetworkTest</string> <string name="tv1"></string> <string name="action_settings">Settings</string> <string name="text1">网络异常</string> <string name="text2">网络正常</string> <string name="but1">重新测试</string> <string name="toast1">获取权限失败,请查看权限分配情况!</string> <string name="toast2">网络连接异常,请检查网络连接情况!</string> </resources>
新建一个Test类
如图:
在Test类中编写代码
代码如下:
package com.example.networktest; import android.app.Activity; import android.content.Context; import android.net.ConnectivityManager; import android.net.NetworkInfo; import android.view.View; import android.view.View.OnClickListener; import android.widget.TextView; import android.widget.Toast; public class Test implements OnClickListener { private TextView tv; private ConnectivityManager cm; private Context context; private NetworkInfo netInfo; public Test(Context context){ this.context=context; } @Override public void onClick(View v) { String text ; int color; Activity c = (Activity)context; tv = (TextView)c.findViewById(R.id.textView1); try { cm = (ConnectivityManager) c.getSystemService(Context.CONNECTIVITY_SERVICE); netInfo = cm.getActiveNetworkInfo(); } catch (Exception e) { Toast.makeText( context,c.getResources().getString(R.string.toast1),Toast.LENGTH_LONG ).show(); } if(netInfo==null){ //网络异常 text = c.getResources().getString(R.string.text1); color = c.getResources().getColor(R.color.red); Toast.makeText( context,c.getResources().getString(R.string.toast2),Toast.LENGTH_LONG ).show(); }else{ //网络正常 text = c.getResources().getString(R.string.text2); color = c.getResources().getColor(R.color.green); } tv.setText(text); tv.setBackgroundColor(color); } }
然后在MainActivity.java类中写入调用代码
代码如下:
package com.example.networktest; import android.app.Activity; import android.os.Bundle; import android.widget.Button; public class MainActivity extends Activity { private Button but; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //程序运行自动检查网络 setContentView(R.layout.activity_main); new Test(this).onClick(null); //手动刷新 but = (Button)findViewById(R.id.button1); but.setOnClickListener(new Test(this)); } }
然后在Android配置文件AndroidManifest.xml中配置相关权限
代码如图:
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.networktest" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="17" android:targetSdkVersion="19" /> <!-- 判断网络连接状态权限 --> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/> <!-- 访问Internet权限 --> <uses-permission android:name="android.permission.INTERNET"/> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name=".MainActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
运行结果如图: