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
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
© 2024 OneMinuteCode. All rights reserved.