android 监控任何数据变化

来源:互联网 发布:临沂知豆租赁电话 编辑:程序博客网 时间:2024/06/03 20:23
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);                        }*/                }        }}