-
調用Android系統的震動,只需要一個類 那就是Vibrator ,這個類在hard包中,一看系統級的服務,又要通過manifest.xml文件設置權限了
01.
<?
xml
version
=
"1.0"
encoding
=
"utf-8"
?>
02.
<
manifest
xmlns:android
=
"http://schemas.android.com/apk/res/android"
03.
package
=
"uni.vibrator"
04.
android:versionCode
=
"1"
05.
android:versionName
=
"1.0"
>
06.
<
uses-sdk
android:minSdkVersion
=
"8"
/>
07.
08.
<
application
android:icon
=
"@drawable/icon"
android:label
=
"@string/app_name"
>
09.
<
activity
android:name
=
".VibratorDemoActivity"
10.
android:label
=
"@string/app_name"
>
11.
<
intent-filter
>
12.
<
action
android:name
=
"android.intent.action.MAIN"
/>
13.
<
category
android:name
=
"android.intent.category.LAUNCHER"
/>
14.
</
intent-filter
>
15.
</
activity
>
16.
17.
</
application
>
18.
<
uses-permission
android:name
=
"android.permission.VIBRATE"
/>
19.
</
manifest
>
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 1Vibrate 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,用來取消震動
一段演示的代碼
01.
/*
02.
* @author octobershiner
03.
* 2011 7 25
04.
* SE.HIT
05.
* 一個使用android手機震動的demo
06.
* */
07.
package uni.vibrator;
08.
09.
import android.app.Activity;
10.
import android.content.Context;
11.
import android.os.Bundle;
12.
import android.os.Vibrator;
13.
14.
public
class
VibratorDemoActivity extends Activity {
15.
private
Vibrator vibrator;
16.
/** Called when the activity is first created. */
17.
@Override
18.
public
void
onCreate(Bundle savedInstanceState) {
19.
super.onCreate(savedInstanceState);
20.
setContentView(R.layout.main);
21.
22.
/*
23.
* 想設置震動大小可以通過改變pattern來設定,如果開啓時間太短,震動效果可能感覺不到
24.
* */
25.
vibrator = (Vibrator)getSystemService(Context.VIBRATOR_SERVICE);
26.
long
[] pattern = {100,400,100,400};
// 停止 開啓 停止 開啓
27.
vibrator.vibrate(pattern,2);
//重複兩次上面的pattern 如果只想震動一次,index設爲-1
28.
}
29.
30.
public
void
onStop(){
31.
super.onStop();
32.
vibrator.cancel();
33.
}
34.
}
-
使用android 調用震動的例子
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章
【Android】APP檢測版本升級更新、apk安裝
冰冷的希望
2020-07-08 01:22:12
Android使用Mqtt案例
咆哮的程序猿
2020-07-07 14:09:18
【安卓學習】1.活動-寫第一個安卓項目
王二牛放小
2020-07-07 05:20:52
【安卓學習】2.Intent的學習
王二牛放小
2020-07-07 05:20:52
【安卓學習】4.碎片(Fragment)實踐---一個簡單的新聞應用。
王二牛放小
2020-07-07 05:20:52
關於安卓中context概念的理解
tribody
2020-07-07 00:49:12
安卓顏色對應的xml配置值(轉載)
原文引用自原始地址 網上找的一些顏色值 Java代碼 <?xml version="1.0" encoding="utf-8" ?> <resources> <color name="whit
tribody
2020-07-07 00:49:12
SQLiteDatabase事務
SQLiteDatabase中包含如下兩個方法來控制事務。 beginTransaction():開始事務 endTransaction():結束事
铸 剑
2020-07-06 23:49:22
獲取相冊照片和獲取拍照照片
铸 剑
2020-07-06 23:49:22
安卓獲取系統當前時間
一、DateFormat import java.text.SimpleDateFormat; SimpleDateFormat formatter = new SimpleDa
铸 剑
2020-07-06 23:49:22
安卓在Java文件裏實現dp和px的轉換
在安卓的xml佈局中,用來表示像素的單位可以是px也可以是dp,但是在java文件裏該如何實現呢?其實很簡單,用這樣的工具類就行了。import a
铸 剑
2020-07-06 23:49:22
獲取手機文件路徑
一、 Environment.getExternalStorageDirectory().getPath()=/storage/emulated/0
铸 剑
2020-07-06 23:49:11
TabHost使用總結
一、相關概念 與TabHost結合使用的還有如下組件。 TabWidget:代表選項卡的標籤條。 TabSpec:代表選項卡的一個Tab頁面。 Ta
铸 剑
2020-07-06 23:49:11