Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8641009
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T11:25:02+00:00 2026-06-12T11:25:02+00:00

This is continuation of my previous question Android: Music Player gets started itself after

  • 0

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
    });
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T11:25:03+00:00Added an answer on June 12, 2026 at 11:25 am

    You are using START_STICKY as your service ! That is the reason it restarts automatically. Try using START_NONSTICKY.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This question is continuation of my previous question. The link is https://stackoverflow.com/questions/12499941/android-getting-wrong-info-on-parsing-json-data#comment16829891_12499941 The problem
This is a continuation of my previous question .NET regex engine returns no matches
This question is a continuation of my previous question here zend models architecture (big
This question is in continuation to my previous question . I am trying to
This is in continuation to my previous question which was closed cause of similarity
This is a continuation of my previous question which I posted when I wasn't
This question is in continuation to my previous question, in which I asked about
This is a continuation from a previous stackoverflow question. I've renamed some variables so
This question is a continuation of a previous thread to compare two lists with
This is a continuation of my previous question: Could not find an implementation of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.