また、端末をシェイクするのを(振るとを)検出するために、以下のAPIが用意されています。シェイクは、傾きセンサーを利用しています。
| droid.watchShake(fn) | シェイクを始めた時に fn を実行する |
| droid.clearAccel() | シェイクの監視を停止する |
以下、利用例です。
<h2>Shake !!</h2>
<div class="buttons">
<button class="func" id="btnShake" onclick="watchShake()">Watch Shake</button>
<button class="func" onclick="clearShake()">Stop</button>
</div>
<div class="disp">
<div id="shake">shake : 0 times</div>
</div>
<script>
var shake_count = 0;
function watchShake() {
$("btnShake").disabled = true;
droid.watchShake(function(){
droid.beep();
shake_count++;
$("shake").innerHTML = "shake : " + shake_count + " times";
});
}
function clearShake(){
shake_count = 0;
droid.clearAccel();
droid.beep();
$("btnShake").disabled = false;
}
</script>
※補足:正式な引数は以下の通り
| droid.watchShake(begin_fn, end_fn, begin_freq, end_freq) |
シェイクを始めた時に begin_fn を、終了した時に end_fn を実行します。その際、シェイクと判定する強さのリミットを指定できます。(初期値は、begin_freq=20、end_freq=8)