android震动服务能设置时长么,Android实现手机振动设置的方法
本文實(shí)例講述了Android實(shí)現(xiàn)手機(jī)振動(dòng)設(shè)置的方法。分享給大家供大家參考。具體如下:
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="關(guān)閉振動(dòng)"
android:textOff="啟動(dòng)振動(dòng)"
android:checked="false"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
android:text="振動(dòng)已關(guān)閉"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
android:layout_width="fill_parent"
android:layout_height="wrap_content">
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" />
android:text="振動(dòng)已關(guān)閉"
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);
//注意模擬器是模擬不了震動(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ì)有所幫助。
總結(jié)
以上是生活随笔為你收集整理的android震动服务能设置时长么,Android实现手机振动设置的方法的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python爬虫分析_Python爬虫解
- 下一篇: oracle修改成olap模式,的Ora