Android 事件:响铃、振动并显示警报框

发布于 2024-11-30 19:39:05 字数 81 浏览 3 评论 0原文

我正在尝试在 Android 中创建一个事件,当到达 schedules 事件的时间时,我想显示一个警报框,让手机响铃和振动。有人可以帮忙吗? 谢谢

I am trying to create an event in Android and when the time of the scedules event is reach, I want to display an alert box, ring and vibrate the phone. Can someone please help on this?
Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

呆头 2024-12-07 19:39:05

来自 Handler 类文档

消息的调度是通过post(Runnable)来完成的,
postAtTime(可运行,长),postDelayed(可运行,长),
sendEmptyMessage(int), sendMessage(消息),
sendMessageAtTime(Message, long) 和 sendMessageDelayed(Message,
长)方法。后期版本允许您将 Runnable 对象排入队列
消息队列收到消息后调用;这
sendMessage 版本允许您将包含以下内容的 Message 对象排入队列
将由 Handler 处理的数据包
handleMessage(Message) 方法(要求您实现一个子类
处理程序)。

更多信息可以在此处找到。在您的情况下,您似乎需要 postAtTimepostDelayed。代码看起来像这样

new Handler().postDelayed(new Runnable() {
    public void run() {
        //show alert, vibrate and ring..
    }
}, 10000);//execute this Runnable in 10 sec

From the Handler class documentation

Scheduling messages is accomplished with the post(Runnable),
postAtTime(Runnable, long), postDelayed(Runnable, long),
sendEmptyMessage(int), sendMessage(Message),
sendMessageAtTime(Message, long), and sendMessageDelayed(Message,
long) methods. The post versions allow you to enqueue Runnable objects
to be called by the message queue when they are received; the
sendMessage versions allow you to enqueue a Message object containing
a bundle of data that will be processed by the Handler's
handleMessage(Message) method (requiring that you implement a subclass
of Handler).

More information can be found here. In your case it seems that you need either postAtTime or postDelayed. The code would look something like

new Handler().postDelayed(new Runnable() {
    public void run() {
        //show alert, vibrate and ring..
    }
}, 10000);//execute this Runnable in 10 sec
眼角的笑意。 2024-12-07 19:39:05

您需要使用 AlarmManager 设置警报。
然后,您需要一个广播接收器来在时间到时执行您的任务。
要响铃,您需要使用您喜欢的声音(通知/铃声/警报)的 URI 来使用 MediaPlayer,要振动,您将使用振动器服务:
振动器 v = (振动器) getSystemService(Context.VIBRATOR_SERVICE);

现在您已经有了大概的了解,我建议您阅读一些示例和 Android 网站以了解更多详细信息...

You will need to set an alarm using the AlarmManager.
Then you need a BroadcastReceiver to perform your tasks when the time is up.
To ring you would need to use the MediaPlayer using the URI of your preferred sound (notification / ringtone / alarm) and to vibrate you will be using the Vibrator service:
Vibrator v = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);

Now that you have the general idea I suggest you read some examples and the Android website for more details...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文