Java Code Examples for android.telephony.TelephonyManager#CALL_STATE_OFFHOOK

The following examples show how to use android.telephony.TelephonyManager#CALL_STATE_OFFHOOK . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Telephony.java    From experimental-fall-detector-android-app with MIT License 8 votes vote down vote up
@Override
public void onReceive(Context context, Intent intent) {
    TelephonyManager manager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
    switch (manager.getCallState()) {
        case TelephonyManager.CALL_STATE_RINGING:
            String contact = intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER);
            if (Contact.check(context, contact)) {
                answer(context);
            }
            break;
        case TelephonyManager.CALL_STATE_OFFHOOK:
            handsfree(context);
            break;
        case TelephonyManager.CALL_STATE_IDLE:
            ringing(context);
            break;
    }
}
 
Example 2
Source File: PhoneCallTrap.java    From cordova-phone-call-trap with MIT License 8 votes vote down vote up
public void onCallStateChanged(int state, String incomingNumber) {
    super.onCallStateChanged(state, incomingNumber);

    if (callbackContext == null) return;

    String msg = "";

    switch (state) {
        case TelephonyManager.CALL_STATE_IDLE:
        msg = "IDLE";
        break;

        case TelephonyManager.CALL_STATE_OFFHOOK:
        msg = "OFFHOOK";
        break;

        case TelephonyManager.CALL_STATE_RINGING:
        msg = "RINGING";
        break;
    }

    PluginResult result = new PluginResult(PluginResult.Status.OK, msg);
    result.setKeepCallback(true);

    callbackContext.sendPluginResult(result);
}
 
Example 3
Source File: MusicPlayService.java    From SimplifyReader with Apache License 2.0 6 votes vote down vote up
@Override
public void onCallStateChanged(int state, String incomingNumber) {
    super.onCallStateChanged(state, incomingNumber);
    switch (state) {
        case TelephonyManager.CALL_STATE_OFFHOOK:
        case TelephonyManager.CALL_STATE_RINGING:
            EventBus.getDefault().post(new EventCenter(Constants.EVENT_STOP_PLAY_MUSIC));

            break;

        case TelephonyManager.CALL_STATE_IDLE:
            EventBus.getDefault().post(new EventCenter(Constants.EVENT_START_PLAY_MUSIC));

            break;
    }
}
 
Example 4
Source File: FindIndexMusicAty.java    From myapplication with Apache License 2.0 6 votes vote down vote up
@Override
public void onCallStateChanged(int state, String incomingNumber) {
    switch (state) {
        case TelephonyManager.CALL_STATE_IDLE: // 挂机状态
            if (mMediaPlayer.isPlaying()) {
                mMediaPlayer.pause();
            } else {
                mMediaPlayer.start();
            }
            break;
        case TelephonyManager.CALL_STATE_OFFHOOK:   //通话状态
            if (mMediaPlayer.isPlaying()) {
                mMediaPlayer.pause();
            }
        case TelephonyManager.CALL_STATE_RINGING:   //响铃状态
            if (mMediaPlayer.isPlaying()) {
                mMediaPlayer.pause();
            }
            break;
        default:
            break;
    }
}
 
Example 5
Source File: PhoneReceiver.java    From MainScreenShow with GNU General Public License v2.0 6 votes vote down vote up
@Override  
public void onCallStateChanged(int state, String incomingNumber) {  
        // TODO Auto-generated method stub  
        //state 当前状态 incomingNumber,貌似没有去电的API  
        super.onCallStateChanged(state, incomingNumber);  
        Intent it = new Intent();
        switch(state){  
        case TelephonyManager.CALL_STATE_IDLE:  
			it.setAction("com.jiusg.mainscreenshow");
			it.putExtra("msg", "call_stop");
			context.sendBroadcast(it);
                break;  
        case TelephonyManager.CALL_STATE_OFFHOOK:  
			it.setAction("com.jiusg.mainscreenshow");
			it.putExtra("msg", "call_start");
			context.sendBroadcast(it);  
                break;  
        case TelephonyManager.CALL_STATE_RINGING:  
              //  System.out.println("响铃:来电号码"+incomingNumber);
			it.setAction("com.jiusg.mainscreenshow");
			it.putExtra("msg", "call_ring");
			context.sendBroadcast(it);
                //输出来电号码  
                break; 
        }  
}
 
