我正在開發一個android音樂播放器應用程序。我只是想暫停當一個電話到達,這已經通過使用電話管理器,但我也想檢測WhatsApp電話和其他互聯網電話。如何做到這一點
這是我的代碼更改,應採取什麼措施也發現互聯網通話暫停呼叫播放器和電話上播放結束檢測WhatsApp電話和暫停ExoPlayer
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);
}
}
whatsapp調用可能是應用程序的內部狀態,它可能不會被捕獲爲全局CALL狀態。 –
只要您的活動失去焦點,就會觸發'onPause',因此您可以在'onPause'中處理此問題嗎? –
@sunilsunny'onPause'方法不能處理這個,因爲如果我們點擊手機的home按鈕,那麼這個方法也會被調用,我的播放器也應該是一個後臺播放器 –