android震动服务能设置时长么,Android实现手机振动设置的方法
本文實例講述了Android實現手機振動設置的方法。分享給大家供大家參考。具體如下:
main.xml布局文件:
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
android:layout_width="fill_parent"
android:layout_height="wrap_content">
android:textOn="關閉振動"
android:textOff="啟動振動"
android:checked="false"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
android:text="振動已關閉"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
android:layout_width="fill_parent"
android:layout_height="wrap_content">
android:textOn="關閉振動"
android:textOff="啟動振動"
android:checked="false"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
android:text="振動已關閉"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
清單文件:
package="com.ljq.activity" android:versionCode="1"
android:versionName="1.0">
android:label="@string/app_name">
android:label="@string/app_name">
android:name="android.intent.category.LAUNCHER" />
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);
//注意模擬器是模擬不了震動的,得真機測試哦
//創建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){
//根據指定的模式進行震動
//第一個參數:該數組中第一個元素是等待多長的時間才啟動震動,
//之后將會是開啟和關閉震動的持續時間,單位為毫秒
//第二個參數:重復震動時在pattern中的索引,如果設置為-1則表示不重復震動
vibrator.vibrate(new long[]{1000,50,50,100,50}, -1);
tv1.setText("振動已啟動");
}else {
//關閉震動
vibrator.cancel();
tv1.setText("震動已關閉");
}
break;
case R.id.tb2:
if(isChecked){
//啟動震動,并持續指定的時間
vibrator.vibrate(3500);
tv2.setText("振動已啟動");
}else {
//關閉啟動
vibrator.cancel();
tv2.setText("震動已關閉");
}
break;
}
}
};
}
運行結果:
希望本文所述對大家的Android程序設計有所幫助。
總結
以上是生活随笔為你收集整理的android震动服务能设置时长么,Android实现手机振动设置的方法的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python爬虫分析_Python爬虫解
- 下一篇: android sina oauth2.