Forum Xamarin.Android

Announcement:

The Xamarin Forums have officially moved to the new Microsoft Q&A experience. Microsoft Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

To create new threads and ask questions head over to Microsoft Q&A for .NET and get involved today.

how to call the method when homebutton is pressed?

BayuBayu Member ✭✭✭
edited April 2020 in Xamarin.Android

I make a simple App. I add a music in the background. I want to stop the music when the home button is pressed, so I use the home button detect interface as refer to thread https://forums.xamarin.com/discussion/comment/409059#Comment_409059.

in my main activity I use the code below

        using Android.App;
        using Android.OS;
        using Android.Support.V7.App;
        using Android.Runtime;
        using Android.Widget;
        using Android.Media;
        namespace tombolhome
        {
            [Activity(Label = "@string/app_name", Theme = "@style/AppTheme", MainLauncher = true)]
            public class MainActivity : AppCompatActivity
            {
                MediaPlayer player;
                protected override void OnCreate(Bundle savedInstanceState)
                {
                    base.OnCreate(savedInstanceState);
                    Xamarin.Essentials.Platform.Init(this, savedInstanceState);
                    // Set our view from the "main" layout resource
                    SetContentView(Resource.Layout.activity_main);
                    player = MediaPlayer.Create(this, Resource.Raw.a);

                    MyHomeImpleWatcher myHomeImpleWatcher = new MyHomeImpleWatcher();

                    HomeWatcher mHomeWatcher = new HomeWatcher(this);
                    mHomeWatcher.setOnHomePressedListener(myHomeImpleWatcher);
                    mHomeWatcher.startWatch();
                }
                public override void OnRequestPermissionsResult(int requestCode, string[] permissions, [GeneratedEnum] Android.Content.PM.Permission[] grantResults)
            {
                Xamarin.Essentials.Platform.OnRequestPermissionsResult(requestCode, permissions, grantResults);

                base.OnRequestPermissionsResult(requestCode, permissions, grantResults);
            }

            protected override void OnStart()
            {
                base.OnStart();
                player.Start();
            }



        }

            class MyHomeImpleWatcher : OnHomePressedListener
            {
                public void onHomeLongPressed()
                {
                    System.Diagnostics.Debug.WriteLine("onHomePressed is pressed A");
               ** //I want stop the music when home pressed, how to call the method here?**
                }
                public void onHomePressed()
                {
                System.Diagnostics.Debug.WriteLine("onHomePressed is pressed B");
** //I want stop the music when home pressed, how to call the method here?**
                }
             }
    }

how to call the method to stop my music? thanks :)

Best Answer

