如何设置应用程序在每天的特定时间触发服务/警报? [英] How to set app to trigger service/alarm at a specific time every day?

查看:129
本文介绍了如何设置应用程序在每天的特定时间触发服务/警报?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我希望我的应用在每天 8:00 发送通知,具体取决于当天是否有活动。

So I want my app to send a notification at 8:00 a.m. everyday subjected to whether there is an event on that day or not.

我有一个SQLlite数据库,其中存储了要发送通知的日期。我想从我的应用程序中得到的是-每天早上8点,它应该检查是否有今天的活动。如果当天有活动,则发送通知。

I have a SQLlite DB which stores the dates on which notification is to be sent. What I want from my app, is this- Everyday at 8 in the morning, it should check if there is an event for today. And if there is an event for that day, then send a notification.

我已经用数据库和通知实现了该部分。我只需要实现应用程序每天早上8点检查数据库的部分。

I have already implemented the part with the DB and the notification. I just need to implement the part where the app checks the DB at 8 a.m. everyday.

**更新:**我提供了一项服务,检查当前时间是否为8:00。以下是该服务的代码。

**Update: ** I made a service which checks if the current time is 8:00 or not. Following is the code for that service.

public int onStartCommand(Intent intent, int flags, int startId) {
    // TODO Auto-generated method stub

    System.out.println("inside on start command for service");

    myPrefs = this.getSharedPreferences("settings", this.MODE_PRIVATE);

    checkTime();

    return START_STICKY;
}

// to check if the time for alarm is here
private void checkTime() {
    // TODO Auto-generated method stub
    try{
    System.out.println("inside check time");

    Calendar cal;
    cal = Calendar.getInstance();

    if (08 == cal.get(cal.HOUR_OF_DAY) && 00 == cal.get(cal.MINUTE)) {
        // to call db and check for events
        nm = new MyNotificationManager(this);
        if (nm.checkEvent()) {
            nm.setNotifications();
        }
    }
    }catch(Exception e){
        System.out.println("inside exception for check time "+e.toString());
    }
}

问题在于服务仅检查时间一旦。如何使其每分钟检查一次时间?
谁能帮我吗?

The problem is that the service is only checking the time once. How to make it check the time every minute? Can anyone please help me?

推荐答案

使用待定意图

未决意图是您提供给另一个应用程序(例如,通知管理器,警报管理器或其他第三方应用程序)的令牌,该令牌使该另一个应用程序可以使用您的应用程序的权限来执行预定义的代码。

A pending intent is a token that you give to another application (e.g., notification manager, alarm manager or other 3rd party applications), which allows this other application to use the permissions of your application to execute a predefined piece of code.

要通过挂起的意图执行广播,请通过PendingIntent类的getBroadcast()方法获取PendingIntent。要通过挂起的意图执行活动,您可以通过PendingIntent.getActivity()接收活动。

To perform a broadcast via a pending intent, get a PendingIntent via the getBroadcast() method of the PendingIntent class. To perform an activity via a pending intent, you receive the activity via PendingIntent.getActivity().

PendingIntent +警报管理器+广播接收器组合可以正常工作,您必须动态注册和取消注册广播接收器,而不是在清单中。像

PendingIntent + Alarm Manager + Broadcast Receiver combination can work fine you have to register and unregistere broadcast receiver dynamically not in manifest. Like

public void onResume()
  {
    IntentFilter filter = new IntentFilter();
    filter.addAction("android.provider.Telephony.SMS_RECEIVED");
    registerReceiver(mybroadcast, filter);  

  }

  public void onPause()
  {
    unregisterReceiver(mybroadcast);
  }

这篇关于如何设置应用程序在每天的特定时间触发服务/警报?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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