如何在Android中计算按钮按下时间?

时间:2014-10-21 06:13:38

标签: java android timer

我正在开发一个项目,其中我需要不同的任务以获得不同的时间,就像用户在5毫秒之后按钮然后执行代码并且如果按下时间更多那么那么不同

实际上是在Facebook聊天中创建一个聊天室

问题是关于ActionDown事件,我想打开或启动我的应用程序

但问题是当用户尝试将聊天头从一个位置重新分配给其他事件时 喜欢

 This is my code

这里在ACTION_DOWN事件中我想要对应用程序

      public class ChadHead extends Service {
  private final Handler mHandler = new Handler();
    private Runnable mTimer1;
private WindowManager windowManager;
private ImageView chatHead;
Display display;
int width;
    int count=0;


@Override
public IBinder onBind(Intent intent) {
    // Not used
    return null;
}

@Override
public void onCreate() {
    super.onCreate();

     mTimer1 = new Runnable() {

            @Override
            public void run() {
                count++;
                // TODO Auto-generated method stub
                mHandler.postDelayed(this, 5000);
                if (count == 1) {
                    mHandler.removeCallbacks(mTimer1);
                    // put your code here:
                }
            }
        };



    windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);

    chatHead = new ImageView(this);
    chatHead.setImageResource(R.drawable.ic_launcher);

    final WindowManager.LayoutParams params = new WindowManager.LayoutParams(
            WindowManager.LayoutParams.WRAP_CONTENT,
            WindowManager.LayoutParams.WRAP_CONTENT,
            WindowManager.LayoutParams.TYPE_PHONE,
            WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
            PixelFormat.TRANSLUCENT);

    params.gravity = Gravity.TOP | Gravity.LEFT;
    params.x = 0;
    params.y = 100;

    windowManager.addView(chatHead, params);

    chatHead.setOnTouchListener(new View.OnTouchListener() {
        private int initialX;
        private int initialY;
        private float initialTouchX;
        private float initialTouchY;

        @SuppressWarnings("deprecation")
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                initialX = params.x;
                initialY = params.y;
                initialTouchX = event.getRawX();
                initialTouchY = event.getRawY();

                 mHandler.postDelayed(mTimer1, 0);
                /*Intent i;
                PackageManager manager = getPackageManager();
                try {
                   i = manager.getLaunchIntentForPackage("com.technorapper.technorappermapp");
                if (i == null)
                    throw new PackageManager.NameNotFoundException();
                i.addCategory(Intent.CATEGORY_LAUNCHER);
                startActivity(i);
                } catch (PackageManager.NameNotFoundException e) {

                }*/


                Toast.makeText(getApplicationContext(), "TechnoRapper", 50)
                        .show();

                return true;
            case MotionEvent.ACTION_MOVE:
                params.x = initialX
                        + (int) (event.getRawX() - initialTouchX);
                params.y = initialY
                        + (int) (event.getRawY() - initialTouchY);
                windowManager.updateViewLayout(chatHead, params);

                display = windowManager.getDefaultDisplay();
                width = display.getWidth();

                return true;
            case MotionEvent.ACTION_UP:
                 count = 0;
                if (initialTouchX < width / 2) {
                    initialTouchX = 0;
                } else if (initialTouchX >= width / 2) {
                    initialTouchX = width;
                }
                return true;
            }
            return false;
        }
    });

}

@Override
public void onDestroy() {
    super.onDestroy();
    if (chatHead != null)
        windowManager.removeView(chatHead);
}
  }

4 个答案:

答案 0 :(得分:2)

您可以使用View.OnTouchListener和两个时间戳来存储按钮单击开始(ACTION_DOWN)和结束时间(ACTION_UP)。然后,这2个变量(end-start)之间的差异将告诉您按钮点击时间的持续时间,您可以根据自己的喜好执行代码。

test.setOnTouchListener(new OnTouchListener() {
    private long start = 0;
    private long end = 0;
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        if (event.getAction()==MotionEvent.ACTION_DOWN) {
            this.start = System.currentTimeMillis();
        } else if (event.getaction()==MotionEvent.ACTION_UP) {
            this.end = System.currentTimeMillis();
        }
    }
});

答案 1 :(得分:2)

我在3个月前面对它,这是我的解决方案:

 private int count = 0;
    Button btn;
    private final Handler mHandler = new Handler();
    private Runnable mTimer1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btn = (Button) findViewById(R.id.btn);
        mTimer1 = new Runnable() {

            @Override
            public void run() {
                count++;
                // TODO Auto-generated method stub
                mHandler.postDelayed(this, 500);
                if (count == 1) {
                    mHandler.removeCallbacks(mTimer1);
                    // put your code here:
                Intent i;
                PackageManager manager = getPackageManager();
                try {
                   i = manager.getLaunchIntentForPackage("com.technorapper.technorappermapp");
                if (i == null)
                    throw new PackageManager.NameNotFoundException();
                i.addCategory(Intent.CATEGORY_LAUNCHER);
                startActivity(i);
                } catch (PackageManager.NameNotFoundException e) {

                }

                }
            }
        };
        btn.setOnTouchListener(new OnTouchListener() {

            private int initialX;
            private int initialY;
            private float initialTouchX;
            private float initialTouchY;

            @Override
            public boolean onTouch(View v, final MotionEvent event) {
                switch (event.getAction()) {
                case MotionEvent.ACTION_DOWN:
                    initialX = (int) btn.getX();
                    initialY = (int) btn.getY();
                    initialTouchX = event.getRawX();
                    initialTouchY = event.getRawY();
                    mHandler.postDelayed(mTimer1, 0);
                    return true;
                case MotionEvent.ACTION_UP:
                    count = 0;
                    return true;
                case MotionEvent.ACTION_MOVE:
                    runOnUiThread(new Runnable() {
                        public void run() {
                            btn.setX(initialX
                                    + (int) (event.getRawX() - initialTouchX));
                            btn.setY(initialY
                                    + (int) (event.getRawY() - initialTouchY));
                        }
                    });

                    return true;
                }
                return false;
            }
        });

使用此代码,您可以移动聊天头,并在500毫秒后启动事件。无需使用OnClickListener,检查位置是否未更改,因此将其检测为OnClickListener  如果你想在捕获一些动作时停止OnTouch事件,只需在ACTION_MOVE中放入'boolean variable'

答案 2 :(得分:1)

如果你想要自定义持续时间,你必须自己编程。

但是建议在函数中使用android build。 有一个View.OnLongClickListener和一个View.OnClickListener可以区分两个不同的持续时间来点击视图。

答案 3 :(得分:0)

我无法告诉你完整的确切代码但是我可以解释这里的流程,我从你的答案得到的是你需要在上次按下按钮时做不同的动作,这是你怎么做您需要两个Date变量,即current和lastTime

  DateTimeUtils.getCurrentUTCFormattedDateTime()

将lastTime存储在首选项的操作中,从首选项中获取最后一次并获取当前时间并减去它,以获得差异。

  long difference = (current.getTime() - lastSaved.getTime()) / 1000;

以秒为单位给你时差,然后你决定做什么。希望它有所帮助:)