Example usage for android.telephony PhoneStateListener PhoneStateListener

List of usage examples for android.telephony PhoneStateListener PhoneStateListener

Introduction

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

Prototype

public PhoneStateListener() 

Source Link

Document

Create a PhoneStateListener for the Phone with the default subscription.

Usage

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

/** Called when the activity is first created. */
@Override/*  w  w  w .  java  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.goftagram.telegram.messenger.MusicPlayerService.java

@Override
public void onCreate() {
    audioManager = (AudioManager) getSystemService(AUDIO_SERVICE);
    NotificationCenter.getInstance().addObserver(this, NotificationCenter.audioProgressDidChanged);
    NotificationCenter.getInstance().addObserver(this, NotificationCenter.audioPlayStateChanged);
    try {/*w  w w.  j a v a  2 s  . c  om*/
        phoneStateListener = new PhoneStateListener() {
            @Override
            public void onCallStateChanged(int state, String incomingNumber) {
                if (state == TelephonyManager.CALL_STATE_RINGING) {
                    if (MediaController.getInstance()
                            .isPlayingAudio(MediaController.getInstance().getPlayingMessageObject())
                            && !MediaController.getInstance().isAudioPaused()) {
                        MediaController.getInstance()
                                .pauseAudio(MediaController.getInstance().getPlayingMessageObject());
                    }
                } else if (state == TelephonyManager.CALL_STATE_IDLE) {

                } else if (state == TelephonyManager.CALL_STATE_OFFHOOK) {

                }
                super.onCallStateChanged(state, incomingNumber);
            }
        };
        TelephonyManager mgr = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
        if (mgr != null) {
            mgr.listen(phoneStateListener, PhoneStateListener.LISTEN_CALL_STATE);
        }
    } catch (Exception e) {
        FileLog.e("tmessages", e);
    }
    super.onCreate();
}

From source file:co.codecrunch.musicplayerlite.manager.MusicPlayerService.java

@Override
public void onCreate() {
    audioManager = (AudioManager) getSystemService(AUDIO_SERVICE);
    NotificationManager.getInstance().addObserver(this, NotificationManager.audioProgressDidChanged);
    NotificationManager.getInstance().addObserver(this, NotificationManager.audioPlayStateChanged);
    try {/*w  ww . j a v a 2s .com*/
        phoneStateListener = new PhoneStateListener() {
            @Override
            public void onCallStateChanged(int state, String incomingNumber) {
                if (state == TelephonyManager.CALL_STATE_RINGING) {
                    if (MediaController.getInstance()
                            .isPlayingAudio(MediaController.getInstance().getPlayingSongDetail())
                            && !MediaController.getInstance().isAudioPaused()) {
                        MediaController.getInstance()
                                .pauseAudio(MediaController.getInstance().getPlayingSongDetail());
                    }
                } else if (state == TelephonyManager.CALL_STATE_IDLE) {

                } else if (state == TelephonyManager.CALL_STATE_OFFHOOK) {

                }
                super.onCallStateChanged(state, incomingNumber);
            }
        };
        TelephonyManager mgr = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
        if (mgr != null) {
            mgr.listen(phoneStateListener, PhoneStateListener.LISTEN_CALL_STATE);
        }
    } catch (Exception e) {
        Log.e("tmessages", e.toString());
    }
    super.onCreate();
}

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

