This is continuation of my previous question Android: Music Player gets started itself after sometime. In my app, I am playing media player in background using service. It works fine in all conditions that I have checked for incoming and outgoing calls. The service gets destroyed in completion as well. When user pauses the song explicitly, media player pauses, but after sometime, it gets started by itself. From the log, I got that call state idle was detected, when this happened. I want to avoid this. I am posting my code below:
Music Service
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
The button code on which I am starting the service:
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
});
You are using START_STICKY as your service ! That is the reason it restarts automatically. Try using START_NONSTICKY.