这是我之前的问题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
});
答案 0 :(得分:1)
您使用START_STICKY作为服务!这就是它自动重启的原因。尝试使用START_NONSTICKY。