Android震动器Vibrator调用 1.首先添加权限 2.获得Vibrator...实例: Vibrator vb = (Vibrator)getSystemService(Service.VIBRATOR_SERVICE); 3.相关方法 abstract void cancel()...super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // 获得系统的Vibrator...实例 myVibrator = (Vibrator) getSystemService(Service.VIBRATOR_SERVICE); } @Override
Android该系统提供的服务–Vibrator(振子) ——转载请注明出处:coder-pig Vibrator简单介绍与相关方法: 简单demo——设置频率不同的振动器 对于Vibrator用的最广泛的莫过于所谓的手机按摩器类的...import android.app.Activity; import android.app.Service; import android.os.Bundle; import android.os.Vibrator...Button btnshort; private Button btnlong; private Button btnrhythm; private Button btncancel; private Vibrator...btnlong.setOnClickListener(this); btnrhythm.setOnClickListener(this); btncancel.setOnClickListener(this); //获得系统的Vibrator...实例: myVibrator = (Vibrator) getSystemService(Service.VIBRATOR_SERVICE); } @Override public void
Vibrator 振动器,是手机自带的振动器哦,不要想成岛国用的那种神秘东西哦~~ Vibrator是Android给我们提供的用于机身震动的一个服务哦 更多详情可见官方API文档:Vibrator...首先添加震动权限: <uses-permission android:name="android.permission.VIBRATE" / 获得Vibrator实例: Vibrator mVibrator...= (Vibrator) getSystemService(VIBRATOR_SERVICE); 点击按钮,震动开启 mButton.setOnClickListener(new View.OnClickListener...{ @Override public void onClick(View v) { //震动30毫秒 mVibrator.vibrate(30); //todo } }); Vibrator...mVibrator.vibrate(new long[]{500, 100, 500, 100, 500, 100}, 0); //取消振动 mVibrator.cancel(); 参考文章: Vibrator
一 获取振动器Vibrator实例: Vibrator mVibrator = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE...); 二 Vibrator.vibrate()方法: 只有1个参数的时候,第一个参数用来指定振动的毫秒数。...vibrator = (Vibrator) getSystemService(VIBRATOR_SERVICE); long[] pattern = {3000, 1000, 2000...vibrator.vibrate(pattern, -1); // -1不重复,非-1为从pattern的指定下标开始重复 } @Override public boolean...vibrator = (Vibrator) getSystemService(VIBRATOR_SERVICE); vibrator.vibrate(10);
VIBRATOR_SERVICE获得,实现该功能的代码很简单,即便用Java书写也只有以下两行代码: Vibrator vibrator = (Vibrator) getSystemService...常规做法:从系统服务中获取震动器对象 val vibrator = getSystemService(Context.VIBRATOR_SERVICE) as Vibrator vibrator.vibrate...Vibrator。...: Vibrator get() = getSystemService(Context.VIBRATOR_SERVICE) as Vibrator 现在回到Activity代码,如下所示只要通过...vibrator就能访问震动器的方法了: //利用扩展函数实现扩展属性,直接使用vibrator即可指代震动器对象 vibrator.vibrate(3000) 当然要想正常访问自定义的扩展函数和扩展属性
@ohos.vibrator (振动) 作者:坚果,公众号:”大前端之旅“,哔哩哔哩,OpenHarmony布道师,OpenHarmony校源行开源大使,51CTO博客专家博主,阿里云博客专家。...导入模块 使用的时候,需要先导入对应的模块 import vibrator from '@ohos.vibrator'; 添加权限 然后需要添加权限 FA "reqPermissions": [...示例: import vibrator from '@ohos.vibrator'; @Entry @Component struct VibPage { @State message: string...this.message) .fontSize(50) .fontWeight(FontWeight.Bold).onClick(()=>{ vibrator.vibrate...} } 需要注意的是在HarmonyOS应用中,ApI8的 接口声明文件编写错误,需要手动修改下SDK目录下接口声明文件,文件路径Sdk\openharmony\8\ets\api@ohos.vibrator.d.ts
Android震动可以通过Vibrator类实现。...--震动--> 2、编写工具; 在Activity中获取Vibrator实例,并调用vibrate方法来触发震动: public class VibratorUtil { /** *...只震动一次 */ public static void Vibrate(final Activity activity, long milliseconds) { Vibrator...vib = (Vibrator) activity.getSystemService(Service.VIBRATOR_SERVICE); vib.vibrate(milliseconds...vib = (Vibrator) activity.getSystemService(Service.VIBRATOR_SERVICE); vib.vibrate(pattern, isRepeat
设置插入器,参数:Interpolator对象 匿名实现Interpolator接口,重写getInterpolation()方法,设置中自定义动画速率,传入一个flaot x 输入框的震动效果 获取Vibrator...对象,调用getSystemService()方法,参数:VIBRATOR_SERVICE 调用Vibrator对象的vibrate()方法,参数:毫秒 需要添加权限android.permission.VIBRATE...AnimationUtils.loadAnimation(this, R.anim.shake); et_phone.startAnimation(shake); //手机震动 vibrator.vibrate
然后看一下如何使用他的把: public class VibrateHelp { private static Vibrator vibrator; /** * @ClassName:VibrateHelp...SuppressWarnings("static-access") public static void vSimple(Context context, int millisecode) { vibrator...= (Vibrator) context.getSystemService(context.VIBRATOR_SERVICE); vibrator.vibrate(millisecode); }...= (Vibrator) context.getSystemService(context.VIBRATOR_SERVICE); vibrator.vibrate(pattern, repeate...= null) vibrator.cancel(); } } 这样的话我们的 震动帮助类就完成呢。 然后我们根据我们的源码来分析一下: 我们需要将Vibrator 实例化一下。
package com.example.test11; import android.app.Activity; import android.os.Bundle; import android.os.Vibrator...Activity { private EditText et_text; private Button btn_submit; /** * 手机振动器 */ private Vibrator...vibrator; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState...= (Vibrator) getSystemService(VIBRATOR_SERVICE); btn_submit.setOnClickListener(new OnClickListener...et_text.startAnimation(shake); /* * 震动的方式 */ // vibrator.vibrate
Vibrator服务提供的控制手机振动的接口,应用可以调用Vibrator的接口来让手机产生振动,达到提醒用户的目的。...mVibrator = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE); mVibrator.vibrate(500); Vibrator使用起来很简单...应用中使用的Vibrator实例是通过Context的一个方法getSystemService(Context.VIBRATOR_SERVICE)获得的,而Context的实现一般都在ContextImpl...(context); ServiceManager.addService("vibrator", vibrator); ......) { return new SystemVibrator(ctx); }}); 在应用中使用Vibrator的接口 Vibrator mVibrator = (Vibrator) getSystemService
vib = (Vibrator) activity.getSystemService(Service.VIBRATOR_SERVICE); vib.vibrate(milliseconds);.../以pattern[]方式震动 public static void vibrate(final Activity activity, long[] pattern,int repeat){ Vibrator...vib = (Vibrator) activity.getSystemService(Service.VIBRATOR_SERVICE); vib.vibrate(pattern,repeat...); } //取消震动 public static void virateCancle(final Activity activity){ Vibrator vib = (Vibrator) activity.getSystemService...(Service.VIBRATOR_SERVICE); vib.cancel(); } 调用时也非常简单,因为可能存在多个地方关闭震动,所以添加一个标志位来判断震动是否已经开启,防止多次关闭抛出异常
Vibrator vibrator = (Vibrator) activity.getSystemService(Context.VIBRATOR_SERVICE); 3.启动震动。...vibrator.vibrate(VIBRATE_DURATION); public void playBeepSoundAndVibrate() { if (enableVibrate)...{ Vibrator vibrator = (Vibrator) activity .getSystemService(Context.VIBRATOR_SERVICE...); //震动一次 vibrator.vibrate(VIBRATE_DURATION); //第一个参数,指代一个震动的频率数组。...//会一直保持循环,我们需要用 vibrator.cancel()主动终止 //vibrator.vibrate(new long[]{300,500},0);
e.printStackTrace(); } 调用震动需要权限授权申请 调用代码 Vibrator...vibrator = (Vibrator)getSystemService(Context.VIBRATOR_SERVICE); long [] pattern = {100,400,100,400}...; // 停止 开启 停止 开启 vibrator.vibrate(pattern,2); //重复两次上面的pattern 如果只想震动一次,index设为-1 //取消代码 vibrator.cancel
stub } // 摇晃监听接口 public interface OnShakeListener { public void onShake(); } } 二,控制震动 得到Vibrator...对象 vibrator=(Vibrator) getBaseContext().getSystemService(Context.VIBRATOR_SERVICE); 震动方法有两种 vibrator.vibrate...(500);震动多少毫秒 或者设置一个震动周期的数组 vibrator.vibrate(new long[]{100,200,100,200}, -1); //停止,震动。...vibrator; @Override public void onCreate() { // TODO Auto-generated method stub super.onCreate...(); vibrator=(Vibrator) getBaseContext().getSystemService(Context.VIBRATOR_SERVICE); mShakeListener
long lastTime; private int rawId; private SoundPool soundPool; //震动 private Vibrator...vibrator; @Override protected void onCreate(Bundle savedInstanceState) { x.Ext.setDebug...savedInstanceState); setContentView(R.layout.activity_main); x.view().inject(this); vibrator...= (Vibrator) getSystemService(VIBRATOR_SERVICE);//获取系统震动服务 sensorManager= (SensorManager) getSystemService...参数6 速率0.5-2 playAnim(); //摇动手机后,再伴随震动提示~~ vibrator.vibrate
的消息提示(震动和提示音),实现代码如下所示: public class VibratorUtil { protected AudioManager audioManager; protected Vibrator...vibrator; private Ringtone ringtone; private static final int MIN_TIME_OUT = 4000; //时间间隔 long...= (AudioManager) MyApp.getContext().getSystemService(Context.AUDIO_SERVICE); //此方法是由Context调用的 vibrator...= (Vibrator) MyApp.getContext().getSystemService(Context.VIBRATOR_SERVICE); //同上 } /** * 开启手机震动和播放系统提示铃声...AudioManager.RINGER_MODE_SILENT) { return; } long[] pattern = new long[]{0, 180, 80, 120}; vibrator.vibrate
public void onReceive(Context context, Intent intent) { if (intent.getAction().equals(ONCLICK)) { Vibrator...vibrator = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE); vibrator.vibrate(1000); }
import android.graphics.Rect; import android.os.Handler; import android.os.Message; import android.os.Vibrator...Handler (){ public void handleMessage(Message msg){ } }; //震动一下下咯 private void virbate(){ Vibrator...vibrator = (Vibrator) mContext.getSystemService(Context.VIBRATOR_SERVICE); vibrator.vibrate(200)
} } return result; } } long[] pattern = MorseCodeConverter.pattern(text); Vibrator...vibrator = (Vibrator)getSystemService(Context.VIBRATOR_SERVICE); vibrator.vibrate(pattern, -1);
领取专属 10元无门槛券
手把手带您无忧上云