How to check when an app calls you

Asked 2 years ago, Updated 2 years ago, 103 views

I'm developing a video player app. It's okay to play the video, but I'm playing the video I have to pause the video when I get a call, how do I know I got a call?

android broadcastreceiver

2022-09-22 22:07

1 Answers

import android.app.Activity;
import android.os.Bundle;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.widget.Toast;

public class PhoneListenerTestActivity extends Activity {

    PhoneStateCheckListener phoneCheckListener;



    @Override

    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.main);



        phoneCheckListener = new PhoneStateCheckListener(this);



        TelephonyManager telephonyManager  =

(TelephonyManager)getSystemService(TELEPHONY_SERVICE); 

        telephonyManager.listen(phoneCheckListener,

PhoneStateListener.LISTEN_CALL_STATE); 

    }



    public class PhoneStateCheckListener extends PhoneStateListener {

        PhoneListenerTestActivity mainActivity;

        PhoneStateCheckListener(PhoneListenerTestActivity _main){

            mainActivity = _main;

        }


    @Override

    public void onCallStateChanged(int state, String incomingNumber) {

    if (state == TelephonyManager.CALL_STATE_IDLE) {

    Toast.makeText(mainActivity,"STATE_IDLE : Incoming number " 

+ + incomingNumber,Toast.LENGTH_SHORT).show();

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

    Toast.makeText(mainActivity,"STATE_RINGING : Incoming number "

 + + incomingNumber,Toast.LENGTH_SHORT).show();

//This is the receiving part.

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

        Toast.makeText(mainActivity,"STATE_OFFHOOK : Incoming number " 

+ + incomingNumber,Toast.LENGTH_SHORT).show();

    }

    }

    }     

}

Go to the android manifest file

    <uses-permission android:name="android.permission.READ_PHONE_STATE"/>

You have to put this in.

Source: http://jwandroid.tistory.com/152


2022-09-22 22:07

If you have any answers or tips

Popular Tags
python x 4647
android x 1593
java x 1494
javascript x 1427
c x 927
c++ x 878
ruby-on-rails x 696
php x 692
python3 x 685
html x 656

© 2024 OneMinuteCode. All rights reserved.