如何在广播接收器呼叫一个号码或处于 OFFHOOK 状态时开始播放音频歌曲,并在 IDLE 或通话结束时停止播放。
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.telephony.TelephonyManager;
import android.widget.Toast;
import android.media.MediaPlayer;
public class Ringing extends BroadcastReceiver{
Context context;
public static MediaPlayer ob=null;
@Override
public void onReceive(Context context, Intent intent){
String state = intent.getStringExtra(TelephonyManager.EXTRA_STATE);
if(state.equals(TelephonyManager.EXTRA_STATE_OFFHOOK)){
// Toast.makeText(context, "Call Recieved", Toast.LENGTH_LONG).show();
ob = MediaPlayer.create(context,R.raw.trouble);
ob.start();
}
if(state.equals(TelephonyManager.EXTRA_STATE_IDLE)){
if(ob.isPlaying()) {
ob.stop();
ob.destroy();
}
}
}
}