編輯:關於Android編程
Android提供的系統服務之--Vibrator(振動器)
——轉載請注明出處:coder-pig
Vibrator簡介與相關方法:
<喎?/kf/ware/vc/" target="_blank" class="keylink">vcD4KPHA+PGJyPgo8L3A+CjxwPjxzdHJvbmc+vPK1pWRlbW+hqqGqyejWw8a1wso8L3N0cm9uZz48c3Ryb25nPrK7zay1xNXxtq/G9zwvc3Ryb25nPjwvcD4KPHA+PGJyPgo8L3A+CjxwPjxzdHJvbmc+ttTT2lZpYnJhdG9y08O1xNfuueO3urXExKq5/dPay/nOvbXEyta7+rC0xKbG98DgtcRhcHAs1NphcHDK0LOh0rvL0SzSu7bRLLHK1d/L5rHjz8LBy7y4uPbPwsC0s/Kz8jwvc3Ryb25nPjwvcD4KPHA+PHN0cm9uZz4strzKx7TzzazQodLstcQs1eK149ChzebS4jwvc3Ryb25nPjxzdHJvbmc+vrnIu9PQOFe24LXEz8LU2MG/Li4uusOwySy6w8/x0rKyu8vjtuAssru5/cbVsem5psTctrzKx8fQu7vV8bavxrXCysC0zeqzyTwvc3Ryb25nPjwvcD4KPHA+PHN0cm9uZz7L+c69tcSwtMSm0Ke5+yzKx7fx1ea1xNPQ0Ke+zbK7tcO2+NaqwcssxMfDtL3Tz8LAtDwvc3Ryb25nPjwvcD4KPHA+PHN0cm9uZz7O0sPHvs3AtMq1z9bSu7j2vPK1pbXEsLTEpsb3sMkhPC9zdHJvbmc+PC9wPgo8cD48c3Ryb25nPrrL0MTG5Mq1vs3Kx3ZpYnJhdGUoKdbQtcTK/dfptcSyzsr9LLj5vt3X1Ly60OjH89C00ru49sr91+m+zb/J0tTByyE8L3N0cm9uZz48L3A+CjxwPjxzdHJvbmc+0vLOqsSjxOLG97K7u+HV8bavtcQsy/nS1NDo0qrU2srWu/rJz9TL0NCyxbvh09DQp7n7xbYhPC9zdHJvbmc+PC9wPgo8cD48YnI+CjwvcD4KPHA+PHN0cm9uZz7Qp7n7zbw6PC9zdHJvbmc+PC9wPgo8cD48c3Ryb25nPjxpbWcgc3JjPQ=="/uploadfile/Collfiles/20141107/20141107083938154.jpg" alt="\">
代碼也很簡單,布局的話就四個簡單的按鈕而已
activity_main.xml:
重寫onClick方法,根據不同的view的id就可以判斷是哪個按鈕點擊了,這種寫法的好處是對於組件較多的
情況這樣寫可以簡化代碼!
MainActivity.java:
package com.jay.example.vibratordemo; import android.app.Activity; import android.app.Service; import android.os.Bundle; import android.os.Vibrator; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.Toast; public class MainActivity extends Activity implements OnClickListener{ private Button btnshort; private Button btnlong; private Button btnrhythm; private Button btncancel; private Vibrator myVibrator; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); btnshort = (Button) findViewById(R.id.btnshort); btnlong = (Button) findViewById(R.id.btnlong); btnrhythm = (Button) findViewById(R.id.btnrhythm); btncancel = (Button) findViewById(R.id.btncancle); btnshort.setOnClickListener(this); btnlong.setOnClickListener(this); btnrhythm.setOnClickListener(this); btncancel.setOnClickListener(this); //獲得系統的Vibrator實例: myVibrator = (Vibrator) getSystemService(Service.VIBRATOR_SERVICE); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.btnshort: myVibrator.cancel(); myVibrator.vibrate(new long[]{100,200,100,200}, 0); Toast.makeText(getApplicationContext(), "短振動", Toast.LENGTH_SHORT).show(); break; case R.id.btnlong: myVibrator.cancel(); myVibrator.vibrate(new long[]{100,100,100,1000}, 0); Toast.makeText(getApplicationContext(), "長振動", Toast.LENGTH_SHORT).show(); break; case R.id.btnrhythm: myVibrator.cancel(); myVibrator.vibrate(new long[]{500,100,500,100,500,100}, 0); Toast.makeText(getApplicationContext(), "節奏振動", Toast.LENGTH_SHORT).show(); break; case R.id.btncancle: myVibrator.cancel(); Toast.makeText(getApplicationContext(), "取消振動", Toast.LENGTH_SHORT).show(); } } }
好了,基本用法其實也是很簡單的,這裡就不多說了,另外上面也說了,虛擬機是沒有震動效果的,所以
需要把應用發布到手機上才能檢測效果了!參考代碼下載:vibratorDemo.rar
為了方便各位,直接把apk導出來吧,直接下載安裝到手機就可以測試效果了,當然只是個小demo,不會
推送廣告,亂發短信什麼的=-=!安裝時可以查看所需要的權限!apk下載:vibratorDemo.apk
如何使用ES文件浏覽器的雲功能。ES文件浏覽器是android手機上非常常見的文件浏覽器,在新的版本中還加入了網絡的功能,還可以通過ES文件浏覽器可以直接訪
本文實例講述了Android實現圖片疊加效果的兩種方法。分享給大家供大家參考,具體如下:效果圖:第一種:第二種:第一種是通過canvas畫出來的效果:public voi
在網上看到了一個IOS組件PendulumView,實現了鐘擺的動畫效果。由於原生的進度條確實是不好看,所以想可以自定義View實現這樣的效果,以後也可以用於加載頁面的進
本文實例講述了Android電話撥號器實現方法。分享給大家供大家參考。具體如下:以下案例模擬android電話撥號器的實現AndroidManifest.xml清單列表&