這兩天幹了點別的,原因是昨天使用getProviders()方法時,用到了ArrayList這個類,突然想深入的瞭解他的機制,結果收穫非常多,很糾結的看了一位大神分析的JAVA COLLECTION FRAMEWORK的原始碼,這樣Fail Fast機制就赤裸裸的出現在眼前了,中間核心代碼帶我走進了設計模式的大門,原來原始碼使用了裝飾器設計模式,今天自己也是總結了一下學習成果 在 http://blog.csdn.net/octobershiner/article/details/6631997
言歸正傳,調用Android系統的震動,只需要一個類 那就是Vibrator ,這個類在hard包中,一看系統級的服務,又要通過manifest.xml檔案設定許可權了
<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android" package="uni.vibrator" android:versionCode="1" android:versionName="1.0"> <uses-sdk android:minSdkVersion="8" /> <application android:icon="@drawable/icon" android:label="@string/app_name"> <activity android:name=".VibratorDemoActivity" android:label="@string/app_name"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> <uses-permission android:name="android.permission.VIBRATE" /></manifest>
下面還是一起學習一下SDK吧
Class that operates the vibrator on the device.
If your process exits, any vibration you started with will stop.
//Vibrator類用來操作裝置上的震動,如果你的線程退出了,那麼啟動的震動也會停止
---------------------------------------------------------------------------------------------------------------------------------------------------
public void vibrate (long[] pattern, int repeat)
Since: API Level 1
Vibrate with a given pattern. //根據給定的節奏震動
Pass in an array of ints that are the durations for which to turn on or off the vibrator in milliseconds. The first value indicates the number of milliseconds to wait before turning the vibrator on. The next value indicates the number of milliseconds for which to keep the vibrator on before turning it off. Subsequent values alternate between durations in milliseconds to turn the vibrator off or to turn the vibrator on.
//傳遞一個整型數組作為關閉和開啟震動的期間,以毫秒為單位。第一個值表示等待震動開啟的毫秒數,下一個值表示保持震動的毫秒數,這個序列值交替表示震動關閉和開啟的毫秒數
To cause the pattern to repeat, pass the index into the pattern array at which to start the repeat, or -1 to disable repeating.
//為了重複的按設定的節奏震動,傳遞index參數表示重複次數,用-1表示不重複。
Parameters
pattern an array of longs of times for which to turn the vibrator on or off.
repeat the index into pattern at which to repeat, or -1 if you don't want to repeat.
---------------------------------------------------------------------------------------------------------------------------------------------------
還包含一個方法叫做cancel,用來取消震動
一段示範的代碼
/* * @author octobershiner * 2011 7 25 * SE.HIT * 一個使用android手機震動的demo * */package uni.vibrator;import android.app.Activity;import android.content.Context;import android.os.Bundle;import android.os.Vibrator;public class VibratorDemoActivity extends Activity {private Vibrator vibrator; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); /* * 想設定震動大小可以通過改變pattern來設定,如果開啟時間太短,震動效果可能感覺不到 * */ vibrator = (Vibrator)getSystemService(Context.VIBRATOR_SERVICE); long [] pattern = {100,400,100,400}; // 停止 開啟 停止 開啟 vibrator.vibrate(pattern,2); //重複兩次上面的pattern 如果只想震動一次,index設為-1 } public void onStop(){ super.onStop(); vibrator.cancel(); }}
具體效果,不知道如何在虛擬機器上類比,所以只好先採用真機測試了,如果有知道用模擬器類比的方法,希望告訴我一下~