@Override
public void onCreate() {
    super.onCreate();
    try {/*  w  w  w.  j a  v  a2 s.c  om*/
        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.smedic.tubtub.BackgroundAudioService.java

private void initPhoneCallListener() {
    PhoneStateListener phoneStateListener = new PhoneStateListener() {
        @Override/*w w  w.  j a va  2 s  .  c o m*/
        public void onCallStateChanged(int state, String incomingNumber) {
            switch (state) {
            case TelephonyManager.CALL_STATE_RINGING:
                pauseVideo();
                break;
            case TelephonyManager.CALL_STATE_IDLE:
                resumeVideo();
                break;
            case TelephonyManager.CALL_STATE_OFFHOOK:
            default:
                break;
            }

            super.onCallStateChanged(state, incomingNumber);
        }
    };

    TelephonyManager mgr = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
    if (mgr != null) {
        mgr.listen(phoneStateListener, PhoneStateListener.LISTEN_CALL_STATE);
    }
}

From source file:com.pandoroid.PandoraRadioService.java

@Override
public void onCreate() {
    m_paused = false;//from   ww  w . jav a 2s  . com
    m_pandora_remote = new PandoraRadio();
    image_downloader = new ImageDownloader();
    m_stations = new ArrayList<Station>();

    connectivity_manager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
    telephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
    m_prefs = PreferenceManager.getDefaultSharedPreferences(getBaseContext());

    // Register the listener with the telephony manager
    telephonyManager.listen(new PhoneStateListener() {
        boolean pausedForRing = false;

        @Override
        public void onCallStateChanged(int state, String incomingNumber) {
            switch (state) {

            case TelephonyManager.CALL_STATE_IDLE:
                if (pausedForRing && m_song_playback != null) {
                    if (m_prefs.getBoolean("behave_resumeOnHangup", true)) {
                        if (m_song_playback != null && !m_paused) {
                            m_song_playback.play();
                        }
                    }
                }

                pausedForRing = false;
                break;

            case TelephonyManager.CALL_STATE_OFFHOOK:
            case TelephonyManager.CALL_STATE_RINGING:
                if (m_song_playback != null) {
                    m_song_playback.pause();
                }

                pausedForRing = true;
                break;
            }
        }
    }, PhoneStateListener.LISTEN_CALL_STATE);

    m_music_intent_receiver = new MusicIntentReceiver();
    this.registerReceiver(m_music_intent_receiver, new IntentFilter(AudioManager.ACTION_AUDIO_BECOMING_NOISY));
}

From source file:za.co.neilson.alarm.alert.AlarmAlertActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    final Window window = getWindow();
    window.addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
            | WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);
    window.addFlags(//from   w w  w  .j a  v a 2  s .c o m
            WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON | WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);

    setContentView(R.layout.alarm_alert);

    Bundle bundle = this.getIntent().getExtras();
    alarm = (Alarm) bundle.getSerializable("alarm");

    sendNotification(this, alarm.getAlarmName(), alarm);

    this.setTitle(alarm.getAlarmName());

    switch (alarm.getDifficulty()) {
    case SUPEREASY:
        mathProblem = new MathProblem(1);
        break;
    case EASY:
        mathProblem = new MathProblem(3);
        break;
    case MEDIUM:
        mathProblem = new MathProblem(4);
        break;
    case HARD:
        mathProblem = new MathProblem(5);
        break;
    }

    answerString = String.valueOf(mathProblem.getAnswer());
    if (answerString.endsWith(".0")) {
        answerString = answerString.substring(0, answerString.length() - 2);
    }

    problemView = (TextView) findViewById(R.id.textView1);
    problemView.setText(mathProblem.toString());

    answerView = (TextView) findViewById(R.id.textView2);
    answerView.setText("= ?");

    ((Button) findViewById(R.id.Button0)).setOnClickListener(this);
    ((Button) findViewById(R.id.Button1)).setOnClickListener(this);
    ((Button) findViewById(R.id.Button2)).setOnClickListener(this);
    ((Button) findViewById(R.id.Button3)).setOnClickListener(this);
    ((Button) findViewById(R.id.Button4)).setOnClickListener(this);
    ((Button) findViewById(R.id.Button5)).setOnClickListener(this);
    ((Button) findViewById(R.id.Button6)).setOnClickListener(this);
    ((Button) findViewById(R.id.Button7)).setOnClickListener(this);
    ((Button) findViewById(R.id.Button8)).setOnClickListener(this);
    ((Button) findViewById(R.id.Button9)).setOnClickListener(this);
    ((Button) findViewById(R.id.Button_clear)).setOnClickListener(this);
    final AlarmAlertActivity foo = this;
    ((Button) findViewById(R.id.Button_alarm_off)).setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View view) {
            alarmActive = false; //Toast.makeText(getApplicationContext(),"off",Toast.LENGTH_SHORT).show();
            foo.finish();
        }
    });
    ((Button) findViewById(R.id.Button_decimal)).setOnClickListener(this);
    ((Button) findViewById(R.id.Button_minus)).setOnClickListener(this);

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

    PhoneStateListener phoneStateListener = new PhoneStateListener() {
        @Override
        public void onCallStateChanged(int state, String incomingNumber) {
            switch (state) {
            case TelephonyManager.CALL_STATE_RINGING:
                Log.d(getClass().getSimpleName(), "Incoming call: " + incomingNumber);
                try {
                    mediaPlayer.pause();
                } catch (IllegalStateException e) {

                }
                break;
            case TelephonyManager.CALL_STATE_IDLE:
                Log.d(getClass().getSimpleName(), "Call State Idle");
                try {
                    mediaPlayer.start();
                } catch (IllegalStateException e) {

                }
                break;
            }
            super.onCallStateChanged(state, incomingNumber);
        }
    };

    telephonyManager.listen(phoneStateListener, PhoneStateListener.LISTEN_CALL_STATE);

    // Toast.makeText(this, answerString, Toast.LENGTH_LONG).show();

    startAlarm();

}

From source file:org.chirpradio.mobile.PlaybackService.java

private void setupTelephonyHooks() {
    TelephonyManager telephonyManager = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);

    phoneStateListener = new PhoneStateListener() {

        @Override/*from w ww .  j  av  a 2  s  . c om*/
        public void onCallStateChanged(int state, String incomingNumber) {
            switch (state) {
            case TelephonyManager.CALL_STATE_OFFHOOK:
            case TelephonyManager.CALL_STATE_RINGING:
                if (isPlaying) {
                    Debug.log(this, "call began, stopping...");
                    stop();
                    isInCall = true;
                }
                break;
            case TelephonyManager.CALL_STATE_IDLE:
                if (isInCall) {
                    isInCall = false;
                    Debug.log(this, "call ended; resuming...");
                    start();
                }
                break;
            }
        }
    };

    telephonyManager.listen(phoneStateListener, PhoneStateListener.LISTEN_CALL_STATE);
}

From source file:org.mythtv.client.ui.MainMenuFragment.java

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    mLocationProfile = mLocationProfileDaoHelper.findConnectedProfile(getActivity());

    adapter = new FrontendAdapter(getActivity(), R.layout.frontend_row, MainMenuFragment.GetFrontends());

    sTelManager = (TelephonyManager) getActivity().getSystemService(Context.TELEPHONY_SERVICE);
    sTelManager.listen(new PhoneStateListener() {

        /* (non-Javadoc)
         * @see android.telephony.PhoneStateListener#onCallStateChanged(int, java.lang.String)
         *///from   w  ww.j  a  va  2  s. c  om
        @Override
        public void onCallStateChanged(int state, String incomingNumber) {

            if (state == TelephonyManager.CALL_STATE_RINGING) {

                final Frontend fe = selectedFrontend;

                if (null == fe)
                    return;

                SendMessageTask sendMessageTask = new SendMessageTask(getActivity(), mLocationProfile);
                sendMessageTask.execute(fe.getUrl(), "Incoming Call From: " + incomingNumber);
            }

            super.onCallStateChanged(state, incomingNumber);
        }
    }, PhoneStateListener.LISTEN_CALL_STATE);
}