-1

这是我之前的问题Android: Music Player gets started itself after sometime的延续。在我的应用程序中,我正在使用服务在后台播放媒体播放器。在我检查来电和去电的所有情况下,它都能正常工作。该服务也会在完成时被破坏。当用户明确暂停歌曲时,媒体播放器会暂停,但一段时间后,它会自行启动。从日志中,我发现在发生这种情况时检测到呼叫状态空闲。我想避免这种情况。我在下面发布我的代码:

音乐服务

public class ChalisaService extends Service implements OnCompletionListener
{
static MediaPlayer mediaPlayer;
static int playerFlag = 0;
ActivityManager actManager;
/**
 * 0 for stop/pause
 * 1 for play*/
@Override
public IBinder onBind(Intent intent) 
{
    return null;
}//onBind

@Override
public void onCreate() 
{
    super.onCreate();

    mediaPlayer = MediaPlayer.create(getApplicationContext(), R.raw.chalisa);
    mediaPlayer.setVolume(100, 100);
    mediaPlayer.setOnCompletionListener(this);
    registerReceiver(CallStateReceiver, new IntentFilter("android.intent.action.PHONE_STATE"));
    Log.v("Chalisa service onCreate", "onCreate called");
}//onCreate

@Override
public int onStartCommand(Intent intent, int flags, int startId) 
{
    if(!mediaPlayer.isPlaying())
    {
        mediaPlayer.start();
        playerFlag = 1;
    }//if
    startForeground(0, null);
    Log.v("Chalisa service onStartCommand", "onStartCommand called");
    return START_STICKY;
}//onStartCommand

@Override
public void onDestroy() 
{
    super.onDestroy();
    if (mediaPlayer.isPlaying()) 
    {
          mediaPlayer.stop();
          playerFlag = 0;
    }//if
    unregisterReceiver(CallStateReceiver);
    mediaPlayer.release();
    Log.v("Chalisa service onDestroy", "onDestroy called");
}//onDestroy



public void onCompletion(MediaPlayer mp) 
{
    this.stopSelf();
    playerFlag = 0;
    updateUI();
    Log.v("Chalisa Service media player", "on completion listener called");
}

private void updateUI() 
{
    Intent in = new Intent("com.dzo.HanumanChalisaWithAudioAndAlarm.UPDATE_UI");
    in.putExtra("Player_FLAG_VALUE", playerFlag);
    getApplicationContext().sendBroadcast(in);
}

public final BroadcastReceiver CallStateReceiver = new BroadcastReceiver()
{
    @Override
    public void onReceive(Context context, Intent intent) 
    {
        String action = intent.getAction();
        if(action.equalsIgnoreCase("android.intent.action.PHONE_STATE"))
        {
            if (intent.getStringExtra(TelephonyManager.EXTRA_STATE)
                    .equals(TelephonyManager.EXTRA_STATE_RINGING))
            {
                if(mediaPlayer.isPlaying())
                {
                    mediaPlayer.pause();
                    playerFlag = 0;
                }//if
                Log.v("Chalisa Service call state ringing", "call state ringing");
            }//if
            else if (intent.getStringExtra(TelephonyManager.EXTRA_STATE)
                    .equals(TelephonyManager.EXTRA_STATE_IDLE))
            {
                if(!mediaPlayer.isPlaying())
                {
                    mediaPlayer.start();
                    playerFlag = 1;
                }//if
                Log.v("Chalisa Service call state idle", "call state idle");
            }//if
            else if (intent.getStringExtra(TelephonyManager.EXTRA_STATE)
                    .equals(TelephonyManager.EXTRA_STATE_OFFHOOK)) 
            {
                if(mediaPlayer.isPlaying())
                {
                    mediaPlayer.pause();
                    playerFlag = 0;
                }//if
                Log.v("Chalisa Service call state offhook", "call state offhook");
            }//if
        }//if             
    }//onReceive
};
}//ChalisaService

我启动服务的按钮代码:

btn_Play.setOnClickListener(new View.OnClickListener() 
    {
        public void onClick(View v) 
        {
            if(ChalisaService.playerFlag == 0)
            {
                startService(in);
                ChalisaService.playerFlag = 1;
                Log.v("HanuAlarm play button if", "in if loop");
                txtPlay.setText("Pause");
                txtPlay.setTextColor(getResources().getColor(R.color.redwine));
                btn_Play.setBackgroundResource(R.drawable.btnpause);
            }//if
            else if(ChalisaService.playerFlag == 1)
            {
                ChalisaService.mediaPlayer.pause();
                ChalisaService.playerFlag = 0;
                Log.v("HanuAlarm play button else", "in else loop");
                txtPlay.setText("Play");
                txtPlay.setTextColor(getResources().getColor(R.color.white));
                btn_Play.setBackgroundResource(R.drawable.btnplay_a);
            }//else if
        }//onClick
    });
4

1 回答 1

1

您正在使用 START_STICKY 作为您的服务!这就是它自动重新启动的原因。尝试使用 START_NONSTICKY。

于 2012-10-05T06:17:32.783 回答