在指定时间未触发警报和通知 [英] alarm and notification not triggered at specified time

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

问题描述

我正在尝试在指定时间触发通知和警报。我已经将日志信息放到控制台上,以查看是否设置了正确的时间并且还可以。但是,仍然没有触发警报。请帮忙。

I am trying to trigger a notification and an alarm at a specified time. I have put log information to console to see if the correct time is being set and it is fine. However, still the alarm is not being triggered. Please help.

/ 用于创建通知和警报的代码 /

/Code for Creating Notification and Alarm/

btn_add_task.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                db.addTask(new DataModel(input_task_title.getText().toString(),input_task_desc.getText().toString(),
                        checkBox.isChecked(),txtDate.getText().toString(),txtTime.getText().toString(),isComplete));

                Calendar calendar = Calendar.getInstance();
                //Set notification for the set date and time
                calendar.set(Calendar.MONTH, month);
                calendar.set(Calendar.YEAR, year);
                calendar.set(Calendar.DAY_OF_MONTH, day);
                calendar.set(Calendar.HOUR_OF_DAY, hour);
                calendar.set(Calendar.MINUTE, minute);
                calendar.set(Calendar.SECOND, 0);
                if(mHour>12) {
                    calendar.set(Calendar.AM_PM, Calendar.PM);
                } else {
                    calendar.set(Calendar.AM_PM, Calendar.AM);
                }

                Log.i("Alarm at: ",calendar.get(Calendar.DAY_OF_MONTH)+"-"+
                        calendar.get(Calendar.MONTH)+"-"+
                        calendar.get(Calendar.YEAR)+" at "+
                        calendar.get(Calendar.HOUR_OF_DAY)+":"+
                        calendar.get(Calendar.MINUTE));

                Intent notifyMessage = new Intent(getApplicationContext(),NotificationMessage.class);
                PendingIntent pi = PendingIntent.getService(getApplicationContext(), 0, notifyMessage, PendingIntent.FLAG_UPDATE_CURRENT);
                AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
                am.setRepeating(AlarmManager.RTC_WAKEUP,calendar.getTimeInMillis(),AlarmManager.INTERVAL_DAY,pi);

                Toast.makeText(getApplicationContext(),R.string.new_task_added,Toast.LENGTH_LONG).show();
                startActivity(new Intent(AddTask.this,MainActivity.class));
            }
        });

/ 通知消息类别 /

/Notification Message Class/

package com.apps.katz.doer;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.support.v4.app.NotificationCompat;
import android.util.Log;

/**
 * Created by katz on 4/5/16.
 */
public class NotificationMessage extends BroadcastReceiver{
    @Override
    public void onReceive(Context context, Intent intent) {
        showNotification(context);
    }

    private void showNotification(Context context) {
        Log.i("notification","visible");
        PendingIntent contentIntent = PendingIntent.getActivity(context,0,
                new Intent(context,NotificationMessage.class),0);
        NotificationCompat.Builder mBuilder =
                new NotificationCompat.Builder(context).setSmallIcon(R.mipmap.ic_launcher)
                .setContentTitle("Doer Notification")
                .setContentText("This is a Doer Notification");
        mBuilder.setContentIntent(contentIntent);
        mBuilder.setDefaults(Notification.DEFAULT_SOUND);
        mBuilder.setDefaults(Notification.DEFAULT_VIBRATE);
        mBuilder.setDefaults(Notification.DEFAULT_LIGHTS);
        mBuilder.setAutoCancel(true);
        NotificationManager manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        manager.notify(1,mBuilder.build());
    }
}

请帮我做错了吗?

推荐答案

看看 setRepeating 方法的html#setRepeating(int,%20long,%20long,%20android.app.PendingIntent) rel = nofollow>文档

Take a look at the documentation of the setRepeating method:


注意:从API 19开始,所有重复的警报都是不精确的。如果您的应用程序需要精确的交付时间,则它必须使用一次性精确警报,并如上所述每次重新安排。 targetSdkVersion早于API 19的旧版应用程序将继续将其所有警报(包括重复警报)视为完全相同。

Note: as of API 19, all repeating alarms are inexact. If your application needs precise delivery times then it must use one-time exact alarms, rescheduling each time as described above. Legacy applications whose targetSdkVersion is earlier than API 19 will continue to have all of their alarms, including repeating alarms, treated as exact.

您的始终使用 setRepeating 警报会不准确,请尝试使用 setExact 并在触发警报时对其进行重新编程。

Your alarms will be inexact always using setRepeating, try using setExact and reprograming the alarm when it's triggered.

也可以尝试扩展 WakefulBroadcastReceiver 而不是 NotificationMessage 中的 BroadcastReceiver 可以在您的应用未运行时唤醒服务。

Also, try extending WakefulBroadcastReceiver instead of BroadcastReceiver in NotificationMessage in order to be able to wake the service when your app isn't running.

这篇关于在指定时间未触发警报和通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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