android系统中的广播机制有没有系统广播,分别都是那些?android 广播

解决方案 »

  1.   

    楼上的朋友,android系统中有很多自带的广播机制,当然android平台上还可以自定义。比如我们常用的一个网络信号监控问题,当你断网时,你可以自定义service去反复监测,不过这就不如系统广播来的快,并且还有保障。贴点代码给你。
        public class extends BroadcastReceiver {  
      
        @Override  
        public void onReceive(Context context, Intent intent) {  
            State wifiState = null;  
            State mobileState = null;  
            ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);  
            wifiState = cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI).getState();  
            mobileState = cm.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).getState();  
            if (wifiState != null && mobileState != null  
                    && State.CONNECTED != wifiState  
                    && State.CONNECTED == mobileState) {  
                // 手机网络连接成功  
            } else if (wifiState != null && mobileState != null  
                    && State.CONNECTED != wifiState  
                    && State.CONNECTED != mobileState) {  
                // 手机没有任何的网络  
            } else if (wifiState != null && State.CONNECTED == wifiState) {  
                // 无线网络连接成功  
            }  
      
        }