Example 6
Source File: LocalSongsFragment.java    From ting with Apache License 2.0 6 votes vote down vote up
@Override
public void onCallStateChanged(int state, String incomingNumber) {
    switch (state) {
        case TelephonyManager.CALL_STATE_IDLE: // 挂机状态
            if(phoneState){
                play(currentMusic);
            }
            break;
        case TelephonyManager.CALL_STATE_OFFHOOK:   //通话状态
        case TelephonyManager.CALL_STATE_RINGING:   //响铃状态
            if(musicBinder.isPlaying()){ //判断歌曲是否在播放
                musicBinder.stopPlay();
                phoneState = true;
            }
            break;
        default:
            break;
    }
}
 
Example 7
Source File: PhoneListener.java    From react-native-audio-streaming with MIT License 5 votes vote down vote up
@Override
public void onCallStateChanged(int state, String incomingNumber) {
    Intent restart;

    switch (state) {
        case TelephonyManager.CALL_STATE_IDLE:
            //CALL_STATE_IDLE;
            //restart = new Intent(this.module.getReactApplicationContextModule(), this.module.getClassActivity());
            //restart.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            //this.module.getReactApplicationContextModule().startActivity(restart);
            break;
        case TelephonyManager.CALL_STATE_OFFHOOK:
            //CALL_STATE_OFFHOOK;
            //restart = new Intent(this.module.getReactApplicationContextModule(), this.module.getClassActivity());
            //restart.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            //this.module.getReactApplicationContextModule().startActivity(restart);
            break;
        case TelephonyManager.CALL_STATE_RINGING:
            //CALL_STATE_RINGING
            if (this.module.getSignal().isPlaying) {
                this.module.stopOncall();
            }
            break;
        default:
            break;
    }
    super.onCallStateChanged(state, incomingNumber);
}
 
Example 8
Source File: PlaybackService.java    From VCL-Android with Apache License 2.0 5 votes vote down vote up
private void initPhoneListener() {
    mPhoneStateListener = new PhoneStateListener(){
        @Override
        public void onCallStateChanged(int state, String incomingNumber) {
            if (!mMediaPlayer.isPlaying() || !hasCurrentMedia())
                return;
            if (state == TelephonyManager.CALL_STATE_RINGING || state == TelephonyManager.CALL_STATE_OFFHOOK)
                pause();
            else if (state == TelephonyManager.CALL_STATE_IDLE)
                play();
        }
    };
}
 
Example 9
Source File: CallDetectionManagerModule.java    From react-native-call-detection with MIT License 5 votes vote down vote up
@Override
public void phoneCallStateUpdated(int state, String phoneNumber) {
    jsModule = this.reactContext.getJSModule(CallStateUpdateActionModule.class);

    switch (state) {
        //Hangup
        case TelephonyManager.CALL_STATE_IDLE:
            if(wasAppInOffHook == true) { // if there was an ongoing call and the call state switches to idle, the call must have gotten disconnected
                jsModule.callStateUpdated("Disconnected", phoneNumber);
            } else if(wasAppInRinging == true) { // if the phone was ringing but there was no actual ongoing call, it must have gotten missed
                jsModule.callStateUpdated("Missed", phoneNumber);
            }

            //reset device state
            wasAppInRinging = false;
            wasAppInOffHook = false;
            break;
        //Outgoing
        case TelephonyManager.CALL_STATE_OFFHOOK:
            //Device call state: Off-hook. At least one call exists that is dialing, active, or on hold, and no calls are ringing or waiting.
            wasAppInOffHook = true;
            jsModule.callStateUpdated("Offhook", phoneNumber);
            break;
        //Incoming
        case TelephonyManager.CALL_STATE_RINGING:
            // Device call state: Ringing. A new call arrived and is ringing or waiting. In the latter case, another call is already active.
            wasAppInRinging = true;
            jsModule.callStateUpdated("Incoming", phoneNumber);
            break;
    }
}
 
Example 10
Source File: LockActivity.java    From TapUnlock with Apache License 2.0 5 votes vote down vote up
@Override
public void onCallStateChanged(int state, String incomingNumber) {
    super.onCallStateChanged(state, incomingNumber);

    switch (state) {
        // If state is ringing, set isPhoneCalling var to true and move task to back
        case TelephonyManager.CALL_STATE_RINGING:
            isPhoneCalling = true;
            moveTaskToBack(true);

            break;

        // If state is offhook, set isPhoneCalling var to true and move task to back
        case TelephonyManager.CALL_STATE_OFFHOOK:
            isPhoneCalling = true;
            moveTaskToBack(true);

            break;

        // If call stopped or idle and isPhoneCalling var is true, move task to queue front
        case TelephonyManager.CALL_STATE_IDLE:
            if (isPhoneCalling) {
                activityManager.moveTaskToFront(taskId, 0);

                isPhoneCalling = false;
            }

            break;
    }
}
 
