简体   繁体   English

如何设置应用程序在每天的特定时间触发服务/警报?

[英]How to set app to trigger service/alarm at a specific time every day?

So I want my app to send a notification at 8:00 am everyday subjected to whether there is an event on that day or not. 因此,我希望我的应用每天早上8:00发送通知,具体取决于当天是否有活动。

I have a SQLlite DB which stores the dates on which notification is to be sent. 我有一个SQLlite数据库,其中存储要发送通知的日期。 What I want from my app, is this- Everyday at 8 in the morning, it should check if there is an event for today. 我想从我的应用程序中得到的是-每天早上8点,它应该检查是否有今天的活动。 And if there is an event for that day, then send a notification. 如果当天有活动,则发送通知。

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 am everyday. 我只需要实现应用程序每天早上8点检查数据库的部分。

**Update: ** I made a service which checks if the current time is 8:00 or not. **更新:**我提供了一项服务,检查当前时间是否为8:00。 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? 谁能帮帮我吗?

Use Pending Intent 使用待定意图

A pending intent is a token that you give to another application (eg, 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. 待定意图是您提供给另一个应用程序(例如,通知管理器,警报管理器或其他第三方应用程序)的令牌,该令牌使该另一个应用程序可以使用您的应用程序的权限来执行预定义的代码。

To perform a broadcast via a pending intent, get a PendingIntent via the getBroadcast() method of the PendingIntent class. 要通过挂起的意图执行广播,请通过PendingIntent类的getBroadcast()方法获取PendingIntent。 To perform an activity via a pending intent, you receive the activity via PendingIntent.getActivity(). 要通过挂起的意图执行活动,您可以通过PendingIntent.getActivity()接收活动。

Or 要么

PendingIntent + Alarm Manager + Broadcast Receiver combination can work fine you have to register and unregistere broadcast receiver dynamically not in manifest. PendingIntent +警报管理器+广播接收器组合可以很好地工作,您必须动态注册和注销广播接收器,而不是在清单中。 Like 喜欢

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

  }

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM