Android_(服務)Vibrator震動器

來源:互聯網
上載者:User

標籤:private   backup   short   hid   height   MIP   isp   androi   cli   

 

Vibrator震動器是Android給我們提供的用於機身震動的一個服務,例如當收到推送訊息的時候我們可以設定震動提醒,也可以運用到遊戲當中增強玩家互動性

 


運行:

 

 

程式結構

<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"    package="com.example.administrator.myapplication">    <application        android:allowBackup="true"        android:icon="@mipmap/ic_launcher"        android:label="@string/app_name"        android:roundIcon="@mipmap/ic_launcher_round"        android:supportsRtl="true"        android:theme="@style/AppTheme">        <activity android:name=".MainActivity">            <intent-filter>                <action android:name="android.intent.action.MAIN" />                <category android:name="android.intent.category.LAUNCHER" />            </intent-filter>        </activity>    </application>    <uses-permission android:name="android.permission.VIBRATE"/></manifest>
AndroidManifest.xml

 

package com.example.administrator.myapplication;import android.app.Service;import android.content.Context;import android.os.Vibrator;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.view.View;import android.widget.Button;import android.widget.Toast;public class MainActivity extends AppCompatActivity implements View.OnClickListener{    private Button btn_hasVibrator;    private Button btn_short;    private Button btn_long;    private Button btn_rhythm;    private Button btn_cancle;    private Vibrator myVibrator;    private Context mContext;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        //獲得系統的Vibrator執行個體:        myVibrator = (Vibrator) getSystemService(Service.VIBRATOR_SERVICE);        mContext = MainActivity.this;        bindViews();    }    private void bindViews() {        btn_hasVibrator = (Button) findViewById(R.id.btn_hasVibrator);        btn_short = (Button) findViewById(R.id.btn_short);        btn_long = (Button) findViewById(R.id.btn_long);        btn_rhythm = (Button) findViewById(R.id.btn_rhythm);        btn_cancle = (Button) findViewById(R.id.btn_cancle);        btn_hasVibrator.setOnClickListener(this);        btn_short.setOnClickListener(this);        btn_long.setOnClickListener(this);        btn_rhythm.setOnClickListener(this);        btn_cancle.setOnClickListener(this);    }    @Override    public void onClick(View v) {        switch (v.getId()) {            case R.id.btn_hasVibrator:                Toast.makeText(mContext, myVibrator.hasVibrator() ? "當前裝置有震動器" : "當前裝置無震動器",                        Toast.LENGTH_SHORT).show();                break;            case R.id.btn_short:                myVibrator.cancel();                myVibrator.vibrate(new long[]{100, 200, 100, 200}, 0);                Toast.makeText(mContext, "短震動", Toast.LENGTH_SHORT).show();                break;            case R.id.btn_long:                myVibrator.cancel();                myVibrator.vibrate(new long[]{100, 100, 100, 1000}, 0);                Toast.makeText(mContext, "長震動", Toast.LENGTH_SHORT).show();                break;            case R.id.btn_rhythm:                myVibrator.cancel();                myVibrator.vibrate(new long[]{500, 100, 500, 100, 500, 100}, 0);                Toast.makeText(mContext, "節奏震動", Toast.LENGTH_SHORT).show();                break;            case R.id.btn_cancle:                myVibrator.cancel();                Toast.makeText(mContext, "取消震動", Toast.LENGTH_SHORT).show();        }    }}
MainActivity

 

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:app="http://schemas.android.com/apk/res-auto"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical"    tools:context="com.example.administrator.myapplication.MainActivity"    android:weightSum="1">    <Button        android:id="@+id/btn_hasVibrator"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="監測手機是否有震動器" />    <Button        android:id="@+id/btn_short"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="短震動" />    <Button        android:id="@+id/btn_long"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="長震動" />    <Button        android:id="@+id/btn_rhythm"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="節奏震動" />    <Button        android:id="@+id/btn_cancle"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="取消震動" /></LinearLayout>
activity_main.xml

 

一、獲得系統的Vibrator

 

 

        myVibrator = (Vibrator) getSystemService(Service.VIBRATOR_SERVICE);        mContext = MainActivity.this;

 

二、判斷並設定頻率不同的震動器

 

abstract void cancel():關閉或者停止震動器

abstract boolean hasVibrator():判斷硬體是否有震動器

void vibrate(long milliseconds):控制手機震動為milliseconds毫秒

void vibrate(long[] pattern,int repeat):指定手機以pattern指定的模式震動

第一個參數:new int[200,400,600,800],在200,400,600,800這個時間交替啟動與關閉震動器
第二個參數:重複次數,如果是-1的只震動一次,如果是0的話則一直震動

 

 public void onClick(View v) {        switch (v.getId()) {            case R.id.btn_hasVibrator:                Toast.makeText(mContext, myVibrator.hasVibrator() ? "當前裝置有震動器" : "當前裝置無震動器",                        Toast.LENGTH_SHORT).show();                break;            case R.id.btn_short:                myVibrator.cancel();                myVibrator.vibrate(new long[]{100, 200, 100, 200}, 0);                Toast.makeText(mContext, "短震動", Toast.LENGTH_SHORT).show();                break;            case R.id.btn_long:                myVibrator.cancel();                myVibrator.vibrate(new long[]{100, 100, 100, 1000}, 0);                Toast.makeText(mContext, "長震動", Toast.LENGTH_SHORT).show();                break;            case R.id.btn_rhythm:                myVibrator.cancel();                myVibrator.vibrate(new long[]{500, 100, 500, 100, 500, 100}, 0);                Toast.makeText(mContext, "節奏震動", Toast.LENGTH_SHORT).show();                break;            case R.id.btn_cancle:                myVibrator.cancel();                Toast.makeText(mContext, "取消震動", Toast.LENGTH_SHORT).show();        }    }

 

三、開啟系統許可權

 

 <uses-permission android:name="android.permission.VIBRATE"/>

 

Android_(服務)Vibrator震動器

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.