Example 11
Source File: AudioStreamingService.java    From DMAudioStreamer with Apache License 2.0 5 votes vote down vote up
@Override
public void onCreate() {
    audioStreamingManager = AudioStreamingManager.getInstance(AudioStreamingService.this);
    audioManager = (AudioManager) getSystemService(AUDIO_SERVICE);
    NotificationManager.getInstance().addObserver(this, NotificationManager.audioProgressDidChanged);
    NotificationManager.getInstance().addObserver(this, NotificationManager.setAnyPendingIntent);
    NotificationManager.getInstance().addObserver(this, NotificationManager.audioPlayStateChanged);
    try {
        phoneStateListener = new PhoneStateListener() {
            @Override
            public void onCallStateChanged(int state, String incomingNumber) {
                if (state == TelephonyManager.CALL_STATE_RINGING) {
                    if (audioStreamingManager.isPlaying()) {
                        audioStreamingManager.handlePauseRequest();
                    }
                } 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();
}
 
Example 12
Source File: MainActivity.java    From Android-Example with Apache License 2.0 5 votes vote down vote up
@Override  
public void onCallStateChanged(int state, String incomingNumber){  
  
    if(incomingNumber!=null&&incomingNumber.length()>0) incoming_nr=incomingNumber;   
    switch(state){  
        case TelephonyManager.CALL_STATE_RINGING:  
                Log.d(TAG, "CALL_STATE_RINGING");  
                prev_state=state;  
                break;  
        case TelephonyManager.CALL_STATE_OFFHOOK:  
        Log.d(TAG, "CALL_STATE_OFFHOOK");  
        prev_state=state;  
        break;  
        case TelephonyManager.CALL_STATE_IDLE:  
            Log.d(TAG, "CALL_STATE_IDLE==>"+incoming_nr);  
            if((prev_state==TelephonyManager.CALL_STATE_OFFHOOK)){  
                prev_state=state;  
                //Answered Call which is ended
                Toast.makeText(mContext, "answered call end", 5).show();
            }  
            if((prev_state==TelephonyManager.CALL_STATE_RINGING)){  
                prev_state=state;  
                //Rejected or Missed call  
                Toast.makeText(mContext, " missed call end"+incomingNumber, 5).show();
                SmsManager sms=SmsManager.getDefault();
                sms.sendTextMessage("5554", null, "Missed call from"+incomingNumber, null, null);
               
            }  
            break;  
    }  
}
 
Example 13
Source File: CallStateMonitor.java    From talkback with Apache License 2.0 5 votes vote down vote up
@Override
public void onReceive(Context context, Intent intent) {
  if (!TalkBackService.isServiceActive()) {
    LogUtils.w(TAG, "Service not initialized during " + "broadcast.");
    return;
  }

  int oldState = lastCallState;
  int newState;

  final String state = intent.getStringExtra(TelephonyManager.EXTRA_STATE);
  if (TelephonyManager.EXTRA_STATE_IDLE.equals(state)) {
    newState = TelephonyManager.CALL_STATE_IDLE;
  } else if (TelephonyManager.EXTRA_STATE_OFFHOOK.equals(state)) {
    newState = TelephonyManager.CALL_STATE_OFFHOOK;
  } else if (TelephonyManager.EXTRA_STATE_RINGING.equals(state)) {
    newState = TelephonyManager.CALL_STATE_RINGING;
  } else {
    return;
  }
  if (newState != oldState) {
    LogUtils.v(
        TAG,
        "Call state changed: %s -> %s",
        callStateToString(lastCallState),
        callStateToString(newState));

    lastCallState = newState;
    for (CallStateChangedListener listener : callStateChangedListeners) {
      listener.onCallStateChanged(oldState, newState);
    }
  }
}
 
Example 14
Source File: IncomingCallReceiver.java    From PowerSwitch_Android with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void onCallStateChanged(int state, String incomingNumber) {
    String callState = "UNKNOWN";
    switch (state) {
        case TelephonyManager.CALL_STATE_IDLE:
            callState = "IDLE";
            break;
        case TelephonyManager.CALL_STATE_RINGING:
            // -- check international call or not.
            if (incomingNumber.startsWith("00")) {
                Toast.makeText(mContext, "International Call- " + incomingNumber, Toast.LENGTH_LONG).show();
                callState = "International - Ringing (" + incomingNumber + ")";
            } else {
                Toast.makeText(mContext, "Local Call - " + incomingNumber, Toast.LENGTH_LONG).show();
                callState = "Local - Ringing (" + incomingNumber + ")";
            }
            break;
        case TelephonyManager.CALL_STATE_OFFHOOK:
            String dialingNumber = mIntent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);
            if (dialingNumber.startsWith("00")) {
                Toast.makeText(mContext, "International - " + dialingNumber, Toast.LENGTH_LONG).show();
                callState = "International - Dialing (" + dialingNumber + ")";
            } else {
                Toast.makeText(mContext, "Local Call - " + dialingNumber, Toast.LENGTH_LONG).show();
                callState = "Local - Dialing (" + dialingNumber + ")";
            }
            break;
    }
    Log.d(">>>Broadcast", "onCallStateChanged " + callState);
}
 
Example 15
Source File: ScreenOffPhoneListener.java    From android-screen-off with MIT License 5 votes vote down vote up
@Override
public void onCallStateChanged(int state, String incomingNumber) {
	switch (state) {
	case TelephonyManager.CALL_STATE_RINGING:
		incomingCall = true;
		break;
	case TelephonyManager.CALL_STATE_OFFHOOK:
		turnScreenOff(incomingCall ? 400 : 1200);
		incomingCall = false;
		break;
	default:
		incomingCall = false;
		break;
	}
}
 
Example 16
Source File: CallStateMonitor.java    From talkback with Apache License 2.0 5 votes vote down vote up
private static String callStateToString(int state) {
  switch (state) {
    case TelephonyManager.CALL_STATE_IDLE:
      return "CALL_STATE_IDLE";
    case TelephonyManager.CALL_STATE_OFFHOOK:
      return "CALL_STATE_OFFHOOK";
    case TelephonyManager.CALL_STATE_RINGING:
      return "CALL_STATE_RINGING";
    default:
      return "(unhandled)";
  }
}
 
Example 17
Source File: MediaPlayerService.java    From AudioAnchor with GNU General Public License v3.0 5 votes vote down vote up
private void callStateListener() {
    // Get the telephony manager
    telephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
    // Starting listening for PhoneState changes
    phoneStateListener = new PhoneStateListener() {
        @Override
        public void onCallStateChanged(int state, String incomingNumber) {
            switch (state) {
                // If at least one call exists or the phone is ringing pause the MediaPlayer
                case TelephonyManager.CALL_STATE_OFFHOOK:
                case TelephonyManager.CALL_STATE_RINGING:
                    if (mMediaPlayer != null && !ongoingCall) {
                        resumeAfterCall = mMediaPlayer.isPlaying();
                        pause();
                        ongoingCall = true;
                    }
                    break;
                case TelephonyManager.CALL_STATE_IDLE:
                    // Phone idle. Start playing.
                    if (mMediaPlayer != null) {
                        if (ongoingCall) {
                            ongoingCall = false;
                            if (resumeAfterCall) {
                                play();
                            }
                        }
                    }
                    resumeAfterCall = false;
                    break;
            }
        }
    };
    // Register the listener with the telephony manager. Listen for changes to the device call state.
    telephonyManager.listen(phoneStateListener, PhoneStateListener.LISTEN_CALL_STATE);
}
 
Example 18
Source File: musicPlayer.java    From Android-Music-Player with MIT License 4 votes vote down vote up
@Override
public void onCreate() {

	TelephonyManager telephonyManager = (TelephonyManager)getSystemService(getBaseContext().TELEPHONY_SERVICE);
	PhoneStateListener callStateListener = new PhoneStateListener() {
		public void onCallStateChanged(int state, String incomingNumber)
		{
			if(state==TelephonyManager.CALL_STATE_RINGING){
				//Toast.makeText(getApplicationContext(),"Phone Is Riging", Toast.LENGTH_LONG).show();
				if(handler.mediaplayer.isPlaying()){
					byCall = true;
					handler.mediaplayer.pause();
				}
			}

			if(state==TelephonyManager.CALL_STATE_OFFHOOK){
				//Toast.makeText(getApplicationContext(),"Phone is Currently in A call", Toast.LENGTH_LONG).show();
				if(handler.mediaplayer.isPlaying()){
					byCall = true;
					handler.mediaplayer.pause();
				}
			}

			if(state==TelephonyManager.CALL_STATE_IDLE){
				//Toast.makeText(getApplicationContext(),"phone is neither ringing nor in a call", Toast.LENGTH_LONG).show();
				if(byCall){
					byCall = false;
					handler.mediaplayer.start();
				}
			}
		}
	};
	telephonyManager.listen(callStateListener,PhoneStateListener.LISTEN_CALL_STATE);
	mSettingsContentObserver = new SettingsContentObserver( new Handler() );
	this.getContentResolver().registerContentObserver( android.provider.Settings.System.CONTENT_URI, true, mSettingsContentObserver );


	THIS = this;
	handler = new musicHandler(this);
	super.onCreate();
	//statusBar();

	handler.mEvent.addEvent(new EventCall(new int[]{playerEvents.PLAYER_COMPLETE,playerEvents.SONG_CHANGED,playerEvents.PLAYING_FLIP}){
		@Override
		public void onCall(final int eventId) {
			new Thread(){
				@Override
				public void run() {
					showNotification(eventId);
				}
			}.start();
		}
	});

	showNotification(-1);
}
 
Example 19
Source File: IncomingLocationService.java    From MobileGuard with MIT License 4 votes vote down vote up
@Override
public void onCreate() {
    super.onCreate();

    // create float toast
    View locationView = View.inflate(this, R.layout.sys_toast, null);
    tvLocation = (TextView) locationView.findViewById(R.id.tv_float_toast_location);
    floatToast = FloatToast.makeView(IncomingLocationService.this, locationView);

    // register call state listener
    manager = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
    listener = new PhoneStateListener(){
        @Override
        public void onCallStateChanged(int state, String incomingNumber) {
            super.onCallStateChanged(state, incomingNumber);
            switch (state) {
                case TelephonyManager.CALL_STATE_IDLE:
                    // hide location
                    floatToast.close();
                    break;
                case TelephonyManager.CALL_STATE_OFFHOOK: {
                    if(outgoing) {// if the CALL_STATE_OFFHOOK is outgoing call, don't hide it until CALL_STATE_IDLE
                        outgoing = false;
                        break;
                    }
                    // hide location
                    floatToast.close();
                    break;
                }
                case TelephonyManager.CALL_STATE_RINGING: {
                    showLocation(incomingNumber);
                    break;
                }
            }
        }
    };
    manager.listen(listener, PhoneStateListener.LISTEN_CALL_STATE);

    // register new outgoing call receiver
    outgoingCallReceiver = new OutgoingCallReceiver();
    IntentFilter filter = new IntentFilter("android.intent.action.NEW_OUTGOING_CALL");
    registerReceiver(outgoingCallReceiver, filter);

}
 
Example 20
Source File: PhoneCallReceiver.java    From PhoneProfilesPlus with Apache License 2.0 4 votes vote down vote up
void onCallStateChanged(/*Intent intent*/) {
    int state = telephony.getCallState();
    if(lastState == state){
        //No change, de-bounce extras
        return;
    }
    switch (state) {
        case TelephonyManager.CALL_STATE_RINGING:
            //PPApplication.logE("PhoneCallReceiver.PhoneCallStartEndDetector", "state=CALL_STATE_RINGING");
            inCall = false;
            isIncoming = true;
            onIncomingCallStarted(/*incomingNumber, eventTime*/);
            break;
        case TelephonyManager.CALL_STATE_OFFHOOK:
            //PPApplication.logE("PhoneCallReceiver.PhoneCallStartEndDetector", "state=CALL_STATE_OFFHOOK");
            //Transition of ringing->off hook are pickups of incoming calls.  Nothing down on them
            if(lastState != TelephonyManager.CALL_STATE_RINGING){
                inCall = true;
                isIncoming = false;
                onOutgoingCallAnswered(/*savedNumber, eventTime*/);
            }
            else
            {
                inCall = true;
                isIncoming = true;
                onIncomingCallAnswered(/*savedNumber, eventTime*/);
            }
            break;
        case TelephonyManager.CALL_STATE_IDLE:
            //PPApplication.logE("PhoneCallReceiver.PhoneCallStartEndDetector", "state=CALL_STATE_IDLE");
            //Went to idle-  this is the end of a call.  What type depends on previous state(s)
            if(!inCall){
                //Ring but no pickup-  a miss
                onMissedCall(/*savedNumber, eventTime*/);
            }
            else 
            {
                if(isIncoming){
                    onIncomingCallEnded(/*savedNumber, eventTime*/);
                }
                else{
                    onOutgoingCallEnded(/*savedNumber, eventTime*/);
                }
                inCall = false;
            }
            break;
    }
    lastState = state;
}