Example usage for android.telephony PhoneStateListener LISTEN_DATA_CONNECTION_STATE

List of usage examples for android.telephony PhoneStateListener LISTEN_DATA_CONNECTION_STATE

Introduction

In this page you can find the example usage for android.telephony PhoneStateListener LISTEN_DATA_CONNECTION_STATE.

Prototype

int LISTEN_DATA_CONNECTION_STATE

To view the source code for android.telephony PhoneStateListener LISTEN_DATA_CONNECTION_STATE.

Click Source Link

Document

Listen for changes to the data connection state (cellular).

Usage

From source file:fr.inria.ucn.listeners.MyPhoneStateListener.java

/**
 * /*www  .  j  a  v  a  2  s .com*/
 * @param c
 */
public void enable(Context c) {
    TelephonyManager tm = (TelephonyManager) c.getSystemService(Context.TELEPHONY_SERVICE);
    if (tm.getPhoneType() != TelephonyManager.PHONE_TYPE_NONE) {
        tm.listen(this,
                PhoneStateListener.LISTEN_CELL_LOCATION | PhoneStateListener.LISTEN_DATA_CONNECTION_STATE);
        this.c = c;
    } // else no telephony
}

From source file:com.karpenstein.signalmon.NetServerService.java

@Override
public void onCreate() {
    super.onCreate();
    try {/*from   ww w.  j  a  v  a 2  s  .  c  o  m*/
        jsonState = new JSONObject();
        jsonState.put("dataActivity", TelephonyManager.DATA_ACTIVITY_NONE);
    } catch (JSONException ex) {
        Log.d("NetServerService", "Failed to put data activity in the JSONObject");
    }

    // Get the telephony manager
    telephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
    if (telephonyManager == null)
        Log.d("NetServerService", "TelephonyManager was null.");
    Log.d("NetServerService", "about to create PhoneStateListener");
    // Create a new PhoneStateListener
    psListener = new PhoneStateListener() {
        @Override
        public void onDataActivity(int direction) {
            Log.d("NetServerService", "received onDataActivity message");
            try {
                jsonState.put("dataActivity", direction);
            } catch (JSONException ex) {
            }
            notifyListeners();
        }

        @Override
        public void onSignalStrengthsChanged(SignalStrength signalStrength) {
            Log.d("NetServerService", "received onSignalStrength message");
            try {
                jsonState.put("cdmaDbm", signalStrength.getCdmaDbm());
                jsonState.put("cdmaEcio", signalStrength.getCdmaEcio());
                jsonState.put("evdoDbm", signalStrength.getEvdoDbm());
                jsonState.put("evdoEcio", signalStrength.getEvdoEcio());
                jsonState.put("evdoSnr", signalStrength.getEvdoSnr());
                jsonState.put("gsmBitErrorRate", signalStrength.getGsmBitErrorRate());
                jsonState.put("gsmSignalStrength", signalStrength.getGsmSignalStrength());
                jsonState.put("isGsm", signalStrength.isGsm());
            } catch (JSONException ex) {
            }
            notifyListeners();
        }

        @Override
        public void onDataConnectionStateChanged(int state, int networkType) {
            Log.d("NetServerService", "received onDataConnectionStateChanged message");
            try {
                jsonState.put("connState", state);
                jsonState.put("netType", networkType);
            } catch (JSONException ex) {
            }
            notifyListeners();
        }
    };

    Log.d("NetServerService", "about to call telephonyManager.listen");
    // Register the listener with the telephony manager
    telephonyManager.listen(psListener, PhoneStateListener.LISTEN_DATA_CONNECTION_STATE
            | PhoneStateListener.LISTEN_SIGNAL_STRENGTHS | PhoneStateListener.LISTEN_DATA_ACTIVITY);
    Log.d("NetServerService", "done calling telephonyManager.listen -- exiting onCreate");
}

From source file:com.secupwn.aimsicd.ui.activities.MapViewerOsmDroid.java

/**
 * Called when the activity is first created.
 *//*from w  w  w  .  ja  va  2 s.  c o m*/
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    log.info("Starting MapViewer");

    setUpMapIfNeeded();

    mDbHelper = new RealmHelper(this);
    tm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);

    // Bind to LocalService
    Intent intent = new Intent(this, AimsicdService.class);
    bindService(intent, mConnection, Context.BIND_AUTO_CREATE);

    TelephonyManager tm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
    tm.listen(mPhoneStateListener,
            PhoneStateListener.LISTEN_CELL_LOCATION | PhoneStateListener.LISTEN_DATA_CONNECTION_STATE);
}

