我写了一个Android应用程序.现在,我想让设备在某个动作发生时振动.我怎样才能做到这一点?
我正在使用Android VIBRATOR_SERVICE
来为按键触摸提供触觉反馈.
((Vibrator) getSystemService(VIBRATOR_SERVICE)).vibrate(300);
Run Code Online (Sandbox Code Playgroud)
Android Studio警告我vibrate(interval)
不推荐使用该方法,我应该使用VibrationEffect
API> 23.
所以我使用VibrationEffect
的方法createOneShot
需要2个参数:间隔和幅度.
我试着寻找它但却不知道要传递什么amplitude
,任何人都知道如何使用它?
更新添加的代码
// Vibrate for 150 milliseconds
private void shakeItBaby() {
if (Build.VERSION.SDK_INT >= 26) {
((Vibrator) getSystemService(VIBRATOR_SERVICE)).vibrate(VibrationEffect.createOneShot(150,10));
} else {
((Vibrator) getSystemService(VIBRATOR_SERVICE)).vibrate(150);
}
}
Run Code Online (Sandbox Code Playgroud) 我正在使用通知编写应用程序.Google开发人员指南鼓励开发人员提供设置来自定义通知(禁用振动,设置通知声音......),因此如果用户设置通知,我会尝试禁用通知振动.
我NotificationCompat.Builder
用来创建通知,如下所示:
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(Application.getContext())
.setDefaults(Notification.DEFAULT_ALL)
.setPriority(Notification.PRIORITY_MAX)
.setSmallIcon(R.drawable.ic_launcher)
.setLargeIcon(largeIconBitmap)
.setAutoCancel(true)
.setContentIntent(resultPendingIntent)
.setContentTitle(title)
.setContentText(content);
Run Code Online (Sandbox Code Playgroud)
我尝试了不同的方法来禁用通知:
notificationBuilder.setVibrate(null);
notificationBuilder.setVibrate(new long[]{0l, 0l});
notificationBuilder.setDefaults(Notification.DEFAULT_ALL | ~Notification.DEFAULT_VIBRATE);
notificationBuilder.setDefaults(Notification.DEFAULT_LIGHTS | Notification.DEFAULT_SOUND);`
Run Code Online (Sandbox Code Playgroud)
我还尝试在结果对象上构建通知并更改值:
Notification notification = notificationBuilder.build();
notification.vibrate = null;
Run Code Online (Sandbox Code Playgroud)
但是当通知出现时,手机仍会振动.
如何禁用通知振动?
我正在制作一个应用程序,用户按住一个按钮,使手机振动,我不知道如何制作,所以只有当按钮按下它振动,我的代码到目前为止.
package one.two.bn;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.os.Vibrator;
import android.view.View;
import android.widget.Button;
import android.view.View.OnClickListener;
public class Vb extends Activity {
/** Called when the activity is first created. */
private Button button1;
private Vibrator vibrator;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
button1 = (Button)findViewById(R.id.button1);
button1.setOnClickListener(new View.OnClickListener() {
Vibrator vibrator = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
public void onClick(View v) {
if(v==button1){
vibrator.vibrate(300000);
}
}
Run Code Online (Sandbox Code Playgroud)
如果有任何可以帮助感谢很多.
当我触摸屏幕上的对象时,我试图让我的设备振动.我正在使用此代码:
Vibrator v = (Vibrator) getSystemService(getApplicationContext().VIBRATOR_SERVICE);
v.vibrate(300);
Run Code Online (Sandbox Code Playgroud)
使用清单文件中的权限但我似乎没有得到任何结果.有什么建议?此外,我的硬件支持振动.
我在收到通知时为特定功能定义了自定义振动.
但是,当手机屏幕关闭时,自定义振动会伴随默认通知振动.
我尝试将手机置于静音模式,并以编程方式将其从静音模式中删除,并尝试使用部分唤醒锁,怀疑当CPU关闭时,也会抛出默认振动,但这两种方法似乎都不起作用.
我还尝试将默认音频和振动静音并在完成接收通知的任务后恢复它,但这只会有助于屏蔽默认通知声音,而不是默认振动.
//Trying to mute the notification sound and vibration
audioManager.setStreamVolume(AudioManager.STREAM_NOTIFICATION, 0, AudioManager.FLAG_REMOVE_SOUND_AND_VIBRATE);
//Restoring the defaults
audioManager.setStreamVolume(AudioManager.STREAM_NOTIFICATION, defaultNotifVolume, AudioManager.FLAG_REMOVE_SOUND_AND_VIBRATE);
Run Code Online (Sandbox Code Playgroud)
请让我知道如何以编程方式禁用/启用默认通知振动.
我有一项服务可以检查我网站上的更新,我希望它能够在发现更新时引起振动,即使屏幕关闭或锁定。
当前仅当屏幕未关闭/锁定时振动才有效。即使屏幕关闭/锁定,所有其他功能也能正常工作。
Vibrator vibrator = (Vibrator) getSystemService(VIBRATOR_SERVICE);
long[] pattern = new long[]{0, 400, 200, 400};
if (Build.VERSION.SDK_INT >= 26) { // New API
vibrator.vibrate(VibrationEffect.createWaveform(pattern,0));
} else { // Old API
vibrator.vibrate(pattern, 0);
}
Run Code Online (Sandbox Code Playgroud)
即使屏幕关闭,如何使手机振动?我试过使用 aWakeLock
但这似乎不是问题?
我设置了所有权限,因为它在屏幕打开时工作。
我知道Vibrate类以及如何使用它,但在哪里可以使用系统默认振动模式?或者是否有某种意图我可以启动以使系统振动其默认振动?
我试图以编程方式生成Android振动模式,打开和关闭"微脉冲",以控制振动对最终用户的强度.这是我在一些类似主题中推荐的解决方案,即API没有提供控制振动强度的接口的问题(因为硬件的功能如我所知).
然而,用于生成这些模式的算法似乎只是暗示,但没有发布实际的算法.
我想要做的是,如果输入强度介于0.0f和1.0f之间,则会生成一个类似于以下模式的数组:
(zero intensity)
[20,0]
[9,1,9,1]
...
[3,1,3,1,3,1,3,1,3,1]
[2,1,2,1,2,1,2,1,2,1,2,1,2]
(half intensity)
[1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1]
[1,2,1,2,1,2,1,2,1,2,1,2,1,1]
[1,3,1,3,1,3,1,3,1,3]
...
[1,9,1,9]
(full intensity)
[0,20]
Run Code Online (Sandbox Code Playgroud)
编写这样的算法的任何帮助(或建议更好的策略来实现相同的目标)?
编辑:我已经添加了100个声望的赏金:)