Android - How to detect action media button long press
<p>I'm developing an application that detect headset button single and double click (Code below). Single and double click works fine, but long click doesn't work. The code below is in a class that extends BroadcastReceiver. How can I resolve this problem? Thanks you very much.</p>

<pre><code>@Override
public void onReceive(Context context, Intent intent)
{
    context1=context;
    intent1=intent;
    String intentAction=intent.getAction();
    if (!Intent.ACTION_MEDIA_BUTTON.equalsIgnoreCase(intentAction))
    {
        return;
    }
    KeyEvent event = (KeyEvent) intent.getParcelableExtra(Intent.EXTRA_KEY_EVENT);
    if (event == null) {
        return;
    }
    int action = event.getAction();
    if (action == KeyEvent.ACTION_DOWN)
    {
        // do something

            d++;
            Handler handler = new Handler();
            Runnable r = new Runnable()
            {
                @Override
                public void run()
                {
                    // single click *******************************
                    if (d == 1)
                    {
                        Toast.makeText(context1, "single click!", Toast.LENGTH_SHORT).show();
                    }
                    // double click *********************************
                    if (d == 2)
                    {
                        Toast.makeText(context1, "Double click!!", Toast.LENGTH_SHORT).show();
                    }
                    d = 0;
                }
            };
            if (d == 1)
            {

                handler.postDelayed(r, 500);
            }
    }
    /**
     * for long click
     */
    if (action == KeyEvent.ACTION_UP)
    {
        if (SystemClock.uptimeMillis() - event.getDownTime() &gt; 2000)
        {
            Toast.makeText(context, "Long click!", Toast.LENGTH_SHORT).show();
        }
    }
    abortBroadcast();
}
</code></pre>

<p><strong>UPDATE</strong></p>

<p>This is Main.class</p>

<pre><code>public class Main extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener
{
private static final String TAG = "MainActivity";
PreferenceFragment pf;
@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.layout_main);

    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    FragmentManager fragmentManager= getFragmentManager();
    SingoloClick singoloClick= new SingoloClick();
    FragmentTransaction ft=fragmentManager.beginTransaction();
    ft.replace(R.id.content_frame, singoloClick);
    ft.commit();

    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
            this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
    drawer.setDrawerListener(toggle);
    toggle.syncState();

    NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
    navigationView.setNavigationItemSelectedListener(this);

    IntentFilter filter = new IntentFilter(Intent.ACTION_MEDIA_BUTTON);//"android.intent.action.MEDIA_BUTTON"
    MediaButtonIntentReceiver r = new MediaButtonIntentReceiver();
    filter.setPriority(999); //this line sets receiver priority
    registerReceiver(r, filter);
((AudioManager)getSystemService(AUDIO_SERVICE)).registerMediaButtonEventReceiver(new ComponentName(getPackageName(), MediaButtonIntentReceiver.class.getName()));
}

@Override public void onResume()
{
    super.onResume();
}

@Override
public void onBackPressed()
{
    moveTaskToBack(true);
    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    if (drawer.isDrawerOpen(GravityCompat.START))
    {
        drawer.closeDrawer(GravityCompat.START);
    }
    /*else
    {
        super.onBackPressed();
    }*/
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item)
{
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings)
    {
        return true;
    }

    return super.onOptionsItemSelected(item);
}

@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
    // Handle navigation view item clicks here.
    int id = item.getItemId();
    if (id == R.id.nav_singoloClick)
    {
        setTitle(""+getResources().getString(R.string.singoloClick));
        FragmentManager fragmentManager= getFragmentManager();
        SingoloClick singoloClick= new SingoloClick();
        FragmentTransaction ft=fragmentManager.beginTransaction();
        ft.replace(R.id.content_frame, singoloClick);
        ft.commit();
    } else if (id == R.id.nav_doppioClick)
    {
        setTitle(""+getResources().getString(R.string.doppioClick));
        FragmentManager fragmentManager= getFragmentManager();
        DoppioClick doppioClick= new DoppioClick();
        FragmentTransaction ft=fragmentManager.beginTransaction();
        ft.replace(R.id.content_frame, doppioClick);
        ft.commit();
    } else if (id == R.id.nav_lungoClick)
    {
    } 

    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    drawer.closeDrawer(GravityCompat.START);
    return true;
}


@Override
protected void onDestroy()
{
    super.onDestroy();
    android.os.Process.killProcess(android.os.Process.myPid());*/
}

@Override public void onPause()
{
    super.onPause();
}
}
</code></pre>

<p>This is Manifest.xml</p>

<pre><code>&lt;manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example"&gt;

&lt;uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" /&gt;

&lt;application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name"
    android:supportsRtl="true" android:theme="@style/AppTheme"&gt;
    &lt;activity android:name=".Main" android:label="@string/app_name" android:theme="@style/AppTheme.NoActionBar"&gt;
        &lt;intent-filter&gt;
            &lt;action android:name="android.intent.action.MAIN" /&gt;

            &lt;category android:name="android.intent.category.LAUNCHER" /&gt;
        &lt;/intent-filter&gt;
    &lt;/activity&gt;

    &lt;receiver android:name=".MediaButtonIntentReceiver"&gt;
        &lt;intent-filter android:priority="999"&gt;
            &lt;action android:name="android.intent.action.MEDIA_BUTTON" /&gt;
        &lt;/intent-filter&gt;
        &lt;intent-filter android:priority="0"&gt;
            &lt;action android:name="android.intent.action.VOICE_COMMAND" /&gt;
            &lt;category android:name="android.intent.category.DEFAULT" /&gt;
        &lt;/intent-filter&gt;
    &lt;/receiver&gt;
&lt;/application&gt;
</code></pre>

<p></p>