本文介绍了如何开始在Android立即报警的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图写一个code有事时激活报警。我想过使用默认的报警及消防它,当我考虑,但首先我不知道如何来触发报警或处理。第二:我不知道是否有火灾立即报警,听起来并告诉用户一个消息的最简单的方法
I'm trying to write a code that activates an alarm when something happens. I've thought on using the default alarm and fire it when I consider it, but first: I don't know how to fire the alarm or handle it. Second: I don't know if there is an easiest way to fire an immediate alarm that sounds and tells the user a message.
例如:
(真)
火灾声音报警,提示信息提醒用户
其他
其他一些code
if (true) Fire an audible alarm with a message to advise the userelse Some other code
感谢您的帮助。
推荐答案
AlarmManager:
AlarmManager:
public void startAlert() {
Intent intent = new Intent(this, MyBroadcastReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(
this.getApplicationContext(), 234324243, intent, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis()
+ (i * 1000), pendingIntent);
Toast.makeText(this, "Alarm set in " + i + " seconds",
Toast.LENGTH_LONG).show();
}
广播接收器:
public class MyBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "Don't panik but your time is up!!!!.",
Toast.LENGTH_LONG).show();
// Vibrate the mobile phone
Vibrator vibrator = (Vibrator)
context.getSystemService(Context.VIBRATOR_SERVICE);
vibrator.vibrate(2000);
}
}
这篇关于如何开始在Android立即报警的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!