Android實現(xiàn)調(diào)用震動的方法
本文實例講述了Android實現(xiàn)調(diào)用震動的方法。分享給大家供大家參考,具體如下:
調(diào)用Android系統(tǒng)的震動,只需要一個類 那就是Vibrator ,這個類在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è)備上的震動,如果你的線程退出了,那么啟動的震動也會停止
public void vibrate (long[] pattern, int repeat)
Since: API Level 1
Vibrate with a given pattern. //根據(jù)給定的節(jié)奏震動
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.
//傳遞一個整型數(shù)組作為關(guān)閉和開啟震動的持續(xù)時間,以毫秒為單位。第一個值表示等待震動開啟的毫秒數(shù),下一個值表示保持震動的毫秒數(shù),這個序列值交替表示震動關(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é)奏震動,傳遞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.
還包含一個方法叫做cancel,用來取消震動
看一段演示的代碼:
/*
* @author octobershiner
* 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);
/*
* 想設(shè)置震動大小可以通過改變pattern來設(shè)定,如果開啟時間太短,震動效果可能感覺不到
* */
vibrator = (Vibrator)getSystemService(Context.VIBRATOR_SERVICE);
long [] pattern = {100,400,100,400}; // 停止 開啟 停止 開啟
vibrator.vibrate(pattern,2); //重復(fù)兩次上面的pattern 如果只想震動一次,index設(shè)為-1
}
public void onStop(){
super.onStop();
vibrator.cancel();
}
}
希望本文所述對大家Android程序設(shè)計有所幫助。
- Android使用Intent的Action和Data屬性實現(xiàn)點擊按鈕跳轉(zhuǎn)到撥打電話和發(fā)送短信界面
- android實現(xiàn)點擊按鈕切換不同的fragment布局
- Android中AlertDialog 點擊按鈕后不關(guān)閉對話框的功能
- Android點擊按鈕返回頂部實現(xiàn)代碼
- Android中手機震動的設(shè)置(Vibrator)的步驟簡要說明
- android開發(fā)之蜂鳴提示音和震動提示的實現(xiàn)原理與參考代碼
- Android 如何定制vibrator的各種震動模式M 具體方法
- Android 使用Vibrator服務(wù)實現(xiàn)點擊按鈕帶有震動效果
相關(guān)文章
深入理解Android熱修復(fù)技術(shù)原理之so庫熱修復(fù)技術(shù)
通常情況下,大多數(shù)人希望android下熱補丁方案能夠做到補丁的全方位修復(fù),包括類修復(fù)/資源修復(fù)/so庫的修復(fù)。 這里主要介紹熱補丁之so庫修復(fù)思路2021-06-06
Android Studio使用Profiler來完成內(nèi)存泄漏的定位
這篇文章主要介紹了Android Studio使用Profiler來完成內(nèi)存泄漏的定位,幫助大家更好的理解和學(xué)習(xí)使用Android,感興趣的朋友可以了解下2021-03-03

