国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統(tǒng) > Android > 正文

Android實(shí)現(xiàn)手機(jī)振動(dòng)設(shè)置的方法

2020-04-11 11:25:10
字體:
供稿:網(wǎng)友

本文實(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è)元素是等待多長的時(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ì)有所幫助。

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 无棣县| 新密市| 南投县| 郎溪县| 大埔区| 上杭县| 开阳县| 曲水县| 体育| 湖南省| 晋中市| 建德市| 韶关市| 临沭县| 讷河市| 绥德县| 华容县| 罗源县| 延寿县| 珲春市| 巢湖市| 宜兰县| 随州市| 博湖县| 永和县| 文成县| 赣榆县| 衡南县| 阿拉善盟| 永靖县| 呼玛县| 丹东市| 栾城县| 错那县| 湛江市| 文水县| 陈巴尔虎旗| 马尔康县| 海宁市| 锡林郭勒盟| 上虞市|