在Android
中,是否可以通过向上/向下按下音量按钮或通过按下电源按钮2或3次来发送消息?
我创建了一个SQLite数据库,其中包含名称,手机号码和消息。我想将保存的邮件发送到保存的号码。
答案 0 :(得分:0)
对于音量按钮,您可以执行以下操作
int counter = 0;
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_VOLUME_DOWN))
{
counter++;
if (counter == 3)
{
//Do something
//Send msg
}
}
return true;
}
答案 1 :(得分:0)
我还没有测试。请尝试一下。
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_VOLUME_DOWN){
// Your code here when pressing volumn down
return true;
} else if (keyCode == KeyEvent.KEYCODE_VOLUME_UP) {
// Your code here when pressing volumn up
return true;
} else if (keyCode == KeyEvent.KEYCODE_POWER) {
// Your code here when pressing power
return true;
}
return super.onKeyDown(keyCode, event);
}
答案 2 :(得分:0)
将onKeyMultiple()
与repeatCount
一起使用以检查点击次数。
@Override
public boolean onKeyMultiple(int keyCode, int repeatCount, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_VOLUME_DOWN) && repeatCount == 3) {
//Send Message...
}
return super.onKeyMultiple(keyCode, repeatCount, event);
}
onKeyMultiple
的Java文档:
/**
* Called when a user's interaction with an analog control, such as
* flinging a trackball, generates simulated down/up events for the same
* key multiple times in quick succession.
*
* @param keyCode The value in event.getKeyCode().
* @param count Number of pairs as returned by event.getRepeatCount().
* @param event Description of the key event.
*
* @return If you handled the event, return true. If you want to allow
* the event to be handled by the next receiver, return false.
*/
boolean onKeyMultiple(int keyCode, int count, KeyEvent event);