Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Vibrate on toast (Homer: Mmmm vibrate on toast)

Is it possible to make the phone vibrate for ANY toast message in your program? Or do you have to insert a vibrate command on each toast?

Cheers.

like image 701
Entropy1024 Avatar asked Nov 17 '10 16:11

Entropy1024


2 Answers

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" />
like image 130
Hazem Farahat Avatar answered Nov 01 '22 05:11

Hazem Farahat


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());
like image 1
Vincent Mimoun-Prat Avatar answered Nov 01 '22 07:11

Vincent Mimoun-Prat