標籤:android service 系統服務 vibrator 震動器
Android提供的系統服務之--Vibrator(震動器)
——轉載請註明出處:coder-pig
Vibrator簡介與相關方法:
簡單demo——設定頻率不同的震動器
對於Vibrator用的最廣泛的莫過於所謂的手機按摩器類的app,在app市場一搜,一堆,筆者隨便下了幾個下來瞅瞅
,都是大同小異的,這點小玩意竟然有8W多的下載量...好吧,好像也不算多,不過普遍功能都是切換震動頻率來完成
所謂的按摩效果,是否真的有效就不得而知了,那麼接下來
我們就來實現一個簡單的按摩器吧!
核心其實就是vibrate()中的數組的參數,根據自己需求寫一個數組就可以了!
因為模擬器不會震動的,所以需要在手機上運行才會有效果哦!
:
代碼也很簡單,布局的話就四個簡單的按鈕而已
activity_main.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/LinearLayout1" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="com.jay.example.vibratordemo.MainActivity" > <Button android:id="@+id/btnshort" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="短震動" /> <Button android:id="@+id/btnlong" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="長震動" /> <Button android:id="@+id/btnrhythm" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="節奏震動" /> <Button android:id="@+id/btncancle" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="取消震動" /> </LinearLayout>
接著就是MainActivity的編寫了,這裡和上一節的寫法是一樣的,讓Activity類實現OnClickListener介面
重寫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;@Overrideprotected 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);}@Overridepublic 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();}}}
最後別忘了在AndroidManifest.xml檔案中添加許可權哦!
<uses-permission android:name="android.permission.VIBRATE"/>
好了,基本用法其實也是很簡單的,這裡就不多說了,另外上面也說了,虛擬機器是沒有震動效果的,所以
需要把應用發布到手機上才能檢測效果了!參考代碼下載:vibratorDemo.rar
為了方便各位,直接把apk匯出來吧,直接下載安裝到手機就可以測試效果了,當然只是個小demo,不會
推送廣告,亂髮簡訊什麼的=-=!安裝時可以查看所需要的許可權!apk下載:vibratorDemo.apk
Android提供的系統服務之--Vibrator(震動器)