国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 系統(tǒng) > Android > 正文

Android實(shí)現(xiàn)調(diào)用震動(dòng)的方法

2020-04-11 11:18:00
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

本文實(shí)例講述了Android實(shí)現(xiàn)調(diào)用震動(dòng)的方法。分享給大家供大家參考,具體如下:

調(diào)用Android系統(tǒng)的震動(dòng),只需要一個(gè)類(lèi) 那就是Vibrator ,這個(gè)類(lèi)在hard包中,一看系統(tǒng)級(jí)的服務(wù),又要通過(guò)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類(lèi)用來(lái)操作設(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)閉和開(kāi)啟震動(dòng)的持續(xù)時(shí)間,以毫秒為單位。第一個(gè)值表示等待震動(dòng)開(kāi)啟的毫秒數(shù),下一個(gè)值表示保持震動(dòng)的毫秒數(shù),這個(gè)序列值交替表示震動(dòng)關(guān)閉和開(kāi)啟的毫秒數(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,用來(lái)取消震動(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)大小可以通過(guò)改變pattern來(lái)設(shè)定,如果開(kāi)啟時(shí)間太短,震動(dòng)效果可能感覺(jué)不到     * */    vibrator = (Vibrator)getSystemService(Context.VIBRATOR_SERVICE);    long [] pattern = {100,400,100,400}; // 停止 開(kāi)啟 停止 開(kāi)啟    vibrator.vibrate(pattern,2); //重復(fù)兩次上面的pattern 如果只想震動(dòng)一次,index設(shè)為-1  }  public void onStop(){    super.onStop();    vibrator.cancel();  }}

希望本文所述對(duì)大家Android程序設(shè)計(jì)有所幫助。

發(fā)表評(píng)論 共有條評(píng)論
用戶(hù)名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 宁明县| 驻马店市| 大方县| 武山县| 凤阳县| 栾川县| 昭苏县| 安泽县| 乾安县| 平江县| 安顺市| 金秀| 探索| 城固县| 丽江市| 东方市| 汝城县| 平山县| 两当县| 万州区| 义乌市| 宜君县| 通江县| 山阴县| 宜都市| 眉山市| 榆中县| 温州市| 五华县| 绿春县| 昌都县| 泸州市| 余干县| 阳谷县| 象山县| 太康县| 延边| 临武县| 华容县| 南澳县| 闽清县|