我尝试制作一个简单的应用程序让我的 phone 振动
I tried to make a simple application to let my phone vibrate
import android.content.Context;
import android.os.Vibrator;
Context context;
Vibrator v = (Vibrator) context.getSystemService(Context.VIBRATE_SERVICE);
void setup(){}
void draw(){
v.vibrate(1000);
noLoop();
}
PS。我通过设置给了振动权限
那我做错了什么?
使用这个
Vibrator v = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE);
而不是这个
Vibrator v = (Vibrator) context.getSystemService(Context.VIBRATE_SERVICE);
NOTE :
Don't forgot to initialize your context
更新
Vibrator.vibrate()
已弃用
使用这个
void draw(){
if (Build.VERSION.SDK_INT >= 26) {
v.vibrate(VibrationEffect.createOneShot(1000, VibrationEffect.DEFAULT_AMPLITUDE));
} else {
v.vibrate(1000);
}
}
示例代码
import android.content.Context;
import android.os.Build;
import android.os.VibrationEffect;
import android.os.Vibrator;
public class Tester
{
Context context;
Vibrator vibrator;
public Tester(Context context) {
this.context = context;
vibrator = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE);
}
void setup(){}
void draw(){
if (Build.VERSION.SDK_INT >= 26) {
vibrator.vibrate(VibrationEffect.createOneShot(150, VibrationEffect.DEFAULT_AMPLITUDE));
} else {
vibrator.vibrate(150);
}
}
}
像这样调用这个方法
Tester tester= new Tester(this);
tester.draw();
import android.content.Context;
import android.os.Vibrator;
Context context;
Vibrator v = (Vibrator) context.getSystemService(Context.VIBRATE_SERVICE);
void setup(){}
void draw(){
v.vibrate(1000);
noLoop();
}
PS。我通过设置给了振动权限
那我做错了什么?
使用这个
Vibrator v = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE);
而不是这个
Vibrator v = (Vibrator) context.getSystemService(Context.VIBRATE_SERVICE);
NOTE : Don't forgot to initialize your
context
更新
Vibrator.vibrate()
已弃用
使用这个
void draw(){
if (Build.VERSION.SDK_INT >= 26) {
v.vibrate(VibrationEffect.createOneShot(1000, VibrationEffect.DEFAULT_AMPLITUDE));
} else {
v.vibrate(1000);
}
}
示例代码
import android.content.Context;
import android.os.Build;
import android.os.VibrationEffect;
import android.os.Vibrator;
public class Tester
{
Context context;
Vibrator vibrator;
public Tester(Context context) {
this.context = context;
vibrator = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE);
}
void setup(){}
void draw(){
if (Build.VERSION.SDK_INT >= 26) {
vibrator.vibrate(VibrationEffect.createOneShot(150, VibrationEffect.DEFAULT_AMPLITUDE));
} else {
vibrator.vibrate(150);
}
}
}
像这样调用这个方法
Tester tester= new Tester(this);
tester.draw();