我写了一个Android应用程序.现在,我想让设备在某个动作发生时振动.我怎样才能做到这一点?
如何将iPhone设置为振动一次?
例如,当玩家失去生命或游戏结束时,iPhone应该振动.
我需要让iPhone振动,但我不知道如何在Swift中做到这一点.我知道在Objective-C中,你只需写:
import AudioToolbox
AudioServicesPlayAlertSound(kSystemSoundID_Vibrate);
Run Code Online (Sandbox Code Playgroud)
但那不适合我.
从iOS 5开始,用户可以为警报和响铃创建自定义振动模式.以下屏幕截图显示了用于创建一个UI(iOS 6中的Contacts应用程序)的UI:

我一直在搜索,包括文档,我找不到任何公开的API来揭示自定义振动的创建或回放.最接近的是使用AudioToolbox框架来发挥短暂的持续振动:
AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);
Run Code Online (Sandbox Code Playgroud)
有谁知道是否有自定义振动的API?它不一定是公共API.我很想知道联系人应用程序使用的是什么.有人知道吗?
PS其他人_CTServerConnectionCreate在CoreTelephony(例子)中提出了建议.我试过了,但由于某种原因无法振动.
2015年10月更新:
iOS 9中有新的私有API可与兼容设备中的Taptic Engine进行交互.有关详细信息,请参阅iOS 9中的Taptic.
我想在我的应用程序按钮中添加触觉反馈,并以编程方式控制它们以显示按钮状态(启用和禁用).默认触觉反馈设置器仅适用于长按.如何使其适用于简单的按钮点击.
有没有办法对触摸移动等事件进行触觉反馈?
正如标题所说,我升级到了 API 31。我有一个执行振动的函数,但是在行中
val vib = this.getSystemService(Context.VIBRATOR_SERVICE) as Vibrator
Run Code Online (Sandbox Code Playgroud)
VIBRATOR_SERVICE现在显示为已弃用。我怎样才能更换它?或者至少,API 31 及以上版本的现代解决方案是什么?
编辑:正如 Joachim Sauer 所写,替代方案是 VibrationManager。我现在需要的是使用 VibrationManager 的等效代码行。
我正在使用通知编写应用程序.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)
但是当通知出现时,手机仍会振动.
如何禁用通知振动?
该程序的功能如下:用户有一个电话号码列表,只有当没有其他系统范围的应用程序提供振动时(例如静音模式),手机才能在来电时振动.我知道这是违反规则的,因为应用程序应该尊重用户的设置,但应用程序仅限于某些具有此需求的用户.我尝试了两种方法,但它们都不令人满意:
收听电话状态并使用我自己的模式直接触发振动服务(带Vibrator.vibrate()).当手机处于CALL_STATE_RINGING状态时,此方法有效,没有来电但随机有效,我猜这是因为与系统范围的应用程序冲突,实际上处理来电时的振动.
判断手机是否在来电时振动AudioManager.shouldVibrate(),并决定是否更改振动设置(使用AudioManager.setRingerMode()和AudioManager.setVibrateSetting()).如果我的应用程序更改了振动设置,则一旦手机恢复CALL_STATE_IDLE状态,它们将被恢复.然而,这种方法有时仍然无法正常工作,没有任何原因.
我希望有人可以就这个问题提出一些建议.欢迎对这两种方式或其他建议的评论.
我不知道这个动画.
我怎么能通过xml这样做呢?或其他解决方案?
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/accelerate_decelerate_interpolator"
android:fillAfter="true">
......
</set>
Run Code Online (Sandbox Code Playgroud)
谢谢你的支持
大家好,有没有办法让按钮振动,但只有当if条件得到验证时?
这是代码:
Vibrator vibe = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE) ;
if(l2>=l1){
insertactone.setBackgroundColor(Color.RED);
};
Run Code Online (Sandbox Code Playgroud)
这是insertactone的onclick方法:
einsertactone = (Button) findViewById(R.id.bsqlinsertactone);
insertactone.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.bsqlinsertactone:
insertactoneClick();
break;
}
}
private void insertactoneClick() {
startActivity(new Intent(
"com.example.everydaybudgetplanner.ACTONESQLENTRY"));
}
});
Run Code Online (Sandbox Code Playgroud)
我希望它只有在验证IF条件时才会振动
TY全部提前
vibration ×10
android ×7
ios ×3
animation ×1
avfoundation ×1
cocoa-touch ×1
deprecated ×1
iphone ×1
java ×1
swift ×1