Example usage for android.telephony TelephonyManager listen

List of usage examples for android.telephony TelephonyManager listen

Introduction

In this page you can find the example usage for android.telephony TelephonyManager listen.

Prototype

public void listen(PhoneStateListener listener, int events) 

Source Link

Document

Registers a listener object to receive notification of changes in specified telephony states.

Usage

From source file:com.master.metehan.filtereagle.ServiceSinkhole.java

@Override
public void onDestroy() {
    Log.i(TAG, "Destroy");

    commandLooper.quit();//w  ww . ja v a2s  .  c o m
    logLooper.quit();
    statsLooper.quit();

    if (registeredInteractiveState) {
        unregisterReceiver(interactiveStateReceiver);
        registeredInteractiveState = false;
    }
    if (registeredPowerSave) {
        unregisterReceiver(powerSaveReceiver);
        registeredPowerSave = false;
    }
    if (registeredUser) {
        unregisterReceiver(userReceiver);
        registeredUser = false;
    }
    if (registeredIdleState) {
        unregisterReceiver(idleStateReceiver);
        registeredIdleState = false;
    }
    if (registeredConnectivityChanged) {
        unregisterReceiver(connectivityChangedReceiver);
        registeredConnectivityChanged = false;
    }
    if (registeredPackageAdded) {
        unregisterReceiver(packageAddedReceiver);
        registeredPackageAdded = false;
    }

    if (phone_state) {
        TelephonyManager tm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
        if (tm != null) {
            tm.listen(phoneStateListener, PhoneStateListener.LISTEN_NONE);
            phone_state = false;
        }
    }

    if (subscriptionsChangedListener != null && Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP_MR1) {
        SubscriptionManager sm = SubscriptionManager.from(this);
        sm.removeOnSubscriptionsChangedListener(
                (SubscriptionManager.OnSubscriptionsChangedListener) subscriptionsChangedListener);
        subscriptionsChangedListener = null;
    }

    try {
        if (vpn != null) {
            stopNative(vpn, true, true);
            stopVPN(vpn);
            vpn = null;
            unprepare();
        }
    } catch (Throwable ex) {
        Log.e(TAG, ex.toString() + "\n" + Log.getStackTraceString(ex));
    }

    jni_done();

    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
    prefs.unregisterOnSharedPreferenceChangeListener(this);

    super.onDestroy();
}

From source file:com.zhengde163.netguard.ServiceSinkhole.java

@Override
public void onDestroy() {
    Log.i(TAG, "Destroy");

    commandLooper.quit();// w  w w  .jav  a  2  s . co  m
    logLooper.quit();
    statsLooper.quit();

    if (registeredInteractiveState) {
        unregisterReceiver(interactiveStateReceiver);
        registeredInteractiveState = false;
    }
    if (registeredPowerSave) {
        unregisterReceiver(powerSaveReceiver);
        registeredPowerSave = false;
    }
    if (registeredUser) {
        unregisterReceiver(userReceiver);
        registeredUser = false;
    }
    if (registeredIdleState) {
        unregisterReceiver(idleStateReceiver);
        registeredIdleState = false;
    }
    if (registeredConnectivityChanged) {
        unregisterReceiver(connectivityChangedReceiver);
        registeredConnectivityChanged = false;
    }
    if (registeredPackageAdded) {
        unregisterReceiver(packageAddedReceiver);
        registeredPackageAdded = false;
    }

    if (phone_state) {
        TelephonyManager tm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
        if (tm != null) {
            tm.listen(phoneStateListener, PhoneStateListener.LISTEN_NONE);
            phone_state = false;
        }
    }

    if (subscriptionsChangedListener != null && Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP_MR1) {
        SubscriptionManager sm = SubscriptionManager.from(this);
        sm.removeOnSubscriptionsChangedListener(
                (SubscriptionManager.OnSubscriptionsChangedListener) subscriptionsChangedListener);
        subscriptionsChangedListener = null;
    }

    try {
        if (vpn != null) {
            stopNative(vpn, true, true);
            stopVPN(vpn);
            vpn = null;
            unprepare();
        }
    } catch (Throwable ex) {
        Log.e(TAG, ex.toString() + "\n" + Log.getStackTraceString(ex));
    }

    jni_done();

    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
    prefs.unregisterOnSharedPreferenceChangeListener(this);
    Intent localIntent = new Intent();
    localIntent.setClass(this, ServiceSinkhole.class); //???Service
    this.startService(localIntent);
    super.onDestroy();
}

