title: Implement shuffle button for music player android app
body: <p>I am trying to get my shuffle button working. But I have been unsuccessful. I commented out the code for that functionality. I am somewhat new to java Android Development and need some guidance understanding what the problem could be. Thank you for your time to help me on this.</p>
  
  <pre><code>public class MainActivity extends Activity{
  private boolean mIsBound = false;
  private MusicService mServ;
  private ServiceConnection Scon ;
  Button playbutton;//play button
  Button pausebutton;//pause button
  Button stopbutton;//stop button
  Button prevbutton;//prev button
  Button nextbutton;//next button
  Button randombutton;//random button
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      //      this.requestWindowFeature(Window.FEATURE_NO_TITLE);
      setContentView(R.layout.playinterface);
  
      Scon =new ServiceConnection(){
  
          public void onServiceConnected(ComponentName name, IBinder
                  binder) {
              mServ = ((new MusicService().new ServiceBinder()).getService());
          }
  
          public void onServiceDisconnected(ComponentName name) {
              mServ = null;
          }
      };
  
  
  
  //      mServ.pauseMusic();
      //play button
      playbutton = (Button) findViewById(R.id.button1);
      playbutton.setOnClickListener(new OnClickListener() {
          public void onClick(View v) {
  //              mp3_play();//mp3 play
  //              
              Intent music = new Intent();
              music.setClass(MainActivity.this,MusicService.class);
  
              startService(music);
              //button enable,disabel set
              playbutton.setEnabled(false);
              playbutton.setText("Playing");
              pausebutton.setEnabled(true);
              stopbutton.setEnabled(true);
              prevbutton.setEnabled(true);
              nextbutton.setEnabled(true);
              randombutton.setEnabled(true);
  //              if(mServ!=null){
  //                  mServ.mPlayer.start();
  //              }else{
  //                  ActivityManager manager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
  //                  for (RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
  //                        String serv = service.service.getClassName();
  //                        Log.e("Danial",serv);
  //                  }
  //              }
          }
      });
  
      //pause button
      pausebutton = (Button) findViewById(R.id.button2);
      pausebutton.setOnClickListener(new OnClickListener() {
          public void onClick(View v) {
  //              mp3_pause();
              Intent music = new Intent();
              music.setClass(MainActivity.this,MusicService.class);
  
              stopService(music);
              //button enable,disabel set
              playbutton.setEnabled(true);
              playbutton.setText("Play");
              pausebutton.setEnabled(false);
              stopbutton.setEnabled(false);
              prevbutton.setEnabled(false);
              nextbutton.setEnabled(false);
              randombutton.setEnabled(false);
          }
      });
  
     //stop button
      stopbutton = (Button) findViewById(R.id.button3);
      stopbutton.setOnClickListener(new OnClickListener() {
          public void onClick(View v) {
  //              mp3_stop();
              Intent music = new Intent();
              music.setClass(MainActivity.this,MusicService.class);
  
              stopService(music);
              //button enable,disabel set
              playbutton.setEnabled(true);
              playbutton.setText("Play");
              pausebutton.setEnabled(false);
              stopbutton.setEnabled(false);
              prevbutton.setEnabled(false);
              nextbutton.setEnabled(false);
              randombutton.setEnabled(false);
          }
      });
  
  //        //Prev button
      prevbutton = (Button) findViewById(R.id.button4);
  //        prevbutton.setOnClickListener(new View.OnClickListener() {
  //          public void onClick(View v) {
  //              
  //              if(randomStart == true)//when play random
  //              {
  //                  Random rnd = new Random();
  //                  int randIndex = rnd.nextInt(mp3_num);               
  //                  if(randIndex == index_mp3)
  //                  {
  //                      if(randIndex == mp3_num-1)
  //                          randIndex--;
  //                      if(randIndex == 0)
  //                          randIndex++;
  //                  }
  //                  
  //                  index_mp3 = randIndex;
  //                  mp3_play();//mp3 play
  //                  return;
  //              }
  //              
  //              if(randomStart == false)//when play into order
  //              {
   //                 if(index_mp3 &gt; 0)
  //                  {
  //                      index_mp3--;//select pre mp3
  //                      mp3_play();//mp3 play
  //                  }
  //                  else{
  //                      Toast.makeText(getActivity(), 
  //                              "There is not prev mp3!", Toast.LENGTH_LONG).show();
  //                  }
  //                  return;
  //              }
  //          }
  //      });
  
  //        //Next button
      nextbutton = (Button) findViewById(R.id.button5);
  //        nextbutton.setOnClickListener(new View.OnClickListener() {
  //          public void onClick(View v) {
  //              if(randomStart == true)//when play random
  //              {
  //                  Random rnd = new Random();
  //                  int randIndex = rnd.nextInt(mp3_num);               
  //                  if(randIndex == index_mp3)
  //                  {
  //                      if(randIndex == mp3_num-1)
  //                          randIndex--;
  //                      if(randIndex == 0)
  //                          randIndex++;
  //                  }
  //                  
  //                  index_mp3 = randIndex;
  //                  mp3_play();//mp3 play
  //                  return;
  //              }
  //              
  //              if(randomStart == false)//wehn play into order
  //              {
  //                  if(index_mp3 &lt; mp3_num -1)
  //                  {
  //                      index_mp3++;//select next mp3
  //                      mp3_play();//mp3 play
  //                  }
  //                  else{
  //                      Toast.makeText(getActivity(), 
  //                              "There is not next mp3!", Toast.LENGTH_LONG).show();
  //                  }
  //                  return;
  //              }
  //          }
  //      });
  
  //        //radmom button
      randombutton = (Button) findViewById(R.id.button6);
  //        randombutton.setOnClickListener(new View.OnClickListener() {
  //          public void onClick(View v) {
  //              if(randomStart == false)
  //              {
  //                  randomStart = true;//start playing random
  //                  randombutton.setText("Shuffle On");
  //                  return;
  //              }
  //              
  //              if(randomStart == true)
  //              {
  //                  randomStart = false;//No playing random
  //                  randombutton.setText("Shuffle Off");
  //                  return;
  //              }
  //          }
  //      });
  
      //pause,stop button disable
      pausebutton.setEnabled(false);
      stopbutton.setEnabled(false);
  //        prevbutton.setEnabled(false);
  //        nextbutton.setEnabled(false);
  //        randombutton.setEnabled(false);
  
  
      ActivityManager manager = (ActivityManager)     getSystemService(Context.ACTIVITY_SERVICE);
      for (RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
          if    (MusicService.class.getName().equals(service.service.getClassName())) {
              playbutton.setEnabled(false);
              playbutton.setText("Playing");
              pausebutton.setEnabled(true);
              stopbutton.setEnabled(true);
              prevbutton.setEnabled(true);
              nextbutton.setEnabled(true);
              randombutton.setEnabled(true);
          }
      }
  
  
      //      //Go to the Fragment
      //      changeFragment(
      //              "PlayInterface", 
      //              R.id.frameRootContainer, 
      //              new MainActivityFragment(), 
      //              false);
  
  
  }
  
  void doBindService(){
      bindService(new Intent(this,MusicService.class),
              Scon,Context.BIND_AUTO_CREATE);
      mIsBound = true;
  }
  
  void doUnbindService()
  {
      if(mIsBound)
      {
          unbindService(Scon);
          mIsBound = false;
      }
  }
  @Override
  public boolean onCreateOptionsMenu(Menu menu) {
      // Inflate the menu; this adds items to the action bar if it is present.
      getMenuInflater().inflate(R.menu.main, menu);
      return true;
  }
  
  @Override
  public void onResume() {
      super.onResume();
  
  } 
  
  @Override
  protected void onStop() {
      // TODO Auto-generated method stub
      super.onStop();
  //      mServ.pauseMusic();
  //      mServ.resumeMusic();
  //      mServ.stopMusic();
  }
  @Override
  protected void onDestroy() {
      // TODO Auto-generated method stub
      super.onDestroy();
  }
  //  public void changeFragment(String fragmentTag, int layoutId, Fragment frag, boolean addToBackStack) {
  //      
  //      FragmentManager fm = getSupportFragmentManager();//Getting Fragment handle
  //        FragmentTransaction transaction = fm.beginTransaction();//Starting Fragment
  //        
  //        transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);//Setting option
  //      transaction.replace(layoutId, frag, fragmentTag);//Replace the fragment
  //      
  //      if(addToBackStack)//put fragment on the interface
  //          transaction.addToBackStack(null);
  //      
  //      transaction.commit();//Run
  //  }   
  </code></pre>
  
  <p>}</p>