Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6002285
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T00:57:22+00:00 2026-05-23T00:57:22+00:00

Inside of the onReceive(Content context, Intent intent) method of my public class MediaButtonIntentReceiver extends

  • 0

Inside of the onReceive(Content context, Intent intent) method of my public class MediaButtonIntentReceiver extends BroadcastReceiver I need to count the number of headset button clicks (single, double, triple), which is denoted by KeyEvent.ACTION_DOWN of the ACTION_MEDIA_BUTTON.

What I have almost works, but my current algorithm sucks and is unreliable after a few times. Basically every successive ACTION_DOWN (hit within a certain number of milliseconds to the previous ACTION_DOWN) I do numClicks++. But also I need to see when the user is done pressing, so after each event I start a CountDownTimer, and if by the time it runs out there are no new clicks, then I’m done and now know the number of clicks.

The problems I’m running into are as follows: for one, the button itself seems noisy – if I press it too fast I usually miss a click. Two, after a few trials when the app it loaded, it starts getting random and I’m assuming that there are multiple CountDownTimer threads (is that the right word?) still running which screws my stuff up.

Anyways here’s the main code snippet:

//note: thisClickTime uses System.currentTimeMillis()

   if (action == KeyEvent.ACTION_UP) {
        if (isDown == true) {
            if (numClicks == 0 && lastClickTime == 0) {
                //we have a new click
                numClicks++;
                lastClickTime = thisClickTime; //update the click time
                isDown = false;
                elapsedTime = thisClickTime - lastClickTime;    
            } else if (thisClickTime - lastClickTime < clickDelay) { //&& thisClickTime - lastClickTime > 10
                numClicks++;
                lastClickTime = thisClickTime; 
                isDown = false; 
            }

            final int oldNumClicks = numClicks;
            final CountDownTimer checkIfDone = new CountDownTimer(clickDelay, 10) {
            public void onTick(long millisUntilFinished) {
                if (oldNumClicks != numClicks) {
                    cancel(); 
                }
            }
            public void onFinish() { //code that executes when counter is done
                if (oldNumClicks == numClicks) {
                    //if user doesn't click anymore in time clickDelay + X milliseconds, then it's done
                    Toast.makeText(context, "Number of clicks: " + Integer.toString(numClicks), Toast.LENGTH_SHORT).show();
                    //reset state variables 
                    numClicks = 0;
                    lastClickTime = 0; 
                }
            }
           }.start();

        } else {
            //? 
        }
    }

For reference, I’ve been looking around at stuff like:

http://musicqueueproject.googlecode.com/svn-history/r83/trunk/src/com/yannickstucki/android/musicqueue/old/PlayerService.java

To see if there’s a good way to register number of clicks. I don’t really understand their code too well though, and from what I can see they only deal with single/double clicks (I may need triple and quadruple).

EDIT – uploaded the current code I’m working with. It works pretty decently most of the time. Here’s what I’ve noticed though: if I do my button testing too close together in time, the results start screwing up and under counting the clicks. I think this is because other CountDownTimers from previous attempts are still open, and when they finish, they reset certain state variables (numClicks = 0, for one). So am I misusing the timer? I can’t think of another solution though as I need some concept of elapsed time after the last click to determine when the clicking is done.

Thanks for any help.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T00:57:23+00:00Added an answer on May 23, 2026 at 12:57 am

    If your BroadcastReceiver is registered in the manifest, the BroadcastReceiver will only exist for a single call to onReceive() — subsequent broadcasts may result in another BroadcastReceiver instance. And a manifest-registered BroadcastReceiver cannot fork threads, as the whole process may get shut down once onReceive() is over.

    I am skeptical that there a clean way to get your code to be reliable, as the media button simply was not designed for your intended use pattern.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to run async task in onReceive(Context context, Intent intent) method. now i
I am using a simple BroadcastReceiver and onReceive method to intercept text messages as
Inside a Webservice method , i have created a helper class whose responsibility is
In my application I have one broadcast receiver. Inside the onReceive() method I am
I want an anonymous BroadcastReceiver to respond to links inside of a WebView. I
Inside the GAC, assemblies are keyed on their major version number only (sometimes major
Inside an initialization method, I have the following code - (id)init { self =
Inside Method A, there is method B. Method B throws exception, but method A
I have a BroadcastReceiver class. I have some static variables declared whose value is
Inside a templated class, I found the expression, *this = NULL What does such

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.