How to stop the Background running service in android?
<pre><code>Intent svc=new Intent(BeapSoundActivity.this, BackgroundService.class);
           pendingIntent = PendingIntent.getService(BeapSoundActivity.this, 0, svc, 0);

           AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);

           Calendar calendar = Calendar.getInstance();
           calendar.setTimeInMillis(System.currentTimeMillis());
           calendar.add(Calendar.SECOND, 2);
           alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 10*1000, pendingIntent);
</code></pre>

<p>I am using the above code to start my service in mt onCreate(),</p>

<p>I just want to stop this service when I click a Button to change my Activity</p>

<pre><code>I used this line in my Button Click

    stopService(new Intent(BeapSoundActivity.this, BackgroundService.class));
</code></pre>

<p>But it didnt worked for me </p>

<h2>Here is my BackGroundService class</h2>

<pre><code>import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONObject;

import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.IBinder;
import android.os.StrictMode;
import android.util.Log;

public class BackgroundService extends Service{

//   private static final String TAG = null;
//   MediaPlayer player;
     MediaPlayer mMediaPlayer;
     String  strResponse;
     int i=0;

     static {

//         StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();

           StrictMode.setThreadPolicy( new StrictMode.ThreadPolicy.Builder().permitAll().build());
       }
    @Override
    public IBinder onBind(Intent intent) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        mMediaPlayer = new MediaPlayer();
        Log.e("Oncreate","oncreate");


    }
    public int onStartCommand(Intent intent, int flags, int startId) {
//        player.start();
        Log.e("Onstartcommand","onstartcommand");

            pendingCount();

        return 1;
    }

    public void onStart(Intent intent, int startId) {
        // TO DO
//      super.onStart(intent,startId);



        Log.e("Onstart","onstart");
//      pendingCount();
    }
    public IBinder onUnBind(Intent arg0) {
        // TO DO Auto-generated method
        return null;
    }

    public void onStop() {

    }
    public void onPause() {

    }
    @Override
    public void onDestroy() {
//        player.stop();
//        player.release();
    }

    @Override
    public void onLowMemory() {

    }

    public void playSound(Context context) throws IllegalArgumentException, SecurityException, IllegalStateException,
    IOException {
  Uri soundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
//   mMediaPlayer = new MediaPlayer();
  mMediaPlayer.setDataSource(context, soundUri);
  final AudioManager audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
  if (audioManager.getStreamVolume(AudioManager.STREAM_ALARM) != 0) {
    mMediaPlayer.setAudioStreamType(AudioManager.STREAM_ALARM);
    mMediaPlayer.setLooping(true);
    mMediaPlayer.prepare();
    mMediaPlayer.start();

}
}

    public void pendingCount(){
         DefaultHttpClient httpClient = new DefaultHttpClient();


           HttpGet post = new HttpGet("http://mybusinessapp2.testshell.net/api/Orders");

           post.addHeader("UserName", "info@mybusinessapp.com");
         post.addHeader("Password", "c7d7cdf598be88a21a477842ebfa5ca5");

         ResponseHandler&lt;String&gt; resHandler = new BasicResponseHandler();

              String post_response = null;
            try {
                post_response = httpClient.execute(post,resHandler);
            } catch (ClientProtocolException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
              Log.e("resp",post_response);

              try{
                    JSONObject jobject=new JSONObject(post_response);

                     strResponse= jobject.getString("Count");


              }
              catch(Exception e){
                  e.printStackTrace();
              }

              Log.e("tv",""+BeapSoundActivity. pending_number);
             try{ 
               BeapSoundActivity. pending_number.setText(strResponse);

//              int pending=Integer.parseInt(BeapSoundActivity.pending_number.getText().toString());

//               Log.e("counter",""+pending);
             }
             catch(NullPointerException e){
                 e.printStackTrace();
             }
             int pending=Integer.parseInt( strResponse);
                 if(pending==0){

                      mMediaPlayer.stop();
                      mMediaPlayer = new MediaPlayer();
                 }


                 if(pending&gt;0){


                        try {
                            playSound(BackgroundService.this);
                        } catch (IOException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                    catch (IllegalArgumentException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    } catch (SecurityException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    } catch (IllegalStateException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();



                 }
                 }


    }


}
</code></pre>

<p>Please help me in solving this issue</p>

<p>Thanks in advance </p>
