设置重复闹钟

时间:2017-05-02 14:12:12

标签: android alarm schedule

9我制作的应用程序会提醒您在某个时间服用药物,因此用户会告诉我频率和时间段,例如:

频率= 8小时 期限= 7天

所以我会发出警报,每7小时触发7天。这是我现在的代码,但它没有按照我想要的方式工作......(它同时发送所有通知,因此在上面的示例中,它一次发送21个通知,我运行代码后几秒钟,这意味着它不是发送第一个代码,而是发送所有代码)

private void makeAlarm(MedAlarm mAlarm) {

    String medName = mAlarm.getMedName();
    String medDescripcion = mAlarm.getMedDescription();
    String frequency = mAlarm.getMedIntervalHours();
    String duration = mAlarm.getMedIntervalDays();
    int lastitem = ((24 / Integer.parseInt(frequency)) * 
    Integer.parseInt(duration)) - 1;

    String alarmMsg = medName;

    for (int k = 0; k < ((24 / Integer.parseInt(frequency)) * 
         Integer.parseInt(duration)); k++) {// hours

        setAlarm(Integer.parseInt(frequency) * (k + 1), alarmMsg, k + "", 
         "0");
        Log.i("setAlarm", k + " - ");
        Log.i("duration", Integer.parseInt(frequency) * (k + 1) + "");

        if (k == lastitem) {
            setAlarm(Integer.parseInt(frequency) * (k + 1), alarmMsg, k + "", 
            "1");
        }
    }
}

/**
 * @param duration
 * @param name
 * @param id
 * @param lastitem 0 normal item, 1 last item
 */
private void setAlarm(int duration, String name, String id, String lastitem) 
{

    Calendar calendar = Calendar.getInstance();
    calendar.setTimeInMillis(System.currentTimeMillis());

    Intent alertIntent = new Intent(this, AlertReceiver.class);
    alertIntent.putExtra("message", name);
    alertIntent.putExtra("id", id);
    alertIntent.putExtra("lastitem", lastitem);
    PendingIntent pi = PendingIntent.getBroadcast(this, duration++, 
    alertIntent, PendingIntent.FLAG_UPDATE_CURRENT);

    int alarmType = AlarmManager.ELAPSED_REALTIME;
    final int FIFTEEN_SEC_MILLIS = 15000;
    final int HOURS_MILLIS = 3600000 * duration;
    AlarmManager alarmManager = (AlarmManager)
            this.getSystemService(this.ALARM_SERVICE);

    alarmManager.setRepeating(alarmType, SystemClock.elapsedRealtime() + 
    FIFTEEN_SEC_MILLIS,
            HOURS_MILLIS, pi);

    Log.i("intent sent", HOURS_MILLIS + " || ");
}

类AlertReciver是发送通知的那个。

如果你可以帮助我,告诉我我错过了什么,或者我是否应该以不同的方式接受这个问题。

谢谢。

1 个答案:

答案 0 :(得分:0)

使用import requests from lxml import html url = "http://www.supplyhouse.com/" def Selectivelinks(address): response = requests.get(address) tree = html.fromstring(response.text) titles = tree.xpath('//ul[@id="shop-by-category-list"]') for title in titles: links=title.xpath('.//a/@href') for lnk in links: print(lnk) Selectivelinks(url) 代替alarmManager.set()

更新您的alarmManager.setRepeating()方法,如下所示:

setAlarm()

希望这会有所帮助〜