From source file:com.SecUpwN.AIMSICD.fragments.MapFragment.java

@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    log.info("Starting MapViewer");

    setUpMapIfNeeded();//w  ww .ja  va 2s  .c  o  m

    mDbHelper = new AIMSICDDbAdapter(getActivity());
    tm = (TelephonyManager) getActivity().getSystemService(Context.TELEPHONY_SERVICE);

    // Bind to LocalService
    Intent intent = new Intent(getActivity(), AimsicdService.class);
    getActivity().bindService(intent, mConnection, Context.BIND_AUTO_CREATE);

    TelephonyManager tm = (TelephonyManager) getActivity().getSystemService(Context.TELEPHONY_SERVICE);
    tm.listen(mPhoneStateListener,
            PhoneStateListener.LISTEN_CELL_LOCATION | PhoneStateListener.LISTEN_DATA_CONNECTION_STATE);
}

From source file:com.SecUpwN.AIMSICD.activities.MapViewerOsmDroid.java

/**
 * Called when the activity is first created.
 *//*from ww  w .  j a  va 2s. com*/
@Override
public void onCreate(Bundle savedInstanceState) {
    Log.i(TAG, "Starting MapViewer");
    super.onCreate(savedInstanceState);
    mContext = this;

    setContentView(R.layout.map);
    setUpMapIfNeeded();

    mDbHelper = new AIMSICDDbAdapter(mContext);

    // Bind to LocalService
    Intent intent = new Intent(mContext, AimsicdService.class);
    mContext.bindService(intent, mConnection, Context.BIND_AUTO_CREATE);

    TelephonyManager tm = (TelephonyManager) mContext.getSystemService(Context.TELEPHONY_SERVICE);
    tm.listen(mPhoneStateListener,
            PhoneStateListener.LISTEN_CELL_LOCATION | PhoneStateListener.LISTEN_DATA_CONNECTION_STATE);
}

From source file:com.secupwn.aimsicd.ui.fragments.MapFragment.java

@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    log.info("Starting MapViewer");

    setUpMapIfNeeded();//  w w  w  .j  av  a  2s .c o m

    mDbHelper = new RealmHelper(getActivity());
    tm = (TelephonyManager) getActivity().getSystemService(Context.TELEPHONY_SERVICE);

    // Bind to LocalService
    Intent intent = new Intent(getActivity(), AimsicdService.class);
    getActivity().bindService(intent, mConnection, Context.BIND_AUTO_CREATE);

    TelephonyManager tm = (TelephonyManager) getActivity().getSystemService(Context.TELEPHONY_SERVICE);
    tm.listen(mPhoneStateListener,
            PhoneStateListener.LISTEN_CELL_LOCATION | PhoneStateListener.LISTEN_DATA_CONNECTION_STATE);
}

From source file:com.karpenstein.signalmon.SignalMonitorActivity.java

