Android uses broadcast monitoring device network connection (off-grid) change situation

Source: Internet
Author: User

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 "/> &LT;/INTENT-FILTER&GT </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>


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.