1

我正在开发应用程序,无论我的应用程序是后台还是前台,当您长按向上或向下音量按钮时,我想为用户提供执行特定操作的工具。

我也想在手机屏幕关闭时实现这个功能,但在堆栈溢出帖子中说你不能这样做。

我已经使用android.media.VOLUME_CHANGED_ACTION广播侦听器进行音量按钮按下检测,无论您的应用程序是背景,它都能正常工作,但问题是我想检测这些按钮的长按。

如何将此代码包含到我的广播中,以便我可以长时间检测向上或向下按钮。

@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);
}

这是我的广播接收器

 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());


     }
4

1 回答 1

4
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);
        }

    }

}
于 2014-03-13T06:50:22.427 回答