Android Stop Background Music

前端 未结 9 966
生来不讨喜
生来不讨喜 2020-12-01 02:29

EDIT - FOUND A EASY 5-10 LINE SOLUTION!!! See MY OWN ANSWER BELOW!!! YAY!!!!!!!!!

I\'ve searched for 5 hours, dozens of SO posts, no answers, and this seems like t

9条回答
  •  情歌与酒
    2020-12-01 02:52

    I'm very happy today, and still have hair :) I've tested this and it works!!!

    First, add this to your Manifest:

    
    

    Second, add this to your 'Home/Main' Activity/Class:

      @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 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();
      }
    

    And obviously replace xYourClassNamex with, well, Your Class Name :)

    Now obviously you do not need the "Toasts" but it will tell you what is going on. The very intersting thind is when you press BACK from your 'Home/Main' activity, you obviously get 2 Toasts, "YOU PRESSED BACK FROM YOUR 'HOME/MAIN' ACTIVITY", and the 2nd Toast is "YOU SWITCHED ACTIVITIES WITHIN YOUR APP". I believe I know why this happens, but it doesn;t matter because I call "player.stop();" from the 2 scenarios that mean my app is no longer being 'used'. Obviously do more work than "player.stop();" if you need to :) And also obvious you dont need the "else" for "YOU SWITCHED ACTIVITIES WITHIN YOUR APP", because there is no reason to "stop/pause" the background music, which is what i needed, but if you DO need to do something when new activities are started, well here you go :)

    Hope this helps anyone looking to know when the user "leaves/exits/is done with" the app :)

    THANKS FOR ALL OF THE COMMENTS POSTS AND HELP EVERYONE!!! YAY!

    EDIT-

    This part has to be in EVERY activity's onPause:

    Context context = getApplicationContext();
            ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
            List 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();
              }
            }
    

    so you'll know if the user left your app from ANY of the activities. this is good to know :)

提交回复
热议问题