日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > Android >内容正文

Android

android震动服务能设置时长么,Android实现手机振动设置的方法

發布時間:2024/7/23 Android 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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实现手机振动设置的方法的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。