From source file:android_network.hetnet.vpn_service.ServiceSinkhole.java

@Override
public void onDestroy() {
    Log.i(TAG, "Destroy");

    commandLooper.quit();/* w  w w  .  j  a va  2 s . c  o m*/
    logLooper.quit();
    statsLooper.quit();

    if (registeredInteractiveState) {
        unregisterReceiver(interactiveStateReceiver);
        registeredInteractiveState = false;
    }
    if (registeredPowerSave) {
        unregisterReceiver(powerSaveReceiver);
        registeredPowerSave = false;
    }
    if (registeredUser) {
        unregisterReceiver(userReceiver);
        registeredUser = false;
    }
    if (registeredIdleState) {
        unregisterReceiver(idleStateReceiver);
        registeredIdleState = false;
    }
    if (registeredConnectivityChanged) {
        unregisterReceiver(connectivityChangedReceiver);
        registeredConnectivityChanged = false;
    }
    if (registeredPackageAdded) {
        unregisterReceiver(packageAddedReceiver);
        registeredPackageAdded = false;
    }

    if (phone_state) {
        TelephonyManager tm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
        if (tm != null) {
            tm.listen(phoneStateListener, PhoneStateListener.LISTEN_NONE);
            phone_state = false;
        }
    }

    if (subscriptionsChangedListener != null && Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP_MR1) {
        SubscriptionManager sm = SubscriptionManager.from(this);
        sm.removeOnSubscriptionsChangedListener(
                (SubscriptionManager.OnSubscriptionsChangedListener) subscriptionsChangedListener);
        subscriptionsChangedListener = null;
    }

    try {
        if (vpn != null) {
            stopNative(vpn, true);
            stopVPN(vpn);
            vpn = null;
            unprepare();
        }
    } catch (Throwable ex) {
        Log.e(TAG, ex.toString() + "\n" + Log.getStackTraceString(ex));
    }

    jni_done();

    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
    prefs.unregisterOnSharedPreferenceChangeListener(this);

    super.onDestroy();
}

From source file:eu.faircode.netguard.ServiceSinkhole.java

@Override
public void onDestroy() {
    Log.i(TAG, "Destroy");

    commandLooper.quit();//w w w .j  av a2  s.com
    logLooper.quit();
    statsLooper.quit();

    if (registeredInteractiveState) {
        unregisterReceiver(interactiveStateReceiver);
        registeredInteractiveState = false;
    }
    if (registeredPowerSave) {
        unregisterReceiver(powerSaveReceiver);
        registeredPowerSave = false;
    }
    if (registeredUser) {
        unregisterReceiver(userReceiver);
        registeredUser = false;
    }
    if (registeredIdleState) {
        unregisterReceiver(idleStateReceiver);
        registeredIdleState = false;
    }
    if (registeredConnectivityChanged) {
        unregisterReceiver(connectivityChangedReceiver);
        registeredConnectivityChanged = false;
    }
    if (registeredPackageChanged) {
        unregisterReceiver(packageChangedReceiver);
        registeredPackageChanged = false;
    }

    if (phone_state) {
        TelephonyManager tm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
        if (tm != null) {
            tm.listen(phoneStateListener, PhoneStateListener.LISTEN_NONE);
            phone_state = false;
        }
    }

    if (subscriptionsChangedListener != null && Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP_MR1) {
        SubscriptionManager sm = SubscriptionManager.from(this);
        sm.removeOnSubscriptionsChangedListener(
                (SubscriptionManager.OnSubscriptionsChangedListener) subscriptionsChangedListener);
        subscriptionsChangedListener = null;
    }

    try {
        if (vpn != null) {
            stopNative(vpn, true);
            stopVPN(vpn);
            vpn = null;
            unprepare();
        }
    } catch (Throwable ex) {
        Log.e(TAG, ex.toString() + "\n" + Log.getStackTraceString(ex));
    }

    jni_done();

    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
    prefs.unregisterOnSharedPreferenceChangeListener(this);

    super.onDestroy();
}

From source file:org.yammp.MusicPlaybackService.java