Answers

  • jezhjezh Member, Xamarin Team Xamurai

    Have you tried to added code player.Stop(); in function public void onHomeLongPressed() ?

  • BayuBayu Member ✭✭✭

    yes, I have but I cant call the player.Stop(); in function public void onHomeLongPressed() @jezh

    @jezh said:
    Have you tried to added code player.Stop(); in function public void onHomeLongPressed() ?

  • jezhjezh Member, Xamarin Team Xamurai

    If it is convenient for you , could you please post a basic demo so that we can help you better?

  • BayuBayu Member ✭✭✭

    okay, of course thank you @jezh,

    1. I add music in raw folder, the name file is a.mp3, Resource>>> raw>>> a.mp3
    2. I make user Interface OnHomePressedListener this is the code
      using System;
      using System.Collections.Generic;
      using System.Linq;
      using System.Text;

          using Android.App;
          using Android.Content;
          using Android.OS;
          using Android.Runtime;
          using Android.Views;
          using Android.Widget;
      
          namespace `HomeButton`
          {
              interface OnHomePressedListener
              {
                  void onHomePressed();
                  void onHomeLongPressed();
              }
          }
      
      1. Then, I define a class HomeWatcher, here is the code:
        using System;
        using System.Collections.Generic;
        using System.Linq;
        using System.Text;

        using Android.App;
        using Android.Content;
        using Android.OS;
        using Android.Runtime;
        using Android.Util;
        using Android.Views;
        using Android.Widget;
        
        namespace HomeButton
        {
            class HomeWatcher
            {
                const String TAG = "hg";
                private Context mContext;
                private IntentFilter mFilter;
                private OnHomePressedListener mListener;
                private InnerReceiver mReceiver;
        
        
                public HomeWatcher(Context context)
                {
                    mContext = context;
                    mFilter = new IntentFilter(Intent.ActionCloseSystemDialogs);
                }
        
                public void setOnHomePressedListener(OnHomePressedListener listener)
                {
                    mListener = listener;
                    mReceiver = new InnerReceiver(mListener);
                }
        
                public void startWatch()
                {
                    if (mReceiver != null)
                    {
                        mContext.RegisterReceiver(mReceiver, mFilter);
                    }
                }
        
                public void stopWatch()
                {
                    if (mReceiver != null)
                    {
                        mContext.UnregisterReceiver(mReceiver);
                    }
                }
        
                class InnerReceiver : BroadcastReceiver
                {
                    const string SYSTEM_DIALOG_REASON_KEY = "reason";
                    const string SYSTEM_DIALOG_REASON_GLOBAL_ACTIONS = "globalactions";
                    const string SYSTEM_DIALOG_REASON_RECENT_APPS = "recentapps";
                    const string SYSTEM_DIALOG_REASON_HOME_KEY = "homekey";
        
                    OnHomePressedListener listener;
        
                    public InnerReceiver(OnHomePressedListener listener)
                    {
                        this.listener = listener;
                    }
        
                    public override void OnReceive(Context context, Intent intent)
                    {
                        String action = intent.Action;
                        if (action.Equals(Intent.ActionCloseSystemDialogs))  //ACTION_CLOSE_SYSTEM_DIALOGS
                        {
                            String reason = intent.GetStringExtra(SYSTEM_DIALOG_REASON_KEY);
                            if (reason != null)
                            {
                                Log.Info(TAG, "action:" + action + ",reason:" + reason);
                                if (listener != null)
                                {
                                    if (reason.Equals(SYSTEM_DIALOG_REASON_HOME_KEY))
                                    {
                                        listener.onHomePressed();
                                    }
                                    else if (reason.Equals(SYSTEM_DIALOG_REASON_RECENT_APPS))
                                    {
                                        listener.onHomeLongPressed();
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
        
    3. In my main Activity I use this code:
      using Android.App;
      using Android.OS;
      using Android.Support.V7.App;
      using Android.Runtime;
      using Android.Widget;
      using Android.Media;
      namespace HomeButton
      {
      [Activity(Label = "@string/app_name", Theme = "@style/AppTheme", MainLauncher = true)]
      public class MainActivity : AppCompatActivity
      {
      MediaPlayer player;
      protected override void OnCreate(Bundle savedInstanceState)
      {
      base.OnCreate(savedInstanceState);
      Xamarin.Essentials.Platform.Init(this, savedInstanceState);
      // Set our view from the "main" layout resource
      SetContentView(Resource.Layout.activity_main);
      player = MediaPlayer.Create(this, Resource.Raw.a);

                  MyHomeImpleWatcher myHomeImpleWatcher = new MyHomeImpleWatcher();
      
                  HomeWatcher mHomeWatcher = new HomeWatcher(this);
                  mHomeWatcher.setOnHomePressedListener(myHomeImpleWatcher);
                  mHomeWatcher.startWatch();
              }
              public override void OnRequestPermissionsResult(int requestCode, string[] permissions, [GeneratedEnum] Android.Content.PM.Permission[] grantResults)
              {
                  Xamarin.Essentials.Platform.OnRequestPermissionsResult(requestCode, permissions, grantResults);
      
                  base.OnRequestPermissionsResult(requestCode, permissions, grantResults);
              }
      
              protected override void OnStart()
              {
                  base.OnStart();
                  player.Start();
              }
      
      
      
          }
      
              class MyHomeImpleWatcher : OnHomePressedListener
              {
                  public void onHomeLongPressed()
                  {
                      System.Diagnostics.Debug.WriteLine("onHomePressed is pressed A");
      //here I cant call player.Stop();
      
                  }
                  public void onHomePressed()
                  {
                  System.Diagnostics.Debug.WriteLine("onHomePressed is pressed B");
                  }
               }
      }
      
      1. this is the demo, I cant call the player.Stop(); in function public void onHomeLongPressed() :

    How to fix it? thanks

  • BayuBayu Member ✭✭✭
    edited April 2020

    Really Nice, that's work properly. Thank you so much.

    nice to discuss with you :)

    @jezh

  • jezhjezh Member, Xamarin Team Xamurai

    It's my pleasure to help you. Have a nice day. :)

Sign In or Register to comment.