我正在开发一个Android音乐播放器应用程序。我只是想在呼叫到达时暂停,这是通过使用Telephony Manager实现的,但我也想检测Whatsapp呼叫和其他互联网呼叫。如何制作可能的 这是我的代码应该做些什么更改来检测互联网电话还暂停播放器的通话并在通话结束时播放
private void player_manager_on_call() {
// TODO Auto-generated method stub
// Manage player on Call
phoneStateListener = new PhoneStateListener() {
@Override
public void onCallStateChanged(int state, String incomingNumber) {
if (state == TelephonyManager.CALL_STATE_RINGING)
// Incoming // call: // Pause // music
{
try {
boolean is_playing = mediaController.Check_IsPlaying();
if (is_playing) {
Log.d("xiomi", "CALL_STATE_RINGING");
// Get details of song from sp
position = PlayerConstants.SONG_NUMBER;
data = PlayerConstants.SONGS_LIST.get(position);
Play_link = data.getPath();
Artist = data.getArtist() + "-" + data.getAlbum();
albumArt = UtilFunctions.getAlbumart(
getApplicationContext(), data.getAlbumId());
Log.e("albumArt", "albumArt " + albumArt);
mediaController.doPauseResume();
showControls();
// start notification service again for play
startNotificationService(MainActivity.this,
data.getTitle(), Artist, albumArt,
is_playing, position);
}
} catch (Exception e) { // TODO Auto-generated catch block
e.printStackTrace();
}
} else if (state == TelephonyManager.CALL_STATE_IDLE) // Not in
// //call:
// //
// Play
// //
// music
try {
if (player != null) {
Log.d("xiomi", "CALL_STATE_RINGING");
// Get details of song from sp
position = PlayerConstants.SONG_NUMBER;
data = PlayerConstants.SONGS_LIST.get(position);
Play_link = data.getPath();
Artist = data.getArtist() + "-" + data.getAlbum();
albumArt = UtilFunctions.getAlbumart(
getApplicationContext(), data.getAlbumId());
Log.e("albumArt", "albumArt " + albumArt);
boolean is_playing = mediaController
.Check_IsPlaying();
mediaController.doPauseResume();
showControls();
// start notification service again for play
startNotificationService(MainActivity.this,
data.getTitle(), Artist, albumArt,
is_playing, position);
}
} catch (Exception e) { // TODO Auto-generated catch block
e.printStackTrace();
}
else if (state == TelephonyManager.CALL_STATE_OFFHOOK) // A //
// call
// is // dialing, // active // or // on // hold
{
try {
boolean is_playing = mediaController.Check_IsPlaying();
if (is_playing) {
Log.d("xiomi", "CALL_STATE_OFFHOOK");
// Get details of song from sp
position = PlayerConstants.SONG_NUMBER;
data = PlayerConstants.SONGS_LIST.get(position);
Play_link = data.getPath();
Artist = data.getArtist() + "-" + data.getAlbum();
albumArt = UtilFunctions.getAlbumart(
getApplicationContext(), data.getAlbumId());
Log.e("albumArt", "albumArt " + albumArt);
mediaController.doPauseResume();
showControls();
// start notification service again for play
startNotificationService(MainActivity.this,
data.getTitle(), Artist, albumArt,
is_playing, position);
}
} catch (Exception e) { // TODO Auto-generated catch block
e.printStackTrace();
}
}
super.onCallStateChanged(state, incomingNumber);
}
};
TelephonyManager mgr = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
if (mgr != null) {
mgr.listen(phoneStateListener, PhoneStateListener.LISTEN_CALL_STATE);
}
}
答案 0 :(得分:0)
我会避免从Android文档中处理代码,所以我只会给你一些应该回答你问题的链接。
首先,您不需要自己检测来自不同应用的呼叫,您只需要注册广播接收器并根据音频焦点变化进行操作。
在这里,您可以查看耳机断开时如何处理: https://developer.android.com/guide/topics/media-apps/volume-and-earphones.html
在下面的链接中,您可以看到有关如何处理呼叫等问题的答案。 https://developer.android.com/guide/topics/media-apps/audio-focus.html