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 Detect Home Button pressed in Xamarin Android?

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

I make background music in my app, but when i press home button or exit the app, the music still play? How to fix it? thanks.

Best Answers

Answers

  • BayuBayu Member ✭✭✭

    thank you @jezh , I am sorry, I am a beginner in Xamarin android, may I ask you How to code step by step?

    where is I write define interface and define class?

  • jezhjezh Member, Xamarin Team Xamurai

    Of course, you can. It's my pleasure to help you. :)
    And I have uploaded the full demo into the attachment, you can try to run it and review it's code.

    where is I write define interface and define class?

    You can create interface and define class by the following steps:

    1.right click your app -> Add ->New Item..
    2.then it will open Add New Item Dialog as follows

    3.now you can create different item by selecting the appropriate options.

  • BayuBayu Member ✭✭✭

    thank you so much @jezh , I am sorry, how to apply this code to my problem?

    I have simple app with background music. how to set the code, when I pressed the middle button (red color) so my music is turn off? thank you

  • BayuBayu Member ✭✭✭

    thank you @jezh ,

    I write this code in my mainactivity

    using Android.App;
    using Android.OS;
    using Android.Support.V7.App;
    using Android.Runtime;
    using Android.Widget;
    using Xamarin.Forms;
    using Android.Media;

    namespace MC
    {
    [Activity(Label = "@string/app_name", Theme = "@style/AppTheme", MainLauncher = true)]
    public class MainActivity : AppCompatActivity
    {
    MediaPlayer player;
    TextView txt;
    Android.Widget.Button np;
    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);
    txt = FindViewById(Resource.Id.music);
    np = FindViewById<Android.Widget.Button>(Resource.Id.nextpage);
    player = MediaPlayer.Create(this, Resource.Raw.a);
    player.Start();

            np.Click += delegate
            {
                StartActivity(typeof(page2));
            };
    
    
            MessagingCenter.Subscribe<object>(this, "Hi", (sender) =>
            {
                player.Start();
            });
            MessagingCenter.Subscribe<object>(this, "Hallo", (sender) =>
            {
                player.Pause();
            });
    
            MyHomeImpleWatcher myHomeImpleWatcher = new MyHomeImpleWatcher();
    
            HomeWatcher mHomeWatcher = new HomeWatcher(this);
            mHomeWatcher.setOnHomePressedListener(myHomeImpleWatcher);
            mHomeWatcher.startWatch();
        }
        class MyHomeImpleWatcher : OnHomePressedListener
        {
            MediaPlayer player;
            public void onHomeLongPressed()
            {
                player.Stop();
            }
            public void onHomePressed()
            {
                System.Diagnostics.Debug.WriteLine("onHomeLongPressed is pressed......");
            }
    
        }
    }
    

    }

    but if I pressed the middle button (home button) my music still turn on, how to fix it?

  • BayuBayu Member ✭✭✭

    oh, Iam sorry, thank you @jezh

  • jezhjezh Member, Xamarin Team Xamurai

    It's my pleasure to help you. You can just open a new thread for the new question, we are all happy to help you. :)

    Best regards!

    Jezh

  • BayuBayu Member ✭✭✭

    Thank you so much @jezh .

    I have make new thread about it. :)

    nice discussion.

Sign In or Register to comment.