如何获得警报触发通知 [英] How to get an alarm to trigger a notification

查看:120
本文介绍了如何获得警报触发通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要设置,将触发一次重复报警,启动一个通知。

I need to set a repeating alarm that will once fired, launch a notification.

步骤:


  1. 我需要一些最新资讯,(只是有一个想法,也许我应该推出一个意图,以显示设置闹钟功能)的接口。的细节的产品是需要被送入报警方法的日期时间字符串。

  2. 当设置报警我需要创建它的一个引用,因此它可以被用户删除。 (或者也许不是,什么是最好的做法吗?也许用户只能删除报警部分报警)

  3. 当警报响起的通知将被创建。

我不明朗的报警系统是如何工作的。报警probaly应该保持沉默,可能与振动。是直截了当设置?

I'm uncertain of how the alarm system works. The alarm probaly should be silent, maybe with vibration. Is that straight forward to setup?

我是否需要服务或将广播reciever做的工作?

Do I need a service or will a broadcast reciever do the job?

基本上我只需要几个指针。我在思考这个正确?是否有一个教程在那里(我还没有发现任何东西)。先谢谢了。

Basically I just need a few pointers. Am I thinking about this correctly? Is there a tutorial out there (I haven't found anything). Thanks in advance.

推荐答案

下面是我如何在我的应用程序使用AlarmService的演练。

Here is a walkthrough on how I use an AlarmService in my app.


  1. 设置一个AlarmManager在x分钟火。

  1. Set up an AlarmManager to fire in x minutes.

在响应报警,启动服务。

In response to the alarm, start a service.

创建您的通知,并让您的服务为自己设定了一个新的报警在另​​一个x分钟再次触发。

Create your notification and have your service set itself up with a new Alarm to fire again in another x minutes.

该服务自行关闭。

1

    Intent alarmIntent = new Intent(this, MyAlarm.class);
    long scTime = 60* 10000;// 10 minutes
    PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, alarmIntent, 0);
    AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
    alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + scTime, pendingIntent);

2

    public class MyAlarm extends BroadcastReceiver
    {

       @Override
       public void onReceive(Context context, Intent intent) {
          Log.d("Alarm Recieved!", "YAAAY");
          Intent i = new Intent(context, InviteService.class);
          context.startService(i);
       }
    }

3。

      public class InviteService extends IntentService
 {

  /** 
   * A constructor is required, and must call the super IntentService(String)
   * constructor with a name for the worker thread.
   */

  public InviteService() {
      super("InviteService");
  }

  /**
   * The IntentService calls this method from the default worker thread with
   * the intent that started the service. When this method returns, IntentService
   * stops the service, as appropriate.
   */

  @Override
  protected void onHandleIntent(Intent intent) {

                  String ns = Context.NOTIFICATION_SERVICE;
                  NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns);

                  int icon = R.drawable.logo;
                  CharSequence tickerText = "New Invite!";
                  long when = System.currentTimeMillis();

                  Notification notification = new Notification(icon, tickerText, when);
                  notification.flags |= Notification.FLAG_AUTO_CANCEL;
                  notification.defaults |= Notification.DEFAULT_VIBRATE;

                  Context context = getApplicationContext();
                  CharSequence contentTitle = "Title";
                  CharSequence contentText = "Text";
                  Intent notificationIntent = new Intent(this, Destination.class);
                  Bundle partyBundle = new Bundle();                    

                  PendingIntent contentIntent = PendingIntent.getActivity(this, SOME_ID, notificationIntent, 0);

                  notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);

                  int NOTIFICATION_ID = SOME_ID;

                  Log.d("NOTIFICATION_ID", "" + NOTIFICATION_ID);
                  mNotificationManager.notify(NOTIFICATION_ID, notification);

4(在同一个班)

4.(In the Same Class)

      Intent alarmIntent = new Intent(this, MyAlarm.class);
      long scTime = 60*1000;//mins
      PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, alarmIntent, 0);
      AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
      alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + scTime, pendingIntent);

      stopService(intent);
      }
    }

希望这有助于!

Hope this helps!

修改

为什么要使用一个服务?

Why use a Service?

这不是明智的做法是做多的处理在广播接收器。虽然你可以在BroadcastReciever做一些处理,它是安全做一个服务,你可以找到在这个计算器问题的广播接收器VS服务

It's not wise to do much processing in the BroadcastReceiver. Although you can do some processing in the BroadcastReciever, it is safer to do this in a Service, you can find some information in this StackOverflow question BroadcastReceiver vs Service

这篇关于如何获得警报触发通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