Example usage for android.telephony PhoneStateListener LISTEN_DATA_ACTIVITY

List of usage examples for android.telephony PhoneStateListener LISTEN_DATA_ACTIVITY

Introduction

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

Prototype

int LISTEN_DATA_ACTIVITY

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

Click Source Link

Document

Listen for changes to the direction of data traffic on the data connection (cellular).

Usage

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

@Override
public void onCreate() {
    super.onCreate();
    try {/*from   w w w .j ava  2s.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.karpenstein.signalmon.SignalMonitorActivity.java

/** Called when the activity is first created. */
@Override//w  w w .j  av a  2  s.c  o 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.odo.kcl.mobileminer.miner.MinerService.java

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    startTime = new Date();
    //Log.i("MinerService","started mining");
    int phoneFlags;

    phoneFlags = PhoneStateListener.LISTEN_DATA_ACTIVITY | PhoneStateListener.LISTEN_CELL_LOCATION;

    /**//  w w  w . j  a  v a  2  s .  c om
     if (Build.VERSION.SDK_INT >= 17) {
     // http://code.google.com/p/android/issues/detail?id=43467
     // http://stackoverflow.com/questions/20049510/oncellinfochanged-callback-is-always-null
     phoneFlags = PhoneStateListener.LISTEN_DATA_ACTIVITY|PhoneStateListener.LISTEN_CELL_INFO
     |PhoneStateListener.LISTEN_SIGNAL_STRENGTHS;
     }
     else {
     phoneFlags = PhoneStateListener.LISTEN_DATA_ACTIVITY|PhoneStateListener.LISTEN_CELL_LOCATION;
     }
     **/

    ((TelephonyManager) this.getSystemService(Context.TELEPHONY_SERVICE)).listen(phoneListener, phoneFlags);
    Toast.makeText(this, "Started Mining...", Toast.LENGTH_SHORT).show();
    moveToForeground();
    return START_STICKY;
}

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

/**
 *  Description:    Cell Information Tracking and database logging
 *
 *          TODO: update this!!/*w  w w .j  a  v a 2s.co  m*/
 *
 *          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();
}

From source file:com.SecUpwN.AIMSICD.service.AimsicdService.java

/**
 * Cell Information Tracking and database logging
 *
 * @param track Enable/Disable tracking/*from  w  ww  . jav a  2 s.c  o m*/
 */
public void setCellTracking(boolean track) {
    if (track) {
        tm.listen(mCellSignalListener,
                PhoneStateListener.LISTEN_CELL_LOCATION | PhoneStateListener.LISTEN_SIGNAL_STRENGTHS
                        | PhoneStateListener.LISTEN_DATA_ACTIVITY
                        | PhoneStateListener.LISTEN_DATA_CONNECTION_STATE);
        if (lm != null) {
            mLocationListener = new MyLocationListener();
            Log.i(TAG, "LocationManager already existed");
            lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, GPS_MIN_UPDATE_TIME,
                    GPS_MIN_UPDATE_DISTANCE, mLocationListener);
        } else {
            Log.i(TAG, "LocationManager did not exist");
            lm = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
            if (lm != null) {
                if (lm.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
                    Log.i(TAG, "LocationManager created");
                    lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, GPS_MIN_UPDATE_TIME,
                            GPS_MIN_UPDATE_DISTANCE, mLocationListener);
                }
            }
        }
        Helpers.msgShort(this, "Tracking cell information");
        mTrackingCell = true;
    } else {
        tm.listen(mCellSignalListener, PhoneStateListener.LISTEN_NONE);
        lm.removeUpdates(mLocationListener);
        mDevice.mCell.setLon(0.0);
        mDevice.mCell.setLat(0.0);
        mTrackingCell = false;
        mDevice.setCellInfo("[0,0]|nn|nn|");
        Helpers.msgShort(this, "Stopped tracking cell information");
    }
    setNotification();
}