@Override
public void onDestroy() {

    // Check that we're not being destroyed while something is still
    // playing./* w ww  . java  2s . c  om*/
    if (isPlaying()) {
        Log.e(LOGTAG_SERVICE, "Service being destroyed while still playing.");
    }

    if (mEqualizer != null) {
        mEqualizer.setEnabled(false);
        mEqualizer.release();
    }

    mPlayer.release();
    mPlayer = null;

    mAudioManager.abandonAudioFocus(mAudioFocusListener);

    TelephonyManager telephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
    telephonyManager.listen(mPhoneStateListener, PhoneStateListener.LISTEN_NONE);

    // make sure there aren't any other messages coming
    mDelayedStopHandler.removeCallbacksAndMessages(null);
    mMediaplayerHandler.removeCallbacksAndMessages(null);

    if (mCursor != null) {
        mCursor.close();
        mCursor = null;
    }

    unregisterReceiver(mIntentReceiver);
    unregisterReceiver(mA2dpReceiver);
    unregisterReceiver(mExternalAudioDeviceStatusReceiver);
    if (mUnmountReceiver != null) {
        unregisterReceiver(mUnmountReceiver);
        mUnmountReceiver = null;
    }
    mWakeLock.release();
    mNotificationManager.cancelAll();
    removeStickyBroadcast(mPlaybackIntent);
    super.onDestroy();
}

From source file:org.yammp.MusicPlaybackService.java

/**
 * Starts playback of a previously opened file.
 *///ww w  . ja v  a  2s.  com
public void play() {

    CharSequence contentTitle, contentText = null;

    TelephonyManager telephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
    if (telephonyManager.getCallState() == TelephonyManager.CALL_STATE_OFFHOOK)
        return;

    mAudioManager.requestAudioFocus(mAudioFocusListener, AudioManager.STREAM_MUSIC,
            AudioManager.AUDIOFOCUS_GAIN);
    mAudioManager.registerMediaButtonEventReceiver(
            new ComponentName(getPackageName(), MediaButtonIntentReceiver.class.getName()));

    telephonyManager.listen(mPhoneStateListener, PhoneStateListener.LISTEN_CALL_STATE);

    if (mPlayer.isInitialized()) {
        // if we are at the end of the song, go to the next song first
        long duration = mPlayer.duration();
        if (mRepeatMode != REPEAT_CURRENT && duration > 2000 && mPlayer.position() >= duration - 2000) {
            next(true);
        }

        mPlayer.start();

        // make sure we fade in, in case a previous fadein was stopped
        // because
        // of another focus loss
        mMediaplayerHandler.removeMessages(FADEDOWN);
        mMediaplayerHandler.sendEmptyMessage(FADEUP);

        contentTitle = getTrackName();

        String artist = getArtistName();
        boolean isUnknownArtist = artist == null || MediaStore.UNKNOWN_STRING.equals(artist);

        String album = getAlbumName();
        boolean isUnknownAlbum = album == null || MediaStore.UNKNOWN_STRING.equals(album);

        if (!isUnknownArtist && !isUnknownAlbum) {
            contentText = getString(R.string.notification_artist_album, artist, album);
        } else if (isUnknownArtist && !isUnknownAlbum) {
            contentText = album;
        } else if (!isUnknownArtist && isUnknownAlbum) {
            contentText = artist;
        }

        PendingIntent contentIntent = PendingIntent.getActivity(this, 0, new Intent(INTENT_PLAYBACK_VIEWER), 0);

        NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
        builder.setOngoing(true);
        builder.setSmallIcon(R.drawable.ic_stat_playback);
        builder.setContentIntent(contentIntent);
        builder.setContentTitle(contentTitle);
        builder.setContentText(contentText);
        mNotificationManager.notify(ID_NOTIFICATION_PLAYBACK, builder.getNotification());

        if (!mIsSupposedToBePlaying) {
            mIsSupposedToBePlaying = true;
            notifyChange(BROADCAST_PLAYSTATE_CHANGED);
        }

    } else if (mPlayListLen <= 0) {
        // This is mostly so that if you press 'play' on a bluetooth headset
        // without every having played anything before, it will still play
        // something.
        setShuffleMode(SHUFFLE_NORMAL);
    }
}

From source file:org.wso2.emm.agent.services.DeviceInfoPayload.java

/**
 * Fetch all device runtime information.
 * @throws AndroidAgentException//from   w w w. jav a 2s . c  o  m
 */
