Android實(shí)現(xiàn)手機(jī)振動設(shè)置的方法
本文實(shí)例講述了Android實(shí)現(xiàn)手機(jī)振動設(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)閉振動"
android:textOff="啟動振動"
android:checked="false"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView android:id="@+id/tv1"
android:text="振動已關(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)閉振動"
android:textOff="啟動振動"
android:checked="false"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView android:id="@+id/tv2"
android:text="振動已關(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ī)震動權(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);
//注意模擬器是模擬不了震動的,得真機(jī)測試哦
//創(chuàng)建vibrator對象
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)行震動
//第一個參數(shù):該數(shù)組中第一個元素是等待多長的時間才啟動震動,
//之后將會是開啟和關(guān)閉震動的持續(xù)時間,單位為毫秒
//第二個參數(shù):重復(fù)震動時在pattern中的索引,如果設(shè)置為-1則表示不重復(fù)震動
vibrator.vibrate(new long[]{1000,50,50,100,50}, -1);
tv1.setText("振動已啟動");
}else {
//關(guān)閉震動
vibrator.cancel();
tv1.setText("震動已關(guān)閉");
}
break;
case R.id.tb2:
if(isChecked){
//啟動震動,并持續(xù)指定的時間
vibrator.vibrate(3500);
tv2.setText("振動已啟動");
}else {
//關(guān)閉啟動
vibrator.cancel();
tv2.setText("震動已關(guān)閉");
}
break;
}
}
};
}
運(yùn)行結(jié)果:

希望本文所述對大家的Android程序設(shè)計(jì)有所幫助。
- 鼠標(biāo)圖片振動代碼
- javascript 手機(jī)號碼正則表達(dá)式驗(yàn)證函數(shù)
- js 手機(jī)號碼合法性驗(yàn)證代碼集合
- 實(shí)用的JS正則表達(dá)式(手機(jī)號碼/IP正則/郵編正則/電話等)
- js和html5實(shí)現(xiàn)手機(jī)端刮刮卡抽獎效果完美兼容android/IOS
- JS判斷客戶端是手機(jī)還是PC的2個代碼
- 利用iOS動畫來模擬音量振動條的實(shí)現(xiàn)
- 安卓系統(tǒng)中實(shí)現(xiàn)搖一搖畫面振動效果的方法
- Android中通知Notification使用實(shí)例(振動、燈光、聲音)
- JavaScript手機(jī)振動API
相關(guān)文章
Android-實(shí)現(xiàn)切換Fragment頁功能的實(shí)現(xiàn)代碼
本篇文章主要介紹了Android-實(shí)現(xiàn)切換Fragment頁功能的實(shí)現(xiàn)代碼,具有一定的參加價值,有興趣的可以了解一下。2017-02-02
Android檢查手機(jī)網(wǎng)絡(luò)狀態(tài)及網(wǎng)絡(luò)類型的方法
這篇文章主要介紹了Android檢查手機(jī)網(wǎng)絡(luò)狀態(tài)及網(wǎng)絡(luò)類型的方法,涉及Android檢測手機(jī)網(wǎng)絡(luò)狀態(tài)的技巧,需要的朋友可以參考下2015-04-04
Android RadioGroup多行顯示效果 解決單選問題
這篇文章主要為大家詳細(xì)介紹了Android RadioGroup多行顯示效果,解決單選問題,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2019-11-11
基于Android 錯誤信息捕獲發(fā)送至服務(wù)器的詳解
本篇文章是對Android中錯誤信息捕獲發(fā)送服務(wù)器進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-06-06
Android?Jetpack庫剖析之ViewModel組件篇
這篇文章主要介紹了Android?Jetpack架構(gòu)組件?ViewModel詳解,ViewModel類讓數(shù)據(jù)可在發(fā)生屏幕旋轉(zhuǎn)等配置更改后繼續(xù)存在,ViewModel類旨在以注重生命周期的方式存儲和管理界面相關(guān)的數(shù)據(jù)。感興趣可以來學(xué)習(xí)一下2022-07-07
保持Android Service在手機(jī)休眠后繼續(xù)運(yùn)行的方法
下面小編就為大家分享一篇保持Android Service在手機(jī)休眠后繼續(xù)運(yùn)行的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-03-03
Android實(shí)用控件自定義逼真相機(jī)光圈View
這篇文章主要為大家詳細(xì)介紹了Android實(shí)用控件自定義逼真相機(jī)光圈,具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-08-08
Android編程獲取設(shè)備MAC地址的實(shí)現(xiàn)方法
這篇文章主要介紹了Android編程獲取設(shè)備MAC地址的實(shí)現(xiàn)方法,涉及Android針對硬件設(shè)備的操作技巧,需要的朋友可以參考下2017-01-01
Android實(shí)現(xiàn)可播放GIF動畫的ImageView
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)可播放GIF動畫的ImageView,具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-09-09
Flutter進(jìn)階之實(shí)現(xiàn)動畫效果(一)
這篇文章主要為大家詳細(xì)介紹了Flutter實(shí)現(xiàn)動畫效果的第一篇,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-08-08