/** Called when the activity is first created. */
@Override/*www .j av a 2  s .co  m*/
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    // Get the UI
    textOut = (TextView) findViewById(R.id.textOut);

    // Get the telephony manager
    telephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);

    // Create a new PhoneStateListener
    listener = new PhoneStateListener() {
        @Override
        public void onDataActivity(int direction) {
            String dirString = "N/A";
            switch (direction) {
            case TelephonyManager.DATA_ACTIVITY_NONE:
                dirString = "DATA_ACTIVITY_NONE";
                break;
            case TelephonyManager.DATA_ACTIVITY_IN:
                dirString = "DATA_ACTIVITY_IN";
                break;
            case TelephonyManager.DATA_ACTIVITY_OUT:
                dirString = "DATA_ACTIVITY_OUT";
                break;
            case TelephonyManager.DATA_ACTIVITY_INOUT:
                dirString = "DATA_ACTIVITY_INOUT";
                break;
            case TelephonyManager.DATA_ACTIVITY_DORMANT:
                dirString = "DATA_ACTIVITY_DORMANT";
                break;
            }
            textOut.append(dirString + "\n");
        }

        @Override
        public void onSignalStrengthsChanged(SignalStrength signalStrength) {
            textOut.append(signalStrength.toString() + "\n");
        }

        @Override
        public void onDataConnectionStateChanged(int state, int networkType) {
            String stateString = "N/A";
            String netTypString = "N/A";
            switch (state) {
            case TelephonyManager.DATA_CONNECTED:
                stateString = "DATA_CONNECTED";
                break;
            case TelephonyManager.DATA_CONNECTING:
                stateString = "DATA_CONNECTING";
                break;
            case TelephonyManager.DATA_DISCONNECTED:
                stateString = "DATA_DISCONNECTED";
                break;
            case TelephonyManager.DATA_SUSPENDED:
                stateString = "DATA_SUSPENDED";
                break;
            }
            switch (networkType) {
            case TelephonyManager.NETWORK_TYPE_1xRTT:
                netTypString = "NETWORK_TYPE_1xRTT";
                break;
            case TelephonyManager.NETWORK_TYPE_CDMA:
                netTypString = "NETWORK_TYPE_CDMA";
                break;
            case TelephonyManager.NETWORK_TYPE_EDGE:
                netTypString = "NETWORK_TYPE_EDGE";
                break;
            case TelephonyManager.NETWORK_TYPE_EVDO_0:
                netTypString = "NETWORK_TYPE_EVDO_0";
                break;
            case TelephonyManager.NETWORK_TYPE_EVDO_A:
                netTypString = "NETWORK_TYPE_EVDO_A";
                break;
            case TelephonyManager.NETWORK_TYPE_GPRS:
                netTypString = "NETWORK_TYPE_GPRS";
                break;
            case TelephonyManager.NETWORK_TYPE_HSDPA:
                netTypString = "NETWORK_TYPE_HSDPA";
                break;
            case TelephonyManager.NETWORK_TYPE_HSPA:
                netTypString = "NETWORK_TYPE_HSPA";
                break;
            case TelephonyManager.NETWORK_TYPE_HSUPA:
                netTypString = "NETWORK_TYPE_HSUPA";
                break;
            case TelephonyManager.NETWORK_TYPE_IDEN:
                netTypString = "NETWORK_TYPE_IDE";
                break;
            case TelephonyManager.NETWORK_TYPE_UMTS:
                netTypString = "NETWORK_TYPE_UMTS";
                break;
            case TelephonyManager.NETWORK_TYPE_UNKNOWN:
                netTypString = "NETWORK_TYPE_UNKNOWN";
                break;
            }
            textOut.append(String.format("onDataConnectionStateChanged: %s; %s\n", stateString, netTypString));
        }
    };

    // Register the listener with the telephony manager
    telephonyManager.listen(listener, PhoneStateListener.LISTEN_DATA_CONNECTION_STATE
            | PhoneStateListener.LISTEN_SIGNAL_STRENGTHS | PhoneStateListener.LISTEN_DATA_ACTIVITY);

    // start the NetServerService
    startService(new Intent(this, NetServerService.class));
}

From source file:com.ultrafunk.network_info.service.NetworkStateService.java

private void mobileDataInit() {
    mobileDataStatusReceiver = new MobileDataStatusReceiver();
    mobileDataStateListener = new MobileDataStateListener(this);
    mobileDataSettingObserver = new MobileDataSettingObserver(this);

    IntentFilter intentFilter = new IntentFilter();
    intentFilter.addAction(Constants.ACTION_SERVICE_STATE_CHANGED);
    intentFilter.addAction(Constants.ACTION_DATA_CONNECTION_CHANGED);
    intentFilter.addAction(Constants.ACTION_DATA_STATE_CHANGED);
    intentFilter.addAction(Constants.ACTION_DATA_USAGE_UPDATE);
    localBroadcastManager.registerReceiver(mobileDataStatusReceiver, intentFilter);

    registerReceiver(mobileDataStatusReceiver, new IntentFilter(Intent.ACTION_SCREEN_ON));

    telephonyManager.listen(mobileDataStateListener,
            PhoneStateListener.LISTEN_DATA_CONNECTION_STATE | PhoneStateListener.LISTEN_SERVICE_STATE);
    getContentResolver().registerContentObserver(mobileDataSettingUri, false, mobileDataSettingObserver);
}

From source file:org.metawatch.manager.Monitors.java

