如何使Android裝置振動?


在Android中,使用振動服務,我們可以使Android手機振動。此示例演示瞭如何使Android裝置振動。

步驟 1 - 在Android Studio中建立一個新專案,轉到檔案⇒新建專案,並填寫所有必需的詳細資訊以建立新專案。

步驟 2 - 將以下程式碼新增到res/layout/activity_main.xml。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:id="@+id/parent"
   xmlns:tools="http://schemas.android.com/tools"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   tools:context=".MainActivity"
   android:gravity="center"
   android:background="#33FFFF00"
   android:orientation="vertical">
   <TextView
      android:id="@+id/text"
      android:textSize="18sp"
      android:text="Click here to vibrate"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content" />
</LinearLayout>

在以上程式碼中,我們使用了TextView,當你點選TextView時,它會振動。

步驟 3 - 將以下程式碼新增到src/MainActivity.java

package com.example.andy.myapplication;

import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.os.VibrationEffect;
import android.os.Vibrator;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
   int view = R.layout.activity_main;
   TextView textView;
   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(view);
      final LinearLayout parent = findViewById(R.id.parent);
      textView = findViewById(R.id.text);
      textView.setOnClickListener(new View.OnClickListener() {
         @Override
         public void onClick(View v) {
            Vibrator vibrator = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
               vibrator.vibrate(VibrationEffect.createOneShot(500, VibrationEffect.DEFAULT_AMPLITUDE));
            } else {
               vibrator.vibrate(500);
            }
         }
      });
   }
}

在以上程式碼中,我們使用了振動服務,它是一個系統服務,如下所示 -

Vibrator vibrator = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
   vibrator.vibrate(VibrationEffect.createOneShot(500, VibrationEffect.DEFAULT_AMPLITUDE));
} else {
   vibrator.vibrate(500);
}

在以上程式碼中,我們設定了500毫秒,所以它會持續振動500毫秒。

步驟 4 - 將以下程式碼新增到manifest.java

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
   package="com.example.andy.myapplication">
   <uses-permission android:name="android.permission.VIBRATE" />
   <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"
         android:screenOrientation="portrait">
         <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
         </intent-filter>
      </activity>
   </application>
</manifest>

在以上程式碼中,我們添加了振動使用者許可權,沒有許可權,它將無法振動。

讓我們嘗試執行您的應用程式。我假設您已將您的實際Android手機裝置連線到您的計算機。要從Android Studio執行應用程式,請開啟專案中的一個活動檔案,然後點選執行    工具欄中的圖示。選擇您的手機裝置作為選項,然後檢查您的手機裝置,它將顯示您的預設螢幕 -

在以上結果中,當你點選TextView時,它會振動500毫秒。

點選這裡下載專案程式碼

更新於: 2019年7月30日

920 次瀏覽

開啟你的職業生涯

透過完成課程獲得認證

開始學習
廣告