📌  相关文章
📜  如何在Android中以编程方式振动设备?

📅  最后修改于: 2021-05-13 18:17:57             🧑  作者: Mango

当涉及到用户体验时,也会考虑肝反馈。因此,在此讨论中,已经讨论了各种类型的触觉或设备的振动类型。例如,单击触觉或长按按钮触觉。在触觉反馈中讨论了五种不同类型的振动模式:

  1. 设备的默认振动
  2. 点击效果振动
  3. 双击效果振动
  4. 重击声振动
  5. 滴答声振动

注意,我们将使用Java语言实现该项目。

在Android中实现触觉反馈的步骤

第1步:创建一个空的活动Android Studio项目

  • 创建一个空的活动android studio项目。
  • 参考Android |如何在Android Studio中创建/启动新项目?
  • 请注意,选择Java作为编程语言。

步骤2:使用activity_main.xml

  • 在此讨论中,讨论了四种不同类型的触觉。
  • 因此,要生成该触觉,布局中包含四个不同的按钮。在activity_main.xml文件中调用以下代码。
  • 确保为所有按钮提供适当的ID,以便在MainActivity中处理它们。 Java文件。
XML


  
    
    


XML


  
    
    
  
    
        
            
                
  
                
            
        
    
  


Java
import androidx.appcompat.app.AppCompatActivity;
import android.content.Context;
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 {
  
    // buttons for all the types of the vibration effects
    Button bNormalVibration, bClickVibration, bDoubleClickVibration, bTickVibration, bHeavyClickVibration;
  
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
  
        // get the VIBRATOR_SERVICE system service
        final Vibrator vibrator = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
  
        // register all of the buttons with their IDs
        bNormalVibration = findViewById(R.id.normalVibrationButton);
        bClickVibration = findViewById(R.id.clickVibrationButton);
        bDoubleClickVibration = findViewById(R.id.doubleClickVibrationButton);
        bTickVibration = findViewById(R.id.tickVibrationButton);
        bHeavyClickVibration = findViewById(R.id.heavyClickVibrationButton);
  
        // handle normal vibration button
        bNormalVibration.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final VibrationEffect vibrationEffect1;
                
                // this is the only type of the vibration which requires system version Oreo (API 26)
                if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
  
                    // this effect creates the vibration of default amplitude for 1000ms(1 sec)
                    vibrationEffect1 = VibrationEffect.createOneShot(1000, VibrationEffect.DEFAULT_AMPLITUDE);
  
                    // it is safe to cancel other vibrations currently taking place
                    vibrator.cancel();
                    vibrator.vibrate(vibrationEffect1);
                }
            }
        });
  
        // handle click vibration button
        bClickVibration.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
  
                // this type of vibration requires API 29
                final VibrationEffect vibrationEffect2;
                
                if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.Q) {
  
                    // create vibrator effect with the constant EFFECT_CLICK
                    vibrationEffect2 = VibrationEffect.createPredefined(VibrationEffect.EFFECT_CLICK);
  
                    // it is safe to cancel other vibrations currently taking place
                    vibrator.cancel();
  
                    vibrator.vibrate(vibrationEffect2);
                }
            }
        });
  
        // handle double click vibration button
        bDoubleClickVibration.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
  
                final VibrationEffect vibrationEffect3;
                
                // this type of vibration requires API 29
                if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.Q) {
  
                    // create vibrator effect with the constant EFFECT_DOUBLE_CLICK
                    vibrationEffect3 = VibrationEffect.createPredefined(VibrationEffect.EFFECT_DOUBLE_CLICK);
  
                    // it is safe to cancel other vibrations currently taking place
                    vibrator.cancel();
  
                    vibrator.vibrate(vibrationEffect3);
                }
            }
        });
  
        // handle tick effect vibration button
        bTickVibration.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final VibrationEffect vibrationEffect4;
                
                // this type of vibration requires API 29
                if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.Q) {
  
                    // create vibrator effect with the constant EFFECT_TICK
                    vibrationEffect4 = VibrationEffect.createPredefined(VibrationEffect.EFFECT_TICK);
  
                    // it is safe to cancel other vibrations currently taking place
                    vibrator.cancel();
  
                    vibrator.vibrate(vibrationEffect4);
                }
            }
        });
  
        // handle heavy click vibration button
        bHeavyClickVibration.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final VibrationEffect vibrationEffect5;
                
                // this type of vibration requires API 29
                if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.Q) {
  
                    // create vibrator effect with the constant EFFECT_HEAVY_CLICK
                    vibrationEffect5 = VibrationEffect.createPredefined(VibrationEffect.EFFECT_HEAVY_CLICK);
  
                    // it is safe to cancel other vibrations currently taking place
                    vibrator.cancel();
  
                    vibrator.vibrate(vibrationEffect5);
                }
            }
        });
    }
}


