简体   繁体   English

带有声音,振动的android警报

[英]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. AlertDialog 似乎是我想要的,但是,我在 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.另一种处理噪音的方法是使用 audiomanager 或 mediaplayer 来播放您的声音。

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:不要忘记在 AndroidManifest.xml 文件中包含权限:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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