如何开发Android应用程序警报警报、应用程序、Android

2023-09-06 03:22:41 作者:〃誓言不过是一时的失言″

我试图开发一个示例应用程序报警。我被搜查谷歌和SC,大部分的例子困惑我。如何将创建一个报警的应用程序有以下要求,

I tried to develop a sample Alarm Application. I was searched Google and SC, most of the examples confusing me. How will create an alarm application with the following requirements,

在我的主屏幕我有一个按钮,如启动报警,当我点击该按钮时间选择器必须启用。

In My Home screen i have a button, like "START ALARM", when i click the button a time picker must enable.

我选择的时间,我想,一旦我挑时间,报警图标将小窗口开启。 (例如,如果我们设置在默认的移动应用程序报警报警时,图标将启用,指示报警设置)。

I select the time as my wish, once i pick the time, the alarm icon will enabled on widget. (For example if we set the alarm in default mobile Alarm application, the icon will enabled, that indicates the alarm is set).

在时间到达(被设置形式TimePicker时间),报警器就会发出蜂鸣声。

At the time is reached (the time which is set form TimePicker), the alarm will beeps.

这些都是我的要求,我完成了第一个两分,但我仍然在努力设置报警。

These are my requirements, i finished the first two points, but I'm still struggling to set alarm.

推荐答案

看看的 AlarmManager 。而且,如果你想使用报警同时,你必须使用服务类的。而且,请参见下面示例code -

Take a look at AlarmManager. And, If you want to use alarm simultaneously you must use Service class for that. And, see below sample code -

public class OnBootReceiver extends BroadcastReceiver {
  private static final int PERIOD=300000;  // 5 minutes

  @Override
  public void onReceive(Context context, Intent intent) {
    AlarmManager mgr =
      (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
    Intent i=new Intent(context, OnAlarmReceiver.class);
    PendingIntent pi=PendingIntent.getBroadcast(context, 0, i, 0);
    mgr.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, 
      SystemClock.elapsedRealtime()+60000, PERIOD, pi);
  }

这将重复报警每6分钟。请参见调度重复报警的文档。

This will repeat the alarm with every 6 Mins. See Scheduling Repeating Alarms document.