问题描述
我正在尝试在指定时间触发通知和警报。我已经将日志信息放到控制台上,以查看是否设置了正确的时间并且还可以。但是,仍然没有触发警报。请帮忙。
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());
}
}
请帮我做错了吗?
推荐答案
看看:
Take a look at the documentation of the setRepeating
method:
您的始终使用 setRepeating
警报会不准确,请尝试使用并在触发警报时对其进行重新编程。
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.
这篇关于在指定时间未触发警报和通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!