简体   繁体   English

如何在android中检测长按音量按钮

[英]how to detect long press of volume button in android

I am working on application in which i want to provide facility to user to do specific thing when you long press up or down volume button no matter my app is background or foreground. 我正在开发应用程序,我想为用户提供设施,当你长按或缩小音量按钮时,无论我的应用程序是背景还是前景,都要做特定的事情。

I also want to implement this functionality when phone screen off but in stack-overflow posts says you cant do that. 我还希望在手机屏幕关闭时实现此功能,但在堆栈溢出的帖子中说你无法做到这一点。

i have used android.media.VOLUME_CHANGED_ACTION broadcast listener for volume button press detection and it work fine no matter your app is background but thing is that i want to detect Long press of these buttons. 我已经使用android.media.VOLUME_CHANGED_ACTION广播监听器进行音量按钮按下检测,它工作正常,无论你的应用程序是背景,但事情是我想检测长按这些按钮。

how can i include this code into my broadcast so i can detect up or down button press for long time. 如何将此代码包含在我的广播中,以便我可以长时间检测向上或向下按钮。

@Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_VOLUME_UP) {
        // Do something here...
       // Needed to track long presses
        Log.e("Main activity", "KEYCODE_VOLUME_UP");
        return true;
    }
    return super.onKeyDown(keyCode, event);
}

This is my broadcast receiver 这是我的广播接收器

 intentfliter  = new IntentFilter("android.media.VOLUME_CHANGED_ACTION");
       mReceiver  = new BroadcastReceiver()
                {   
                @Override
                public void onReceive(Context context, Intent intent) {
                    // TODO Auto-generated method stub
                         Log.e("Main activity", "Volume button press");     
                }
                };
                getApplicationContext().registerReceiver(mReceiver, intentfliter); 
                 manager.registerMediaButtonEventReceiver(getCallingActivity());


     }
package com.example.androidsample;
import java.util.Timer;
import java.util.TimerTask;
import java.util.logging.Logger;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;




public class VolumeButtonPressedBroadcast extends BroadcastReceiver{

    public static boolean sIsReceived; // this is made true and false after each timer clock
    public static Timer sTimer=null;
    public static int i;
    final int MAX_ITERATION=15;
    public static boolean sIsAppWorkFinished=true;
    @Override
    public void onReceive(final Context context, Intent intent) 
    {


        sIsReceived=true; // Make this true whenever isReceived called
        if(sTimer==null && sIsAppWorkFinished){
            sTimer=new Timer();
            sTimer.schedule(new TimerTask() {

                @Override
                public void run() {
                    if(sIsReceived){  // if its true it means user is still pressing the button
                        i++;
                    }else{ //in this case user must has released the button so we have to reset the timer
                        cancel(); 
                        sTimer.cancel();
                        sTimer.purge();
                        sTimer=null;
                        i=0;
                    }
                    if(i>=MAX_ITERATION){ // In this case we had successfully detected the long press event
                        cancel();
                        sTimer.cancel();
                        sTimer.purge();
                        sTimer=null;
                        i=0;
                        //it is called after 3 seconds
                    }

                    sIsReceived=false; //Make this false every time a timer iterates
                }
            }, 0, 200);
        }

    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM