简体   繁体   中英

android alert with sound, vibration

I need to add an alerting system to my application. When some data is read i need to alert the user of the data. I need a dialog to pop up letting the user know the time it occurred and tell them some other stuff. The user needs to click ok and that is it. Just to bring their attention to something. I need it to make noise and vibrate. I was looking into notifications and alertdialogs. AlertDialog seems to be what i want, however, i do not see anything about sounds and vibrations in the documentation for alertdialog. Is there noise and vibration for alertdialog? Or is there another way for me to do this with sounds and vibration? I need the window to stay active until the user presses ok.

Thanks!

I can't help you with the sound part but the Vibrate is easy:

Vibrator vibrator;
vibrator = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
vibrator.vibrate(500);

You also need the permission in the manifest.

<uses-permission android:name="android.permission.VIBRATE" />

For vibration there is a separate class which you have to implement. Seethis .

And another way is for noise is to use audiomanager or mediaplayer to play your sound.

This is a very simple way for vibrating your device.

import android.os.Vibrator;

 ...

 Vibrator v = (Vibrator) this.context.getSystemService(Context.VIBRATOR_SERVICE);
 // Vibrate for 500 milliseconds
 v.vibrate(500);

Note:

Don't forget to include permission in AndroidManifest.xml file:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM