Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 2nd, 2012  |  syntax: None  |  size: 3.37 KB  |  hits: 24  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Android Stop Background Music
  2. player = MediaPlayer.create(this, R.raw.idil);
  3. player.setLooping(true);
  4. player.setVolume(100,100);
  5. player.start();
  6.        
  7. MediaPlayer player;
  8.        
  9. @Override
  10.     protected void onPause() {
  11.         super.onPause();
  12.         if (this.isFinishing()){
  13.             player.stop();
  14.         }
  15.     }
  16.        
  17. @Override
  18. protected void onStop() {
  19.         super.onStop();
  20.         if (this.isFinishing()){
  21.             player.stop();
  22.         }
  23.     }
  24.        
  25. public void onUserLeaveHint() {
  26.     player.stop();
  27.     super.onUserLeaveHint();
  28. }
  29.        
  30. @Override
  31.     protected void onPause() {
  32.         super.onPause();
  33.         if (this.isFinishing()){
  34.             player.stop();
  35.         }
  36.     }
  37.        
  38. @Override
  39.     protected void onPause() {
  40.         super.onPause();
  41.         if (mediaPlayer != null){
  42.             mediaPlayser.stop();
  43.             if (isFinishing()){
  44.             mediaPlayer.stop();
  45.             mediaPlayer.release();
  46.             }
  47.         }
  48.     }
  49.        
  50. <uses-permission android:name="android.permission.GET_TASKS"/>
  51.        
  52. @Override
  53.   protected void onPause() {
  54.     if (this.isFinishing()){ //basically BACK was pressed from this activity
  55.       player.stop();
  56.       Toast.makeText(xYourClassNamex.this, "YOU PRESSED BACK FROM YOUR 'HOME/MAIN' ACTIVITY", Toast.LENGTH_SHORT).show();
  57.     }
  58.     Context context = getApplicationContext();
  59.     ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
  60.     List<RunningTaskInfo> taskInfo = am.getRunningTasks(1);
  61.     if (!taskInfo.isEmpty()) {
  62.       ComponentName topActivity = taskInfo.get(0).topActivity;
  63.       if (!topActivity.getPackageName().equals(context.getPackageName())) {
  64.         player.stop();
  65.         Toast.makeText(xYourClassNamex.this, "YOU LEFT YOUR APP", Toast.LENGTH_SHORT).show();
  66.       }
  67.       else {
  68.         Toast.makeText(xYourClassNamex.this, "YOU SWITCHED ACTIVITIES WITHIN YOUR APP", Toast.LENGTH_SHORT).show();
  69.       }
  70.     }
  71.     super.onPause();
  72.   }
  73.        
  74. Context context = getApplicationContext();
  75.         ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
  76.         List<RunningTaskInfo> taskInfo = am.getRunningTasks(1);
  77.         if (!taskInfo.isEmpty()) {
  78.           ComponentName topActivity = taskInfo.get(0).topActivity;
  79.           if (!topActivity.getPackageName().equals(context.getPackageName())) {
  80.             player.stop();
  81.             Toast.makeText(xYourClassNamex.this, "YOU LEFT YOUR APP", Toast.LENGTH_SHORT).show();
  82.           }
  83.         }
  84.        
  85. private static HashMap<String, Boolean> focus = new HashMap<String, Boolean>();
  86.  
  87. public static void check(Context context, boolean hasFocus)
  88. {
  89.     handler.removeMessages(0);
  90.     focus.put(((Activity)context).getLocalClassName(), hasFocus);
  91.     handler.sendEmptyMessageDelayed(0, 500);
  92. }
  93.  
  94. public static void check(String classname, boolean hasFocus)
  95. {
  96.     handler.removeMessages(0);
  97.     focus.put(classname, hasFocus);
  98.     handler.sendEmptyMessageDelayed(0, 500);
  99. }
  100.  
  101. private static Handler handler = new Handler()
  102. {
  103.     @Override
  104.     public void handleMessage(Message msg)
  105.     {
  106.         Iterator<Boolean> it = focus.values().iterator();
  107.         while(it.hasNext())
  108.         {
  109.             if(it.next())
  110.                 return;
  111.         }
  112.         pause();
  113.         super.handleMessage(msg);
  114.     }
  115. };
  116.        
  117. @Override
  118. public void onWindowFocusChanged(boolean hasFocus)
  119. {
  120.     MusicManager.check(this, hasFocus);
  121.     super.onWindowFocusChanged(hasFocus);
  122. }