1

I am using following service to play sound in my app when app start sound start and when user click on any button in menu its stop. But I am facing some problems. Like if user open app and without pressing any button in app menu he press menu button on mobile sound not stop. And if app start and call or message is coming on mobile still sound play in background. How to stop service on these 2 events?

Service code-

public class PlayAudio extends Service{
private static final String LOGCAT = null;
MediaPlayer objPlayer;

public void onCreate(){
super.onCreate();
Log.d(LOGCAT, "Service Started!");
objPlayer = MediaPlayer.create(this, R.raw.test);
}

public int onStartCommand(Intent intent, int flags, int startId){
objPlayer.start();
Log.d(LOGCAT, "Media Player started!");
if(objPlayer.isLooping() != true){
Log.d(LOGCAT, "Problem in Playing Audio");
}
return 1;
}

public void onStop(){
objPlayer.stop();
objPlayer.release();
}

public void onPause(){
objPlayer.stop();
objPlayer.release();
}
public void onDestroy(){
objPlayer.stop();
objPlayer.release();
}
@Override
public IBinder onBind(Intent objIndent) {
return null;
}
}

When my app start from splash activity I starting audio like-

@Override
    public void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.splash);
       objIntent = new Intent(this, PlayAudio.class);
       startService(objIntent);
       new Handler().postDelayed(csRunnable2, 5000);  
       }

Then in main menu activity stopping it when user press any button-

hist = (Button) findViewById(R.id.hist);
        hist.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View arg0) {
                objIntent = new Intent(MainActivity.this, PlayAudio.class);
                stopService(objIntent);
                startActivity(new Intent(MainActivity.this, History.class));
                finish();

And please tell me where I have to do changes?

John R
  • 2,078
  • 8
  • 35
  • 58

4 Answers4

2

You should override onStop() and onPause() methods of your Activity like this:

@Override
public void onStop()
{
    super.onStop();
    stopService(new Intent(getApplicationContext(), YourService.class));
}

@Override
public void onPause()
{
    super.onPause();
    stopService(new Intent(getApplicationContext(), YourService.class));
}

Or

@Override
public void onPause()
{
    super.onPause();
    // send a message to service to set sound player on pause(using Handler)
}

 @Override
public void onResume()
{
    super.onResume();
    // send a message to service to resume sound playing
}
floyd
  • 692
  • 8
  • 23
  • try to override onBackPressed() method too: @Override public void onBackPressed() { super.onBackPressed(); stopService(new Intent(getApplicationContext(), YourService.class)); } – floyd Jan 27 '14 at 10:19
  • Try to do the same thing, but call super.onBackPressed() after stopService(new Intent(getApplicationContext(), YourService.class)); – floyd Jan 27 '14 at 10:59
  • http://stackoverflow.com/questions/4273539/android-service-running-after-pressing-home-key – floyd Jan 27 '14 at 11:18
1
start.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View arg0) {
        // TODO Auto-generated method stub
        startService(new Intent(getApplicationContext(), MyService.class));
    }
});

stop.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        stopService(new Intent(getApplicationContext(), MyService.class));
    }
});
0

For your first issue, you can refer to this post,

Android service running after pressing Home key

For your second issue, you should create a receiver that listens for the incoming call.

and in the onReceive() method of the receiver, you could stop the service.

Refer to this question for details about how to create an incoming receiver.

Using this, you can start the service again once the call is disconnected.

Community
  • 1
  • 1
Sahil Mahajan Mj
  • 11,033
  • 8
  • 53
  • 100
0

just call

stopService(new Intent(getApplicationContext(), YourService.class));

this will stops the Your Service

Imtiyaz Khalani
  • 2,037
  • 18
  • 32