android 监控任何数据变化

public class DeviceStateReceiver extends BroadcastReceiver {

        private static final String ACTION_CONNECTIVITY_CHANGED = "android.net.conn.CONNECTIVITY_CHANGE";
        private static final String ACTION_DATA_STATE_CHANGED = "android.intent.action.ANY_DATA_STATE";
        private static final String THIS_FILE = "Device State";
        
        @Override
        public void onReceive(Context context, Intent intent) {
                
                PreferencesWrapper prefWrapper = new PreferencesWrapper(context);
                String intentAction = intent.getAction();
                
                //
                // ACTION_DATA_STATE_CHANGED
                // Data state change is used to detect changes in the mobile
                // network such as a switch of network type (GPRS, EDGE, 3G) 
                // which are not detected by the Connectivity changed broadcast.
                //
                if (intentAction.equals(ACTION_DATA_STATE_CHANGED)) {
                        Log.d(THIS_FILE, ">>> Data state change detected");
                        
                        if (prefWrapper.isValidConnectionForOutgoing()) {
                                Log.d(THIS_FILE, "Data state indicates connectivity now available");
                                Intent sip_service_intent = new Intent(context, SipService.class);
                                context.startService(sip_service_intent);
                        }
                        Log.d(THIS_FILE, "<<< Data state change detected");
                        
                }
                //
                // ACTION_CONNECTIVITY_CHANGED
                // Connectivity change is used to detect changes in the overall
                // data network status as well as a switch between wifi and mobile
                // networks.
                //
                else if (intentAction.equals(ACTION_CONNECTIVITY_CHANGED)) {
                        Log.d(THIS_FILE, ">>> Connectivity change detected");
                        
                        if (prefWrapper.isValidConnectionForOutgoing()) {
                                Log.d(THIS_FILE, "Connectivity now available");
                                Intent sip_service_intent = new Intent(context, SipService.class);
                                context.startService(sip_service_intent);
                        }
                        Log.d(THIS_FILE, "<<< Connectivity change detected");
                        
                        /* Perhaps use the possible upcomming network information
                         * when performing the connectivity check instead of the 
                         * current information, but it does not seem to be necessary.

                        // New connection info
                        Bundle extras = intent.getExtras();
                        NetworkInfo ni = (NetworkInfo) extras.get(ConnectivityManager.EXTRA_NETWORK_INFO);
                        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
                        
                        // Other case (ie update IP etc) are handled directly inside the
                        // service if started
                        if (PreferencesWrapper.isValidConnectionFor(ni, prefs, "out")) {
                                Log.d(THIS_FILE, "Connectivity now available");
                                Intent sip_service_intent = new Intent(context, SipService.class);
                                context.startService(sip_service_intent);
                        }*/
                }
        }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值