Android Vibrate on toast(荷马:Mmmm振动吐司)

Ent*_*024 8 android toast vibration

是否可以让手机在程序中为任何Toast消息振动?或者你必须在每个吐司上插入一个振动命令?

干杯.

Haz*_*hat 11

将此类添加到您的代码中:

import android.content.Context;
import android.os.Vibrator;
import android.widget.Toast;;

public class VibratingToast extends Toast{

public VibratingToast(Context context,CharSequence text, int duration) {
    super(context);
    Vibrator v = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE);
    v.vibrate(300); 
    super.makeText(context, text, duration).show();
}
Run Code Online (Sandbox Code Playgroud)

}

然后你可以在想要展示振动吐司的时候添加这一行来召唤祝酒词:

new VibratingToast(this, "Hi,....", Toast.LENGTH_SHORT);
Run Code Online (Sandbox Code Playgroud)

如果您还没有,还需要在清单文件中添加振动权限

<uses-permission android:name="android.permission.VIBRATE" />
Run Code Online (Sandbox Code Playgroud)


Vin*_*rat 1

您可以简单地子类化通知类并在构造函数中初始化其振动命令。然后,每次需要在应用程序中发出通知时都使用该类,而不是使用 SDK 通知类。

public class MyNotification extends Notification {
    public MyNotification() {
        super();
        vibrate = /* Your vibration parameters here */;
        // Or to use default vibration:
        // flags = DEFAULT_VIBRATE;
    }
}
Run Code Online (Sandbox Code Playgroud)

然后,当您想要通知时:

notificationManager.notify(new MyNotification());
Run Code Online (Sandbox Code Playgroud)