Android實(shí)現(xiàn)調(diào)用震動(dòng)的方法
本文實(shí)例講述了Android實(shí)現(xiàn)調(diào)用震動(dòng)的方法。分享給大家供大家參考,具體如下:
調(diào)用Android系統(tǒng)的震動(dòng),只需要一個(gè)類 那就是Vibrator ,這個(gè)類在hard包中,一看系統(tǒng)級的服務(wù),又要通過manifest.xml文件設(shè)置權(quán)限了
<?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>
下面還是一起學(xué)習(xí)一下SDK吧
Class that operates the vibrator on the device.
If your process exits, any vibration you started with will stop.
//Vibrator類用來操作設(shè)備上的震動(dòng),如果你的線程退出了,那么啟動(dòng)的震動(dòng)也會(huì)停止
public void vibrate (long[] pattern, int repeat)
Since: API Level 1
Vibrate with a given pattern. //根據(jù)給定的節(jié)奏震動(dòng)
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.
//傳遞一個(gè)整型數(shù)組作為關(guān)閉和開啟震動(dòng)的持續(xù)時(shí)間,以毫秒為單位。第一個(gè)值表示等待震動(dòng)開啟的毫秒數(shù),下一個(gè)值表示保持震動(dòng)的毫秒數(shù),這個(gè)序列值交替表示震動(dòng)關(guān)閉和開啟的毫秒數(shù)
To cause the pattern to repeat, pass the index into the pattern array at which to start the repeat, or -1 to disable repeating.
//為了重復(fù)的按設(shè)定的節(jié)奏震動(dòng),傳遞index參數(shù)表示重復(fù)次數(shù),用-1表示不重復(fù)。
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.
還包含一個(gè)方法叫做cancel,用來取消震動(dòng)
看一段演示的代碼:
/* * @author octobershiner * SE.HIT * 一個(gè)使用android手機(jī)震動(dòng)的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); /* * 想設(shè)置震動(dòng)大小可以通過改變pattern來設(shè)定,如果開啟時(shí)間太短,震動(dòng)效果可能感覺不到 * */ vibrator = (Vibrator)getSystemService(Context.VIBRATOR_SERVICE); long [] pattern = {100,400,100,400}; // 停止 開啟 停止 開啟 vibrator.vibrate(pattern,2); //重復(fù)兩次上面的pattern 如果只想震動(dòng)一次,index設(shè)為-1 } public void onStop(){ super.onStop(); vibrator.cancel(); } }
希望本文所述對大家Android程序設(shè)計(jì)有所幫助。
- Android使用Intent的Action和Data屬性實(shí)現(xiàn)點(diǎn)擊按鈕跳轉(zhuǎn)到撥打電話和發(fā)送短信界面
- android實(shí)現(xiàn)點(diǎn)擊按鈕切換不同的fragment布局
- Android中AlertDialog 點(diǎn)擊按鈕后不關(guān)閉對話框的功能
- Android點(diǎn)擊按鈕返回頂部實(shí)現(xiàn)代碼
- Android中手機(jī)震動(dòng)的設(shè)置(Vibrator)的步驟簡要說明
- android開發(fā)之蜂鳴提示音和震動(dòng)提示的實(shí)現(xiàn)原理與參考代碼
- Android 如何定制vibrator的各種震動(dòng)模式M 具體方法
- Android 使用Vibrator服務(wù)實(shí)現(xiàn)點(diǎn)擊按鈕帶有震動(dòng)效果
相關(guān)文章
Android?app啟動(dòng)節(jié)點(diǎn)與上報(bào)啟動(dòng)實(shí)例詳解
系統(tǒng)的啟動(dòng)過程非常復(fù)雜,下面這篇文章主要給大家介紹了關(guān)于Android?app啟動(dòng)節(jié)點(diǎn)與上報(bào)啟動(dòng)的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-04-04Android getActivity()為空的問題解決辦法
這篇文章主要介紹了Android getActivity()為空的問題解決辦法的相關(guān)資料,導(dǎo)致apk空指針崩潰問題,很嚴(yán)重的問題,為了解決這問題,上網(wǎng)搜索了很多資料,需要的朋友可以參考下2017-07-07Android使用AudioManager修改系統(tǒng)音量的方法
這篇文章主要介紹了Android使用AudioManager修改系統(tǒng)音量的方法,結(jié)合實(shí)例形式分析了AudioManager調(diào)節(jié)音量的常用方法及相關(guān)使用技巧,需要的朋友可以參考下2016-08-08Android開發(fā)實(shí)現(xiàn)Files文件讀取解析功能示例
這篇文章主要介紹了Android開發(fā)實(shí)現(xiàn)Files文件讀取解析功能,結(jié)合實(shí)例形式分析了Android針對txt文本文件的讀取、保存功能實(shí)現(xiàn)方法與布局操作技巧,需要的朋友可以參考下2017-09-09Android中網(wǎng)絡(luò)框架簡單封裝的實(shí)例方法
在本篇文章里小編給大家整理的是關(guān)于Android中網(wǎng)絡(luò)框架簡單封裝的實(shí)例方法,需要的朋友們可以學(xué)習(xí)下。2020-03-03android實(shí)現(xiàn)倒計(jì)時(shí)功能(開始、暫停、0秒結(jié)束)
這篇文章主要為大家詳細(xì)介紹了android實(shí)現(xiàn)倒計(jì)時(shí)功能,開始、暫停、0秒結(jié)束,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-09-09Android避免內(nèi)存溢出(Out of Memory)方法匯總
這篇文章主要為大家詳細(xì)介紹了Android避免內(nèi)存溢出Out of Memory方法匯總,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-01-01Android網(wǎng)絡(luò)監(jiān)聽和網(wǎng)絡(luò)判斷示例介紹
大家好,本篇文章主要講的是Android網(wǎng)絡(luò)監(jiān)聽和網(wǎng)絡(luò)判斷示例介紹,感興趣的同學(xué)趕快來看一看吧,對你有幫助的話記得收藏一下,方便下次瀏覽2021-12-12android Jsoup獲取網(wǎng)站內(nèi)容 android獲取新聞標(biāo)題實(shí)例
這篇文章主要為大家詳細(xì)介紹了android Jsoup獲取網(wǎng)站內(nèi)容,android獲取新聞標(biāo)題實(shí)例,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-03-03