如何开发安卓闹钟应用程序

10

我尝试开发一个示例闹钟应用程序。我在谷歌和SC上搜索过,但它们的大多数示例令人困惑。如何创建具有以下要求的闹钟应用程序:

  1. 在我的主屏幕上,我有一个按钮,类似于“启动闹钟”,当我单击按钮时,必须启用时间选择器。

  2. 我可以根据自己的意愿选择时间,一旦我选择了时间,小部件上的闹钟图标就会启用。(例如,如果我们在默认移动闹钟应用中设置闹钟,则将启用该图标,表示已设置闹钟)。

  3. 当到达设置的时间(从TimePicker应用程序设置的时间)时,闹钟将响起。

这些是我的要求,我已完成前两个点,但我仍在努力设置闹钟。


2
是的,但我在过去的两天里一直在苦苦挣扎。 - Aerrow
兄弟,创建一个应用程序吧。首先,了解一些从应用程序创建小部件的知识。然后,在那里放置时间选择器。并且,将时间(以毫秒为单位)存储在数据库中,这是您想要设置闹钟的时间。在那个时间,只需从数据库中调用该时间并将其与当前时间(以毫秒为单位)进行比较。在所需的时间,只需从Alarm Manager中触发闹钟或从Notification Manager中发送通知。 - Praveenkumar
您有此方面的示例吗?因为我不知道如何做。 - Aerrow
只需尝试自己操作。您可以使用闹钟管理器示例创建应用程序。如果遇到任何困难,请在此处联系我。 - Praveenkumar
5个回答

10

请查看AlarmManager。如果您想同时使用闹钟,必须使用Service类。以下是样本代码 -

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分钟重复一次闹钟。请参阅计划重复闹钟文档。


我从您给的链接下载了代码,但运行代码后什么也没有发生。请问是否有运行此代码的特定步骤?请告诉我。 - Aerrow
请注意,输出将放置在您的日志记录和DDMS中,请在代码中查看。您只需根据自己的需求进行修改即可。 - Praveenkumar
@Aerrow 将 <receiver android:process=":remote" android:name=".OnBootReceiver"></receiver> 添加到你的 manifest 文件中。 - computerquest
你能帮我解决这个问题吗?https://stackoverflow.com/questions/51542099/cant-stop-the-ringing-alarm-from-another-activity/51543197#51543197 @Praveenkumar - Zhu

4

当您启用警报时,您需要调用内置的闹钟管理器并使用alarmmanager.set在管理器中设置警报时间。一旦将警报时间(以毫秒为单位)提供给闹钟管理器,它将发送消息,您可以通过接收器类检索消息

//creating and assigning value to alarm manager class
    Intent AlarmIntent = new Intent(MainActivity.this, AlarmReciever.class);
    AlarmManager AlmMgr = (AlarmManager)getSystemService(ALARM_SERVICE);
    PendingIntent Sender = PendingIntent.getBroadcast(MainActivity.this, 0, AlarmIntent, 0);    
    AlmMgr.set(AlarmManager.RTC_WAKEUP, Alarm.getTimeInMillis(), Sender);

要接收警报,您需要创建一个扩展Receiver的新类,在onReceive中设置意图以调用警报时间所需的活动,您还可以提供通知。

public class AlarmReciever extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) 
{   //Build pending intent from calling information to display Notification
    PendingIntent Sender = PendingIntent.getBroadcast(context, 0, intent, 0);
    NotificationManager manager = (NotificationManager)context.getSystemService(android.content.Context.NOTIFICATION_SERVICE);
    Notification noti = new Notification(android.R.drawable.stat_notify_more, "Wake up alarm", System.currentTimeMillis());
    noti.setLatestEventInfo(context, "My Alarm", "WAKE UP...!!!", Sender);
    noti.flags = Notification.FLAG_AUTO_CANCEL;
    manager.notify(R.string.app_name, noti); 

    //intent to call the activity which shows on ringing
    Intent myIntent = new Intent(context, Alarmring.class);
    myIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    context.startActivity(myIntent);

    //display that alarm is ringing
    Toast.makeText(context, "Alarm Ringing...!!!", Toast.LENGTH_LONG).show();
}}

如果您仍然遇到任何问题,请再次询问... :)

1

0

为了完成你的最后一个点,你需要进行日期比较并使用AlaramManager Alaram Doc,然后再次使用Service来比较下一个日期和时间。希望对你有所帮助。


我尝试了一些示例代码,但它们并不是很有效,程序可以正常运行,但没有警报声和指示。 - Aerrow
好的。然后在那里使用通知。或者使用铃声管理器或使用URI访问音调,参见此链接可能会有所帮助:http://developer.android.com/reference/android/media/RingtoneManager.html - OnkarDhane

0
你需要使用RingtoneManager或者NotificationManager(在屏幕顶部显示任何文本或图像来通知用户),或者你可以使用MediaPlayer在闹钟时间到达时播放声音。你必须在清单文件中设置<receiver>标签,其中必须包含一个扩展BroadCastReceiver的类。在接收器类中,你可以编写代码来唤醒设备。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接