public static void start(Context context, TelephonyManager telephonyManager) {
    // start weather updater

    if (Preferences.logging)
        Log.d(MetaWatch.TAG, "Monitors.start()");

    createBatteryLevelReciever(context);
    createWifiReceiver(context);// w w w. j av  a 2  s. co m

    if (Preferences.weatherGeolocation) {
        if (Preferences.logging)
            Log.d(MetaWatch.TAG, "Initialising Geolocation");

        try {
            locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
            locationProvider = LocationManager.NETWORK_PROVIDER;

            networkLocationListener = new NetworkLocationListener(context);

            locationManager.requestLocationUpdates(locationProvider, 30 * 60 * 1000, 500,
                    networkLocationListener);

            RefreshLocation();
        } catch (IllegalArgumentException e) {
            if (Preferences.logging)
                Log.d(MetaWatch.TAG, "Failed to initialise Geolocation " + e.getMessage());
        }
    } else {
        if (Preferences.logging)
            Log.d(MetaWatch.TAG, "Geolocation disabled");
    }

    CallStateListener phoneListener = new CallStateListener(context);

    telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
    int phoneEvents = PhoneStateListener.LISTEN_CALL_STATE | PhoneStateListener.LISTEN_SIGNAL_STRENGTHS
            | PhoneStateListener.LISTEN_DATA_CONNECTION_STATE;
    telephonyManager.listen(phoneListener, phoneEvents);

    if (Utils.isGmailAccessSupported(context)) {
        gmailMonitor = new GmailMonitor(context);
        gmailMonitor.startMonitor();
    }

    try {
        contentObserverMessages = new ContentObserverMessages(context);
        Uri uri = Uri.parse("content://mms-sms/conversations/");
        contentResolverMessages = context.getContentResolver();
        contentResolverMessages.registerContentObserver(uri, true, contentObserverMessages);
    } catch (Exception x) {
    }

    try {
        contentObserverCalls = new ContentObserverCalls(context);
        //Uri uri = Uri.parse("content://mms-sms/conversations/");
        contentResolverCalls = context.getContentResolver();
        contentResolverCalls.registerContentObserver(android.provider.CallLog.Calls.CONTENT_URI, true,
                contentObserverCalls);
    } catch (Exception x) {
    }

    try {
        contentObserverAppointments = new ContentObserverAppointments(context);
        Uri uri = Uri.parse("content://com.android.calendar/calendars/");
        contentResolverAppointments = context.getContentResolver();
        contentResolverAppointments.registerContentObserver(uri, true, contentObserverAppointments);
    } catch (Exception x) {
    }

    // temporary one time update
    updateWeatherData(context);

    startAlarmTicker(context);
}

From source file:com.secupwn.aimsicd.service.CellTracker.java

/**
 *  Description:    Cell Information Tracking and database logging
 *
 *          TODO: update this!!//from  ww  w.j a  va 2 s . c om
 *
 *          If the "tracking" option is enabled (as it is by default) then we are keeping
 *          a record (tracking) of the device location "gpsd_lat/lon", the connection
 *          signal strength (rx_signal) and data activity (?) and data connection state (?).
 *
 *          The items included in these are stored in the "DBi_measure" table.
 *
 *          DATA_ACTIVITY:
 *          DATA_CONNECTION_STATE:
 *
 *
 *  UI/function:        Drawer:  "Toggle Cell Tracking"
 *
 *  Issues:
 *
 *  Notes:              TODO:   We also need to listen and log for:
 *
 *      [ ]     LISTEN_CALL_STATE:
 *                  CALL_STATE_IDLE
 *                  CALL_STATE_OFFHOOK
 *                  CALL_STATE_RINGING
 *
 *      [ ]     LISTEN_SERVICE_STATE:
 *                  STATE_EMERGENCY_ONLY
 *                  STATE_IN_SERVICE
 *                  STATE_OUT_OF_SERVICE
 *                  STATE_POWER_OFF
 *
 * @param track Enable/Disable tracking
 */
public void setCellTracking(boolean track) {
    if (track) {
        tm.listen(cellSignalListener, PhoneStateListener.LISTEN_CELL_LOCATION | // gpsd_lat/lon ?
                PhoneStateListener.LISTEN_SIGNAL_STRENGTHS | // rx_signal
                PhoneStateListener.LISTEN_DATA_ACTIVITY | // No,In,Ou,IO,Do
                PhoneStateListener.LISTEN_DATA_CONNECTION_STATE | // Di,Ct,Cd,Su
                PhoneStateListener.LISTEN_CELL_INFO // !? (Need API 17)
        );
        trackingCell = true;
        Helpers.msgShort(context, context.getString(R.string.tracking_cell_information));
    } else {
        tm.listen(cellSignalListener, PhoneStateListener.LISTEN_NONE);
        device.cell.setLon(0.0);
        device.cell.setLat(0.0);
        device.setCellInfo("[0,0]|nn|nn|"); //default entries into "locationinfo"::Connection
        trackingCell = false;
        Helpers.msgShort(context, context.getString(R.string.stopped_tracking_cell_information));
    }
    setNotification();
}