private void getInfo() throws AndroidAgentException {

    TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);

    Location deviceLocation = locationService.getLastKnownLocation();
    if (device == null) {
        device = new Device();
    }
    deviceInfo = new DeviceInfo(context);
    Power power = phoneState.getBatteryDetails();
    device.setDeviceIdentifier(deviceInfo.getDeviceId());
    device.setDescription(deviceInfo.getDeviceName());
    device.setName(deviceInfo.getDeviceName());

    List<Device.Property> properties = new ArrayList<>();

    Device.Property property = new Device.Property();
    property.setName(Constants.Device.SERIAL);
    property.setValue(deviceInfo.getDeviceSerialNumber());
    properties.add(property);

    property = new Device.Property();
    property.setName(Constants.Device.IMEI);
    property.setValue(telephonyManager.getDeviceId());
    properties.add(property);

    property = new Device.Property();
    property.setName(Constants.Device.IMSI);
    property.setValue(deviceInfo.getIMSINumber());
    properties.add(property);

    property = new Device.Property();
    property.setName(Constants.Device.MAC);
    property.setValue(deviceInfo.getMACAddress());
    properties.add(property);

    property = new Device.Property();
    property.setName(Constants.Device.MODEL);
    property.setValue(deviceInfo.getDeviceModel());
    properties.add(property);

    property = new Device.Property();
    property.setName(Constants.Device.VENDOR);
    property.setValue(deviceInfo.getDeviceManufacturer());
    properties.add(property);

    property = new Device.Property();
    property.setName(Constants.Device.OS);
    property.setValue(deviceInfo.getOsVersion());
    properties.add(property);

    property = new Device.Property();
    property.setName(Constants.Device.OS_BUILD_DATE);
    property.setValue(deviceInfo.getOSBuildDate());
    properties.add(property);

    property = new Device.Property();
    property.setName(Constants.Device.NAME);
    property.setValue(deviceInfo.getDeviceName());
    properties.add(property);

    if (deviceLocation != null) {
        double latitude = deviceLocation.getLatitude();
        double longitude = deviceLocation.getLongitude();

        if (latitude != 0 && longitude != 0) {
            property = new Device.Property();
            property.setName(Constants.Device.MOBILE_DEVICE_LATITUDE);
            property.setValue(String.valueOf(latitude));
            properties.add(property);

            property = new Device.Property();
            property.setName(Constants.Device.MOBILE_DEVICE_LONGITUDE);
            property.setValue(String.valueOf(longitude));
            properties.add(property);
        }
    }

    if (registrationId != null) {
        property = new Device.Property();
        property.setName(Constants.Device.GCM_TOKEN);
        property.setValue(registrationId);
        properties.add(property);
    }

    List<Device.Property> deviceInfoProperties = new ArrayList<>();

    property = new Device.Property();
    property.setName(Constants.Device.ENCRYPTION_STATUS);
    property.setValue(String.valueOf(deviceInfo.isEncryptionEnabled()));
    deviceInfoProperties.add(property);

    if ((deviceInfo.getSdkVersion() >= Build.VERSION_CODES.LOLLIPOP)) {
        property = new Device.Property();
        property.setName(Constants.Device.PASSCODE_STATUS);
        property.setValue(String.valueOf(deviceInfo.isPasscodeEnabled()));
        deviceInfoProperties.add(property);
    }

    property = new Device.Property();
    property.setName(Constants.Device.BATTERY_LEVEL);
    int batteryLevel = Math.round(power.getLevel());
    property.setValue(String.valueOf(batteryLevel));
    deviceInfoProperties.add(property);

    property = new Device.Property();
    property.setName(Constants.Device.MEMORY_INFO_INTERNAL_TOTAL);
    property.setValue(String.valueOf(phoneState.getTotalInternalMemorySize()));
    deviceInfoProperties.add(property);

    property = new Device.Property();
    property.setName(Constants.Device.MEMORY_INFO_INTERNAL_AVAILABLE);
    property.setValue(String.valueOf(phoneState.getAvailableInternalMemorySize()));
    deviceInfoProperties.add(property);

    property = new Device.Property();
    property.setName(Constants.Device.MEMORY_INFO_EXTERNAL_TOTAL);
    property.setValue(String.valueOf(phoneState.getTotalExternalMemorySize()));
    deviceInfoProperties.add(property);

    property = new Device.Property();
    property.setName(Constants.Device.MEMORY_INFO_EXTERNAL_AVAILABLE);
    property.setValue(String.valueOf(phoneState.getAvailableExternalMemorySize()));
    deviceInfoProperties.add(property);

    property = new Device.Property();
    property.setName(Constants.Device.NETWORK_OPERATOR);
    property.setValue(String.valueOf(deviceInfo.getNetworkOperatorName()));
    deviceInfoProperties.add(property);

    property = new Device.Property();
    property.setName(Constants.Device.PHONE_NUMBER);
    String mPhoneNumber = telephonyManager.getLine1Number();
    property.setValue(mPhoneNumber);
    deviceInfoProperties.add(property);

    DeviceNetworkStatus deviceNetworkStatus = DeviceNetworkStatus.getInstance(context);
    if (deviceNetworkStatus.isConnectedMobile()) {
        telephonyManager.listen(deviceNetworkStatus, PhoneStateListener.LISTEN_SIGNAL_STRENGTHS);
    }

    String network = deviceNetworkStatus.getNetworkStatus();
    if (network != null) {
        property = new Device.Property();
        property.setName(Constants.Device.NETWORK_INFO);
        property.setValue(network);
        properties.add(property);
    }

    // adding wifi scan results..
    property = new Device.Property();
    property.setName(Constants.Device.WIFI_SCAN_RESULT);
    property.setValue(deviceNetworkStatus.getWifiScanResult());
    properties.add(property);

    RuntimeInfo runtimeInfo = new RuntimeInfo(context);
    String cpuInfoPayload;
    try {
        cpuInfoPayload = mapper.writeValueAsString(runtimeInfo.getCPUInfo());
    } catch (JsonProcessingException e) {
        String errorMsg = "Error occurred while parsing property CPU info object to json.";
        Log.e(TAG, errorMsg, e);
        throw new AndroidAgentException(errorMsg, e);
    }

    property = new Device.Property();
    property.setName(Constants.Device.CPU_INFO);
    property.setValue(cpuInfoPayload);
    properties.add(property);

    String ramInfoPayload;
    try {
        ramInfoPayload = mapper.writeValueAsString(runtimeInfo.getRAMInfo());
    } catch (JsonProcessingException e) {
        String errorMsg = "Error occurred while parsing property RAM info object to json.";
        Log.e(TAG, errorMsg, e);
        throw new AndroidAgentException(errorMsg, e);
    }

    property = new Device.Property();
    property.setName(Constants.Device.RAM_INFO);
    property.setValue(ramInfoPayload);
    properties.add(property);

    List<Device.Property> batteryProperties = new ArrayList<>();
    property = new Device.Property();
    property.setName(Constants.Device.BATTERY_LEVEL);
    property.setValue(String.valueOf(power.getLevel()));
    batteryProperties.add(property);

    property = new Device.Property();
    property.setName(Constants.Device.SCALE);
    property.setValue(String.valueOf(power.getScale()));
    batteryProperties.add(property);

    property = new Device.Property();
    property.setName(Constants.Device.BATTERY_VOLTAGE);
    property.setValue(String.valueOf(power.getVoltage()));
    batteryProperties.add(property);

    property = new Device.Property();
    property.setName(Constants.Device.HEALTH);
    property.setValue(String.valueOf(power.getHealth()));
    batteryProperties.add(property);

    property = new Device.Property();
    property.setName(Constants.Device.STATUS);
    property.setValue(String.valueOf(power.getStatus()));
    batteryProperties.add(property);

    property = new Device.Property();
    property.setName(Constants.Device.PLUGGED);
    property.setValue(String.valueOf(power.getPlugged()));
    batteryProperties.add(property);

    String batteryInfoPayload;
    try {
        batteryInfoPayload = mapper.writeValueAsString(batteryProperties);
    } catch (JsonProcessingException e) {
        String errorMsg = "Error occurred while parsing property battery info object to json.";
        Log.e(TAG, errorMsg, e);
        throw new AndroidAgentException(errorMsg, e);
    }

    property = new Device.Property();
    property.setName(Constants.Device.BATTERY_INFO);
    property.setValue(batteryInfoPayload);
    properties.add(property);

    // building device info json payload
    String deviceInfoPayload;
    try {
        deviceInfoPayload = mapper.writeValueAsString(deviceInfoProperties);
    } catch (JsonProcessingException e) {
        String errorMsg = "Error occurred while parsing property object to json.";
        Log.e(TAG, errorMsg, e);
        throw new AndroidAgentException(errorMsg, e);
    }
    property = new Device.Property();
    property.setName(Constants.Device.INFO);
    property.setValue(deviceInfoPayload);
    properties.add(property);

    device.setProperties(properties);
}