Mainactivity as follows:
Package Cc.testnetworkchange;import Android.os.bundle;import android.app.activity;/** * Demo Description: * Change of network connection using broadcast monitoring device * It is also possible to monitor if the device has been disconnected from the network */public class Mainactivity extends Activity {@Overrideprotected void OnCreate (Bundle Savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.main);}}
Networkchangebroadcastreceiver as follows:
Package Cc.testnetworkchange;import Android.content.broadcastreceiver;import Android.content.context;import Android.content.intent;import Android.net.connectivitymanager;import Android.net.networkinfo;import Android.net.networkinfo.state;public class Networkchangebroadcastreceiver extends Broadcastreceiver {@ overridepublic void OnReceive (context context, Intent Intent) {Connectivitymanager connectivitymanager= ( Connectivitymanager) Context.getsystemservice (Context.connectivity_service), if (connectivitymanager!=null) { Networkinfo [] Networkinfos=connectivitymanager.getallnetworkinfo (); for (int i = 0; i < networkinfos.length; i++) {Sta Te State=networkinfos[i].getstate (); if (networkinfo.state.connected==state) {System.out.println ("------------> Network is OK "); return;}} No return is performed, indicating that there is currently no network connection System.out.println ("------------> Network is validate"); Intent.setclass (Context, Networkerroractivity.class); Intent.setflags (intent.flag_activity_clear_top| Intent.flag_activity_new_task); Context.startactivity (intent);}}
networkerroractivity as follows:
Package Cc.testnetworkchange;import Android.os.bundle;import Android.app.activity;public class NetWorkErrorActivity Extends Activity {@Overrideprotected void onCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.networkerror);}}
Main.xml as follows:
<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 " > <textview android:layout_width= "wrap_content" android:layout_height= "Wrap_content " Android:layout_centerinparent= "true" android:text= "use broadcast monitoring device network connection change situation" /></relativelayout>
Androidmanifest.xml as follows:
<?xml version= "1.0" encoding= "Utf-8"? ><manifest xmlns:android= "http://schemas.android.com/apk/res/ Android "package=" Cc.testnetworkchange "android:versioncode=" 1 "android:versionname=" 1.0 "> <uses-sdk android:minsdkversion= "8" android:targetsdkversion= "8"/> <uses-permission android:name= "Android Oid.permission.INTERNET "/> <uses-permission android:name=" Android.permission.ACCESS_NETWORK_STATE "/> <application android:allowbackup= "true" android:icon= "@drawable/ic_launcher" android:label= "@strin G/app_name "Android:theme=" @style/apptheme "> <activity android:name=" Cc.testnetworkchange. 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> <!--registration Displays the wrong activity--> <activity android:name= "Cc.testnetworkchange.Net Workerroractivity "></activity> <!--Register Broadcast--<receiver android:name=" Cc.testnetwor Kchange. Networkchangebroadcastreceiver "> <intent-filter> <action android:name=" android.net. Conn. Connectivity_change "/> </intent-filter> </receiver> </application></manifes T>