2022年3月31日 星期四

Android Studio - 手機震動類(Vibrator Class)(八十一)

Android Studio - 手機震動類(Vibrator Class)(八十一):

當處於一個寧靜的環境,例如在劇院、圖書館、會議、教堂或嘈吵環境,手機便利用機內的震動功能作為提示,通知用戶,便可以避免錯過重要時刻。Android Studio 提共了手機震動類(Vibrator Class),可以程式震動功能,方便編程。

手機調成靜音模式標示
Summary:

Constants

Description

int

VIBRATION_EFFECT_SUPPORT_NO

Vibration effect support: unsupported - This effect is not natively supported by the underlying hardware, although the system may still play a fallback vibration. Constant Value: 2 (0x00000002)

int

VIBRATION_EFFECT_SUPPORT_UNKNOWN

Vibration effect support: unknown - The hardware doesn't report its supported effects, so we can't determine whether the effect is supported or not. Constant Value: 0 (0x00000000)

int

VIBRATION_EFFECT_SUPPORT_YES

Vibration effect support: supported - This effect is supported by the underlying hardware. Constant Value: 1 (0x00000001)


Public methods:

Public methods

Description

final int

areAllEffectsSupported(int... effectIds)

Query whether the vibrator supports all the given effects.

final boolean

areAllPrimitivesSupported(int... primitiveIds)

Query whether the vibrator supports all of the given primitives.

int[]

areEffectsSupported(int... effectIds)

Query whether the vibrator natively supports the given effects.

boolean[]

arePrimitivesSupported(int... primitiveIds)

Query whether the vibrator supports the given primitives.

abstract void

cancel()

Turn the vibrator off.

VibratorFrequencyProfile

getFrequencyProfile()

Gets the profile that describes the vibrator output across the supported frequency range.

int

getId()

Return the ID of this vibrator.

int[]

getPrimitiveDurations(int... primitiveIds)

Query the estimated durations of the given primitives.

float

getQFactor()

Gets the Q factor of the vibrator.

float

getResonantFrequency()

Gets the resonant frequency of the vibrator, if applicable.

abstract boolean

hasAmplitudeControl()

Check whether the vibrator has amplitude control.

boolean

hasFrequencyControl()

Check whether the vibrator has independent frequency control.

abstract boolean

hasVibrator()

Check whether the hardware has a vibrator.

void

vibrate(long milliseconds)

This method was deprecated in API level 26. Use vibrate(android.os.VibrationEffect) instead.

void

vibrate(VibrationEffect vibe, VibrationAttributes attributes)

Vibrate with a given effect.

void

vibrate(long[] pattern, int repeat)

This method was deprecated in API level 26. Use vibrate(android.os.VibrationEffect) instead.

void

vibrate(VibrationEffect vibe, AudioAttributes attributes)

This method was deprecated in API level Tiramisu. Use vibrate(android.os.VibrationEffect, android.os.VibrationAttributes) instead.

void

vibrate(long milliseconds, AudioAttributes attributes)

This method was deprecated in API level 26. Use vibrate(android.os.VibrationEffect, android.os.VibrationAttributes) instead.

void

vibrate(VibrationEffect vibe)

Vibrate with a given effect.

void

vibrate(long[] pattern, int repeat, AudioAttributes attributes)

This method was deprecated in API level 26. Use vibrate(android.os.VibrationEffect, android.os.VibrationAttributes) instead.


Inherited methods - From class java.lang.Object:

From class java.lang.Object

Description

Object

clone()

Creates and returns a copy of this object.

boolean

equals(Object obj)

Indicates whether some other object is "equal to" this one.

void

finalize()

Called by the garbage collector on an object when garbage collection determines that there are no more references to the object.

final Class<?>

getClass()

Returns the runtime class of this Object.

int

hashCode()

Returns a hash code value for the object.

final void

notify()

Wakes up a single thread that is waiting on this object's monitor.

final void

notifyAll()

Wakes up all threads that are waiting on this object's monitor.

String

toString()

Returns a string representation of the object.

final void

wait(long timeout, int nanos)

Causes the current thread to wait until another thread invokes the notify() method or the notifyAll() method for this object, or some other thread interrupts the current thread, or a certain amount of real time has elapsed.

final void

wait(long timeout)

Causes the current thread to wait until either another thread invokes the notify() method or the notifyAll() method for this object, or a specified amount of time has elapsed.

final void

wait()

Causes the current thread to wait until another thread invokes the notify() method or the notifyAll() method for this object.


primitiveIds:

primitiveIds

Constant Value

primitiveIds

int: Which primitives to query for. This value cannot be null. Value is

 

VibrationEffect.Composition.PRIMITIVE_CLICK,

1 (0x00000001)

VibrationEffect.Composition.PRIMITIVE_THUD,

2 (0x00000002)

VibrationEffect.Composition.PRIMITIVE_SPIN,

3 (0x00000003)

VibrationEffect.Composition.PRIMITIVE_QUICK_RISE,

4 (0x00000004)

VibrationEffect.Composition.PRIMITIVE_SLOW_RISE,

5 (0x00000005)

VibrationEffect.Composition.PRIMITIVE_QUICK_FALL,

6 (0x00000006)

VibrationEffect.Composition.PRIMITIVE_LOW_TICK

8 (0x00000008)

VibrationEffect.Composition.PRIMITIVE_TICK

7 (0x00000007)


effectIds:

effectIds

Constant Value

effectIds

int: Which effects to query for. This value cannot be null. Value is

 

VibrationEffect.EFFECT_TICK, VibrationEffect

2 (0x00000002)

VibrationEffect.EFFECT_HEAVY_CLICK

5 (0x00000005)

EFFECT_CLICK

0 (0x00000000)

EFFECT_DOUBLE_CLICK

