国产探花免费观看_亚洲丰满少妇自慰呻吟_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ā)表
主站蜘蛛池模板: 临夏市| 方城县| 浦县| 洪雅县| 墨脱县| 三原县| 湘乡市| 肥西县| 汉川市| 万全县| 凤阳县| 高阳县| 淅川县| SHOW| 房产| 特克斯县| 沁阳市| 宁城县| 北碚区| 泰和县| 苏尼特左旗| 满洲里市| 连南| 郁南县| 锦州市| 济源市| 银川市| 浦县| 宾川县| 衡山县| 禄丰县| 石景山区| 前郭尔| 芒康县| 台安县| 毕节市| 登封市| 芜湖县| 明溪县| 广德县| 绥化市|