Android volume key monitoring and android Volume monitoring

Source: Internet
Author: User

Android volume key monitoring and android Volume monitoring

Conclusion:

The following two methods can be used to obtain the volume key. During the test, we found that

When KeyEvent. KEYCODE_VOLUME_UP is held down all the time, Samsung's tablet counts to 200, and this event is not triggered;

Therefore, when KeyEvent. ACTION_DOWN is changed, a method is started to count the number of stop techniques of KeyEvent. ACTION_UP;

package com.akm.testvolume; import android.app.Activity;import android.os.Bundle;import android.view.KeyEvent;import android.widget.TextView; public class MainActivity extends Activity {    private TextView tv;     @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        tv = (TextView) findViewById(R.id.tv);    }     int count = -1;     @Override    public boolean dispatchKeyEvent(KeyEvent event) {         int action = event.getAction();         if (action ==KeyEvent.ACTION_DOWN) {            tv.setText("+++++++++ACTION_DOWN++++++"+ count++);            return true;        }         if (action== KeyEvent.ACTION_UP) {            tv.setText("+++++ACTION_UP++++++++++");            return true;        }         return super.dispatchKeyEvent(event);    }       @Override    public boolean onKeyDown(int keyCode, KeyEvent event) {        switch (keyCode) {         case KeyEvent.KEYCODE_VOLUME_DOWN:             tv.setText("-----------------"+count);            count--;             return true;         case KeyEvent.KEYCODE_VOLUME_UP:            tv.setText("++++++++++++++++"+ count);            count++;            return true;        case KeyEvent.KEYCODE_VOLUME_MUTE:            tv.setText("MUTE");             return true;        }        return super.onKeyDown(keyCode, event);    } }


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.