- Android Stop Background Music
- player = MediaPlayer.create(this, R.raw.idil);
- player.setLooping(true);
- player.setVolume(100,100);
- player.start();
- MediaPlayer player;
- @Override
- protected void onPause() {
- super.onPause();
- if (this.isFinishing()){
- player.stop();
- }
- }
- @Override
- protected void onStop() {
- super.onStop();
- if (this.isFinishing()){
- player.stop();
- }
- }
- public void onUserLeaveHint() {
- player.stop();
- super.onUserLeaveHint();
- }
- @Override
- protected void onPause() {
- super.onPause();
- if (this.isFinishing()){
- player.stop();
- }
- }
- @Override
- protected void onPause() {
- super.onPause();
- if (mediaPlayer != null){
- mediaPlayser.stop();
- if (isFinishing()){
- mediaPlayer.stop();
- mediaPlayer.release();
- }
- }
- }
- <uses-permission android:name="android.permission.GET_TASKS"/>
- @Override
- protected void onPause() {
- if (this.isFinishing()){ //basically BACK was pressed from this activity
- player.stop();
- Toast.makeText(xYourClassNamex.this, "YOU PRESSED BACK FROM YOUR 'HOME/MAIN' ACTIVITY", Toast.LENGTH_SHORT).show();
- }
- Context context = getApplicationContext();
- ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
- List<RunningTaskInfo> taskInfo = am.getRunningTasks(1);
- if (!taskInfo.isEmpty()) {
- ComponentName topActivity = taskInfo.get(0).topActivity;
- if (!topActivity.getPackageName().equals(context.getPackageName())) {
- player.stop();
- Toast.makeText(xYourClassNamex.this, "YOU LEFT YOUR APP", Toast.LENGTH_SHORT).show();
- }
- else {
- Toast.makeText(xYourClassNamex.this, "YOU SWITCHED ACTIVITIES WITHIN YOUR APP", Toast.LENGTH_SHORT).show();
- }
- }
- super.onPause();
- }
- Context context = getApplicationContext();
- ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
- List<RunningTaskInfo> taskInfo = am.getRunningTasks(1);
- if (!taskInfo.isEmpty()) {
- ComponentName topActivity = taskInfo.get(0).topActivity;
- if (!topActivity.getPackageName().equals(context.getPackageName())) {
- player.stop();
- Toast.makeText(xYourClassNamex.this, "YOU LEFT YOUR APP", Toast.LENGTH_SHORT).show();
- }
- }
- private static HashMap<String, Boolean> focus = new HashMap<String, Boolean>();
- public static void check(Context context, boolean hasFocus)
- {
- handler.removeMessages(0);
- focus.put(((Activity)context).getLocalClassName(), hasFocus);
- handler.sendEmptyMessageDelayed(0, 500);
- }
- public static void check(String classname, boolean hasFocus)
- {
- handler.removeMessages(0);
- focus.put(classname, hasFocus);
- handler.sendEmptyMessageDelayed(0, 500);
- }
- private static Handler handler = new Handler()
- {
- @Override
- public void handleMessage(Message msg)
- {
- Iterator<Boolean> it = focus.values().iterator();
- while(it.hasNext())
- {
- if(it.next())
- return;
- }
- pause();
- super.handleMessage(msg);
- }
- };
- @Override
- public void onWindowFocusChanged(boolean hasFocus)
- {
- MusicManager.check(this, hasFocus);
- super.onWindowFocusChanged(hasFocus);
- }