利用广播BroadCast监听网络的变化
package com.app.test02; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.net.ConnectivityManager; import android.net.NetworkInfo.State; import android.widget.Toast; /** * Android 利用广播BroadCast监听网络的变化 * @author 402-9 */ public class BroadCastDemo extends BroadcastReceiver{ State wifiState = null; State mobileState = null; @Override public void onReceive(Context context, Intent intent) { // TODO Auto-generated method stub //获取手机的连接服务管理器,这里是连接管理器类 ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); wifiState = cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI).getState(); mobileState = cm.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).getState(); Intent intent2 = new Intent(context , BroadCastActivity2_SMS.class); if (wifiState != null && mobileState != null && State.CONNECTED != wifiState && State.CONNECTED == mobileState) { context.startService(intent2); Toast.makeText(context, "手机网络连接成功!", Toast.LENGTH_SHORT).show(); } else if (wifiState != null && mobileState != null && State.CONNECTED == wifiState && State.CONNECTED != mobileState) { context.startService(intent2); Toast.makeText(context, "无线网络连接成功!", Toast.LENGTH_SHORT).show(); } else if (wifiState != null && mobileState != null && State.CONNECTED != wifiState && State.CONNECTED != mobileState) { context.startService(intent2); Toast.makeText(context, "手机没有任何网络...", Toast.LENGTH_SHORT).show(); } } }
<receiver android:name=".BroadCastDemo"> <intent-filter > <action android:name="android.net.conn.CONNECTIVITY_CHANGE"/> </intent-filter> </receiver>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
\
补充:移动开发 , Android ,