简体   繁体   English

使用警报管理器的每日通知

[英]Daily notification using alarm manager

I have written this code that will be executed everyday and display a notification: 我写了这个代码,每天都会执行并显示一个通知:

class DailyNotification extends BroadcastReceiver {

    // Register the alarm and set it at 7am everyday (repeating mode)
    public static void registerAlarm(Context paramContext) {
        Calendar calendar = Calendar.getInstance();
        if (calendar.get(Calendar.HOUR_OF_DAY) >= 7) {
            calendar.add(7, 1);
        }
        calendar.set(Calendar.HOUR_OF_DAY, 7);
        calendar.set(Calendar.MINUTE, 00);
        calendar.set(Calendar.SECOND, 00);

        // PendingIntent that will perform a broadcast
        PendingIntent localPendingIntent = PendingIntent
                .getBroadcast(
                        paramContext,
                        22341,
                        new Intent(
                                "com.bestweightmanager.example.exampledailynotification.DAILY_NOTIFICATION"),
                        PendingIntent.FLAG_UPDATE_CURRENT);
        // Retrieve an AlarmManager to set a repeating daily alarm
        ((AlarmManager) paramContext.getSystemService("alarm")).setRepeating(1,
                calendar.getTimeInMillis(), 1000,
                localPendingIntent);
    }
}

The manifiest file looks like the following: 最明显的文件如下所示:

<receiver
    android:name=".utils.DailyNotification"
    android:process=":remote" >
    <intent-filter>
        <action android:name="com.bestweightmanager.example.exampledailynotification.DAILY_NOTIFICATION" />
        <action android:name="android.intent.action.EXTERNAL_APPLICATIONS_AVAILABLE" />
        <action android:name="android.intent.action.BOOT_COMPLETED" />
    </intent-filter>
</receiver>

But I don't get any notifications. 但我没有得到任何通知。 Can anyone suggest me how to solve this problem? 任何人都可以建议我如何解决这个问题?

Also, that what is meaning of below code 另外,这是下面代码的含义

Calendar calendar = Calendar.getInstance();
if (calendar.get(Calendar.HOUR_OF_DAY) >= 7) {
    calendar.add(7, 1);
}

Answering in parts. 部分回答。

1. 1。

Replace the line in your code 替换代码中的行

((AlarmManager) paramContext.getSystemService("alarm")).setRepeating(1,
                calendar.getTimeInMillis(), 1000,
                localPendingIntent);

with this line: 用这一行:

((AlarmManager) paramContext.getSystemService(Context.ALARM_SERVICE)).setRepeating(1,
                calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY,
                localPendingIntent);

You actually need to get system service - Context.ALARM_SERVICE 您实际上需要获得系统服务 - Context.ALARM_SERVICE

And instead of setting the repeat frequency to 1000 ms, you need to use AlarmManager.INTERVAL_DAY , so as to trigger it daily on the desired set time. 而不是将重复频率设置为1000毫秒,您需要使用AlarmManager.INTERVAL_DAY ,以便在所需的设置时间每天触发它。

2. 2。

About the meaning of this code: 关于这段代码的含义:

Calendar calendar = Calendar.getInstance();
if (calendar.get(Calendar.HOUR_OF_DAY) >= 7) {
    calendar.add(7, 1);
}

Here calendar.add(7, 1) does not makes perfect sense to me. 这里calendar.add(7, 1)对我来说并不是很有意义。 The add() function of calendar is used to add a given amount to a specific Calendar field, identified by a unique integer. 日历的add()函数用于将给定量添加到特定Calendar字段,由唯一整数标识。

If your code line calendar.add(7, 1); 如果您的代码行为calendar.add(7, 1); is replaced with calendar.add(Calendar.DATE, 1); 替换为calendar.add(Calendar.DATE, 1); , than this code might make some sense. ,这个代码可能会有所帮助。 It will than actually check that while registering the alarm, if the current Hour of Day is greater than 7 (ie, current day time has exceeded 7 AM), than it will set the calendar object (which is later used to register alarm) for 7 AM of next date (tomorrow). 如果当前的小时数大于7(即当前时间超过上午7点),它将实际检查注册警报时,将比设置日历对象(稍后用于注册警报)下一个日期上午7点(明天)。

3. 3。

Also I am doubtful about the declaration in your manifest file. 此外,我对您的清单文件中的声明表示怀疑。 You need to crosscheck it with the source from where you are referring. 您需要使用所引用的来源对其进行交叉检查。

In general, the structure of manifest file for AlarmManager should look somewhat like this: 通常,AlarmManager的清单文件结构应该如下所示:

<application>

    <activity>
        <intent-filter>
            <action/>

            <category/>
        </intent-filter>
    </activity>

    <receiver android:name=".DailyNotification" />
</application>

Hope that helps. 希望有所帮助。

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

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