Android實(shí)現(xiàn)手機(jī)振動(dòng)設(shè)置的方法
本文實(shí)例講述了Android實(shí)現(xiàn)手機(jī)振動(dòng)設(shè)置的方法。分享給大家供大家參考。具體如下:
main.xml布局文件:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <LinearLayout android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="wrap_content"> <ToggleButton android:id="@+id/tb1" android:textOn="關(guān)閉振動(dòng)" android:textOff="啟動(dòng)振動(dòng)" android:checked="false" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:id="@+id/tv1" android:text="振動(dòng)已關(guān)閉" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout> <LinearLayout android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="wrap_content"> <ToggleButton android:id="@+id/tb2" android:textOn="關(guān)閉振動(dòng)" android:textOff="啟動(dòng)振動(dòng)" android:checked="false" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:id="@+id/tv2" android:text="振動(dòng)已關(guān)閉" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout> </LinearLayout>
清單文件:
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.ljq.activity" android:versionCode="1" android:versionName="1.0"> <application android:icon="@drawable/icon" android:label="@string/app_name"> <activity android:name=".VibrateActivity" 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-sdk android:minSdkVersion="7" /> <!-- 設(shè)置手機(jī)震動(dòng)權(quán)限 --> <uses-permission android:name="android.permission.VIBRATE" /> </manifest>
VibrateActivity類:
package com.ljq.activity; import android.app.Activity; import android.app.Service; import android.os.Bundle; import android.os.Vibrator; import android.widget.CompoundButton; import android.widget.TextView; import android.widget.ToggleButton; import android.widget.CompoundButton.OnCheckedChangeListener; public class VibrateActivity extends Activity { private Vibrator vibrator=null; private ToggleButton tb1=null, tb2=null; private TextView tv1=null, tv2=null; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); //注意模擬器是模擬不了震動(dòng)的,得真機(jī)測(cè)試哦 //創(chuàng)建vibrator對(duì)象 vibrator=(Vibrator)getSystemService(Service.VIBRATOR_SERVICE); tv1=(TextView)findViewById(R.id.tv1); tv2=(TextView)findViewById(R.id.tv2); tb1=(ToggleButton)findViewById(R.id.tb1); tb2=(ToggleButton)findViewById(R.id.tb2); tb1.setOnCheckedChangeListener(listener); tb2.setOnCheckedChangeListener(listener); } OnCheckedChangeListener listener=new OnCheckedChangeListener(){ public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { ToggleButton toggleButton=(ToggleButton)buttonView; switch (toggleButton.getId()) { case R.id.tb1: if(isChecked){ //根據(jù)指定的模式進(jìn)行震動(dòng) //第一個(gè)參數(shù):該數(shù)組中第一個(gè)元素是等待多長(zhǎng)的時(shí)間才啟動(dòng)震動(dòng), //之后將會(huì)是開啟和關(guān)閉震動(dòng)的持續(xù)時(shí)間,單位為毫秒 //第二個(gè)參數(shù):重復(fù)震動(dòng)時(shí)在pattern中的索引,如果設(shè)置為-1則表示不重復(fù)震動(dòng) vibrator.vibrate(new long[]{1000,50,50,100,50}, -1); tv1.setText("振動(dòng)已啟動(dòng)"); }else { //關(guān)閉震動(dòng) vibrator.cancel(); tv1.setText("震動(dòng)已關(guān)閉"); } break; case R.id.tb2: if(isChecked){ //啟動(dòng)震動(dòng),并持續(xù)指定的時(shí)間 vibrator.vibrate(3500); tv2.setText("振動(dòng)已啟動(dòng)"); }else { //關(guān)閉啟動(dòng) vibrator.cancel(); tv2.setText("震動(dòng)已關(guān)閉"); } break; } } }; }
運(yùn)行結(jié)果:
希望本文所述對(duì)大家的Android程序設(shè)計(jì)有所幫助。
- 鼠標(biāo)圖片振動(dòng)代碼
- javascript 手機(jī)號(hào)碼正則表達(dá)式驗(yàn)證函數(shù)
- js 手機(jī)號(hào)碼合法性驗(yàn)證代碼集合
- 實(shí)用的JS正則表達(dá)式(手機(jī)號(hào)碼/IP正則/郵編正則/電話等)
- js和html5實(shí)現(xiàn)手機(jī)端刮刮卡抽獎(jiǎng)效果完美兼容android/IOS
- JS判斷客戶端是手機(jī)還是PC的2個(gè)代碼
- 利用iOS動(dòng)畫來模擬音量振動(dòng)條的實(shí)現(xiàn)
- 安卓系統(tǒng)中實(shí)現(xiàn)搖一搖畫面振動(dòng)效果的方法
- Android中通知Notification使用實(shí)例(振動(dòng)、燈光、聲音)
- JavaScript手機(jī)振動(dòng)API
相關(guān)文章
android導(dǎo)入第三方j(luò)ar包報(bào)錯(cuò) 如何正確導(dǎo)入jar包
怎樣在android平臺(tái)上使用第三方j(luò)ar包,為什么我在引入了,編譯時(shí)沒有錯(cuò)誤,運(yùn)行時(shí)就有錯(cuò)誤,報(bào)無法實(shí)例化錯(cuò)誤,請(qǐng)問這是什么原因,本文給于解決方法,需要了解的朋友可以參考下2012-12-12TabLayout+ViewPager實(shí)現(xiàn)切頁的示例代碼
這篇文章主要介紹了TabLayout+ViewPager實(shí)現(xiàn)切頁的示例代碼,可實(shí)現(xiàn)左右滑動(dòng)切換視圖界面和點(diǎn)擊切換,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2019-01-01Android自定義ProgressBar實(shí)現(xiàn)漂亮的進(jìn)度提示框
這篇文章主要為大家詳細(xì)介紹了Android自定義ProgressBar實(shí)現(xiàn)漂亮的進(jìn)度提示框,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-06-06Android使用Kotlin和RxJava 2.×實(shí)現(xiàn)短信驗(yàn)證碼倒計(jì)時(shí)效果
本篇文章主要介紹了Android使用Kotlin和RxJava 2.×實(shí)現(xiàn)短信驗(yàn)證碼倒計(jì)時(shí)效果,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2017-12-12Android AndBase框架實(shí)現(xiàn)多功能標(biāo)題欄(一)
這篇文章主要整理了Android AndBase框架學(xué)習(xí)筆記,本文主要使用AndBase實(shí)現(xiàn)多功能標(biāo)題欄,感興趣的小伙伴們可以參考一下2016-03-03Android編程實(shí)現(xiàn)仿心跳動(dòng)畫效果的方法
這篇文章主要介紹了Android編程實(shí)現(xiàn)仿心跳動(dòng)畫效果的方法,實(shí)例分析了Android基于線程實(shí)現(xiàn)動(dòng)畫過度效果的相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-11-11