Android Vibrate on toast (Homer: Mmmm vibrate on toast)
Is it possible to make 开发者_如何学Pythonthe phone vibrate for ANY toast message in your program? Or do you have to insert a vibrate command on each toast?
Cheers.
add this class to your code:
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();
}
}
and then you can call a toast by adding this line when you want to show a vibrating toast:
new VibratingToast(this, "Hi,....", Toast.LENGTH_SHORT);
You will also need, if you have't already, to add vibration permission in your manifest file
<uses-permission android:name="android.permission.VIBRATE" />
You could simply subclass the Notification class and have its vibrate command initialised in the Constructor. Then instead of using the SDK Notification class, use that one each time you need to notify in your application.
public class MyNotification extends Notification {
public MyNotification() {
super();
vibrate = /* Your vibration parameters here */;
// Or to use default vibration:
// flags = DEFAULT_VIBRATE;
}
}
Then, when you want to notify:
notificationManager.notify(new MyNotification());
精彩评论