如何检测音量按钮在Android的长期preSS [英] how to detect long press of volume button in android

查看:290
本文介绍了如何检测音量按钮在Android的长期preSS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我工作的应用程序中,我想提供方便用户做特定的事情,当你长时间preSS向上或向下音量键,无论我的应用程序是背景或前景。

我也想实现这个功能,当手机屏幕关闭,但在堆栈溢出的帖子说,你不能做到这一点。

我用 android.media.VOLUME_CHANGED_ACTION 广播监听音量按钮preSS检测,它做工精细,无论您的应用程序的背景,但事情是,我想检测这些按钮隆preSS。

我怎么能有这种code进入我的直播,所以我可以检测向上或向下键preSS长时间。

  @覆盖
公共布尔的onkeyup(INT键code,KeyEvent的事件){
    如果(键code == KeyEvent.KEY code_VOLUME_UP){
        //做的东西在这里...
       //用来跟踪长presses
        Log.e(主要业务,KEY code_VOLUME_UP);
        返回true;
    }
    返回super.onKeyDown(键code,事件);
}
 

这是我的广播接收器

  intentfliter =新的IntentFilter(android.media.VOLUME_CHANGED_ACTION);
       mReceiver =新的BroadcastReceiver()
                {
                @覆盖
                公共无效的onReceive(上下文的背景下,意图意图){
                    // TODO自动生成方法存根
                         Log.e(主要业务,音量键preSS);
                }
                };
                。getApplicationContext()registerReceiver(mReceiver,intentfliter);
                 manager.registerMediaButtonEventReceiver(getCallingActivity());


     }
 

解决方案

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

    }

}

这篇关于如何检测音量按钮在Android的长期preSS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