1 (0x00000001)

DEFAULT_AMPLITUDE

-1 (0xffffffff)


2022年 3月 31日(Thu)天氣報告
氣溫:41.0°F / 5.0°C @ 07:00
風速:每小時 6公里
降雨機會:5%
相對濕度:百分之 88%
天氣:多雲

2022年3月30日 星期三

Android Studio - 手機震動程式(Vibrator Program)(八十)

Android Studio - 手機震動程式(Vibrator Program)(八十):

手機震動程式(Vibrator Program)主要是打開震動器(Vibrator),然後傳遞一個 int 數值或陣列,它們是以毫秒(ms)為單位,代表打開或關閉振動器的持續時間,如 1000 是代表 1000ms 等於 1s (vibrator.vibrate(1000) )。需要重複使用,可以在第二值加入 0 (vibrator.vibrate(mVibratePattern, 0) ),禁用重複可以在第二值加入 -1 (vibrator.vibrate(mVibratePattern, -1))。

Android 手機震動程式(Vibrator Program)
  • 操作系統:Windows 7 64-bit 版本
  • 開發環境:Android Studio 4.0.1 版本
  • 原程式:C:\Development\Development_Android\Android_Project\DIY-Android-152-02 Vibration Bugworkshop
  • 程式:C:\Development\Development_Android\Android_Project\DIY-Android-152-02 Vibration Bugworkshop
  • 測試環境:Vivo(維沃)X5Max Android 4.4.4(KitKat)版本

Android 手機震動程式(Vibrator Program)編程步驟:
  • 設定 Vibrator 許可權限後 - AndroidManifest.xml
  • 設定 Vibrator Data 參數
  • 設定 Button

AndroidManifest.xml 獲取 Vibrator 許可權:

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


activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>

<androidx.constraintlayout.widget.ConstraintLayout 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"

    tools:context=".MainActivity">

 

    <TextView

        android:id="@+id/textView"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:text="Hello World!"

        app:layout_constraintBottom_toBottomOf="parent"

        app:layout_constraintLeft_toLeftOf="parent"

        app:layout_constraintRight_toRightOf="parent"

        app:layout_constraintTop_toTopOf="parent" />

 

    <Button

        android:id="@+id/btn_vibrator_300"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:layout_marginStart="30dp"

        android:layout_marginLeft="30dp"

        android:layout_marginTop="40dp"

        android:text="300ms"

        app:layout_constraintStart_toStartOf="parent"

        app:layout_constraintTop_toBottomOf="@+id/textView" />

 

    <Button

        android:id="@+id/btn_vibrator_custom"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:layout_marginStart="20dp"

        android:layout_marginLeft="20dp"

        android:layout_marginTop="40dp"

        android:text="Custom"

        app:layout_constraintStart_toEndOf="@+id/btn_vibrator_300"

        app:layout_constraintTop_toBottomOf="@+id/textView" />

 

    <Button

        android:id="@+id/btn_vibrator_repeat"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:layout_marginStart="20dp"

        android:layout_marginLeft="20dp"

        android:layout_marginTop="40dp"

        android:text="Repeat"

        app:layout_constraintStart_toEndOf="@+id/btn_vibrator_custom"

        app:layout_constraintTop_toBottomOf="@+id/textView" />

 

</androidx.constraintlayout.widget.ConstraintLayout>


MainActivity.java:

package bugworkshop.blogspot.com.vibration;

 

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;

import android.os.VibrationEffect;

import android.os.Vibrator;

import android.view.View;

import android.widget.Button;

 

public class MainActivity extends AppCompatActivity {

 

    Vibrator vibrator;

 

    private Button mBtn_virator_300;

    private Button mBtn_virator_custom;

    private Button mBtn_virator_repeat;

 

    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

 

        mBtn_virator_300 = (Button)findViewById(R.id.btn_vibrator_300);

        mBtn_virator_custom = (Button)findViewById(R.id.btn_vibrator_custom);

        mBtn_virator_repeat= (Button)findViewById(R.id.btn_vibrator_repeat);

        mBtn_virator_amplitudes = (Button)findViewById(R.id.btn_vibrator_300);

 

        vibrator = (Vibrator) getSystemService(VIBRATOR_SERVICE);

 

        mBtn_virator_custom.setOnClickListener(new View.OnClickListener() {

            @Override

            public void onClick(View view) {

                customVibratePattern();

            }

        });

 

        mBtn_virator_300.setOnClickListener(new View.OnClickListener() {

            @Override

            public void onClick(View view) {

                VibratePattern300();

            }

        });

 

        mBtn_virator_repeat.setOnClickListener(new View.OnClickListener() {

            @Override

            public void onClick(View view) {

                customVibratePatternReapt();

            }

        });

    }

 

    private void VibratePattern300() {

 

        // 300 : Vibrate for 300 milliseconds

        vibrator.vibrate(300);

    }

 

    private void customVibratePattern() {

 

        // 0 : Start without a delay

        // 400 : Vibrate for 400 milliseconds

        // 200 : Pause for 200 milliseconds

        // 400 : Vibrate for 400 milliseconds

        long[] mVibratePattern = new long[]{0, 400, 200, 400};

 

        // -1 : Do not repeat this pattern

        // pass 0 if you want to repeat this pattern from 0th index

        vibrator.vibrate(mVibratePattern, -1);

    }

 

    private void customVibratePatternReapt() {

        long[] mVibratePattern = new long[]{0, 400, 200, 400, 0};

        vibrator.vibrate(mVibratePattern, 0);

    }

 

}


2022年 3月 30日(Wed)天氣報告
氣溫:46.0°F / 8.0°C @ 07:00
風速:每小時 13公里
降雨機會:12%
相對濕度:百分之 83%
天氣:多雲