输出界面:

在Android中以编程方式振动设备

步骤3:在AndroidManifest文件中调用“振动”权限

设备的振动需要许可。在AndroidManifest文件中调用以下代码。

XML格式



  
    
    
  
    
        
            
                
  
                
            
        
    
  

步骤4:使用MainActivity。 Java文件

  • 据说有五种不同的振动。对于这五种振动,每种振动都有一个常数。那些是:
  • MainActivity中调用以下代码。 Java文件。在代码内部添加了注释,以更详细地了解代码。

Java

import androidx.appcompat.app.AppCompatActivity;
import android.content.Context;
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 {
  
    // buttons for all the types of the vibration effects
    Button bNormalVibration, bClickVibration, bDoubleClickVibration, bTickVibration, bHeavyClickVibration;
  
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
  
        // get the VIBRATOR_SERVICE system service
        final Vibrator vibrator = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
  
        // register all of the buttons with their IDs
        bNormalVibration = findViewById(R.id.normalVibrationButton);
        bClickVibration = findViewById(R.id.clickVibrationButton);
        bDoubleClickVibration = findViewById(R.id.doubleClickVibrationButton);
        bTickVibration = findViewById(R.id.tickVibrationButton);
        bHeavyClickVibration = findViewById(R.id.heavyClickVibrationButton);
  
        // handle normal vibration button
        bNormalVibration.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final VibrationEffect vibrationEffect1;
                
                // this is the only type of the vibration which requires system version Oreo (API 26)
                if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
  
                    // this effect creates the vibration of default amplitude for 1000ms(1 sec)
                    vibrationEffect1 = VibrationEffect.createOneShot(1000, VibrationEffect.DEFAULT_AMPLITUDE);
  
                    // it is safe to cancel other vibrations currently taking place
                    vibrator.cancel();
                    vibrator.vibrate(vibrationEffect1);
                }
            }
        });
  
        // handle click vibration button
        bClickVibration.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
  
                // this type of vibration requires API 29
                final VibrationEffect vibrationEffect2;
                
                if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.Q) {
  
                    // create vibrator effect with the constant EFFECT_CLICK
                    vibrationEffect2 = VibrationEffect.createPredefined(VibrationEffect.EFFECT_CLICK);
  
                    // it is safe to cancel other vibrations currently taking place
                    vibrator.cancel();
  
                    vibrator.vibrate(vibrationEffect2);
                }
            }
        });
  
        // handle double click vibration button
        bDoubleClickVibration.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
  
                final VibrationEffect vibrationEffect3;
                
                // this type of vibration requires API 29
                if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.Q) {
  
                    // create vibrator effect with the constant EFFECT_DOUBLE_CLICK
                    vibrationEffect3 = VibrationEffect.createPredefined(VibrationEffect.EFFECT_DOUBLE_CLICK);
  
                    // it is safe to cancel other vibrations currently taking place
                    vibrator.cancel();
  
                    vibrator.vibrate(vibrationEffect3);
                }
            }
        });
  
        // handle tick effect vibration button
        bTickVibration.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final VibrationEffect vibrationEffect4;
                
                // this type of vibration requires API 29
                if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.Q) {
  
                    // create vibrator effect with the constant EFFECT_TICK
                    vibrationEffect4 = VibrationEffect.createPredefined(VibrationEffect.EFFECT_TICK);
  
                    // it is safe to cancel other vibrations currently taking place
                    vibrator.cancel();
  
                    vibrator.vibrate(vibrationEffect4);
                }
            }
        });
  
        // handle heavy click vibration button
        bHeavyClickVibration.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final VibrationEffect vibrationEffect5;
                
                // this type of vibration requires API 29
                if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.Q) {
  
                    // create vibrator effect with the constant EFFECT_HEAVY_CLICK
                    vibrationEffect5 = VibrationEffect.createPredefined(VibrationEffect.EFFECT_HEAVY_CLICK);
  
                    // it is safe to cancel other vibrations currently taking place
                    vibrator.cancel();
  
                    vibrator.vibrate(vibrationEffect5);
                }
            }
        });
    }
}

输出:

输出应在物理android设备上进行测试。要了解如何设置物理Android Studio,请参阅如何在真实设备上运行Android App?