在我的 Android Java 后台服务中需要帮助以在检测到呼叫时停止

Need help in my Android Java background service to stop when call detected

我为我的一个项目创建了背景流音乐播放服务。它工作正常,但在播放时我接到了一个电话,媒体并没有像其他应用程序那样停止。另外,当我接听电话时,音乐还在播放。

package mypackage;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.os.IBinder;
import android.support.v7.app.NotificationCompat;
import android.widget.Toast;

import mypackage.R;

public class StreamPlayer extends Service implements OnCompletionListener {
    MediaPlayer mediaPlayer;
    Notification noti;
    android.support.v4.app.NotificationCompat.Builder noticom;
    NotificationManager notiman;

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onCreate() {

        mediaPlayer = new MediaPlayer();
        mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
        mediaPlayer.setOnBufferingUpdateListener(new MediaPlayer.OnBufferingUpdateListener() {
            @Override
            public void onBufferingUpdate(MediaPlayer mp, int percent) {
                Toast.makeText(StreamPlayer.this, "Buffering", Toast.LENGTH_LONG).show();
            }
        });
        try {
            mediaPlayer.setDataSource("http://149.56.185.83:8138/stream");
            mediaPlayer.prepare();
        }catch (Exception ex){
        }
        mediaPlayer.setOnCompletionListener(this);
        noticom = new NotificationCompat.Builder(this)
                .setSmallIcon(R.mipmap.ic_launcher)
                .setCategory(MEDIA_SESSION_SERVICE)
                .setAutoCancel(true)
                .setContentTitle("Sample Streamed Music Player")
                .setOngoing(true)
                .setPriority(NotificationCompat.PRIORITY_HIGH)
                .setContentText("Now Playing")
                .setCategory(NotificationCompat.CATEGORY_EVENT);
        notiman = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    }


    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        if (!mediaPlayer.isPlaying()) {
            mediaPlayer.start();
            Toast.makeText(StreamPlayer.this, "Now Playing", Toast.LENGTH_SHORT).show();
            noti = noticom.build();
            notiman.notify(2017,noti);
        }
        return START_STICKY;
    }

    public void onDestroy() {
        if (mediaPlayer.isPlaying()) {
            mediaPlayer.stop();
            noticom.setOngoing(false);
            noticom.setContentText("Stopped");
            Toast.makeText(StreamPlayer.this, "Stopped", Toast.LENGTH_SHORT).show();
            noti = noticom.build();
            notiman.notify(2017,noti);
        }
        mediaPlayer.release();
    }

    public void onCompletion(MediaPlayer _mediaPlayer) {
        stopSelf();
    }

}

有人知道如何解决这个问题吗?

首先return它作为Service.START_REDELIVER_INTENT; 第二次调用 startForeground(int id, notificationFunction());

这可以是 notificationFunction()

    Notification notificationFunction(){
      Notification.Builder mBuilder = new Notification.Builder(this);

/*customize notification*/

            NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
            return mBuilder.build();
    }

对于音乐播放器,您还需要 check this

我找到了解决方案。 感谢您的支持。 :)

public class MyActivity extends Activity implements OnAudioFocusChangeListener {

private AudioManager mAudioManager;

@Override
public void onCreate(Bundle savedInstanceState) {
    ...
    mAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
    mAudioManager.requestAudioFocus(this, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN);
    ...
}

@Override
 public void onDestroy(){
     super.onDestroy();
     ...
     mAudioManager.abandonAudioFocus(this);
     ...
 }


@Override
public void onAudioFocusChange(int focusChange) {
    if(focusChange<=0) {
        //LOSS -> PAUSE
    } else {
        //GAIN -> PLAY
    }
}