且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何检测音量按钮在Android的长期preSS

更新时间:2023-11-18 13:01:10

 包com.example.androidsample;
进口java.util.Timer中;
进口java.util.TimerTask中;
进口java.util.logging.Logger中;

进口android.content.BroadcastReceiver;
进口android.content.Context;
进口android.content.Intent;




公共类音量按钮pressedBroadcast延伸的BroadcastReceiver {

    公共静态布尔sIsReceived; //这是由真假每个定时器时钟后,
    公共静态定时STIMER = NULL;
    公共静态INT I;
    最终诠释MAX_ITERATION = 15;
    公共静态布尔sIsAppWorkFinished = TRUE;
    @覆盖
    公共无效的onReceive(最终上下文的背景下,意图意图)
    {


        sIsReceived = TRUE; //使这个真实每当isReceived称为
        如果(STIMER == NULL和放大器;&安培; sIsAppWorkFinished){
            STIMER =新的Timer();
            sTimer.schedule(新的TimerTask(){

                @覆盖
                公共无效的run(){
                    如果(sIsReceived){//如果属实则意味着用户仍然是pressing按钮
                        我++;
                    }其他{//在这种情况下,用户必须已经发布了按钮,所以我们必须复位定时器
                        取消();
                        sTimer.cancel();
                        sTimer.purge();
                        STIMER = NULL;
                        I = 0;
                    }
                    如果(I> = MAX_ITERATION){//在这种情况下,我们已经成功地检测到的长preSS事件
                        取消();
                        sTimer.cancel();
                        sTimer.purge();
                        STIMER = NULL;
                        I = 0;
                        //这是在3秒后叫
                    }

                    sIsReceived = FALSE; //使这种虚假每次定时器迭代
                }
            },0,200);
        }

    }

}
 

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.

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

    }

}