我正在尝试在指定时间触发通知和警报。我已将日志信息添加到控制台以查看是否正在设置正确的时间并且没有问题。但是,仍然没有触发警报。请帮忙。
/ 创建通知和警报的代码 /
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));
}
});
/ 通知邮件类 /
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());
}
}
请帮助我做错了什么?
答案 0 :(得分:1)
查看app.use(subdomain('test-developer', developerRoutes));
方法的documentation:
注意:从API 19开始,所有重复警报都不准确。如果您的应用程序需要精确的交付时间,那么它必须使用一次性精确警报,每次重新安排如上所述。 targetSdkVersion早于API 19的旧应用程序将继续将所有警报(包括重复警报)视为完全警报。
您的警报将始终使用setRepeating
不准确,请尝试使用setExact并在触发警报时重新编程。
另外,请尝试在setRepeating
中扩展WakefulBroadcastReceiver
而不是BroadcastReceiver
,以便在您的应用未投放时唤醒服务。
答案 1 :(得分:0)
在btn_add_task监听器内部,将警报设置如下 -
Intent notifyMessage = new Intent(getApplicationContext(),NotificationMessage.class);
PendingIntent pi = PendingIntent.getBroadcast(getApplicationContext(), 0, notifyMessage, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
am.setRepeating(AlarmManager.RTC_WAKEUP,calendar.getTimeInMillis(),AlarmManager.INTERVAL_DAY,pi);
现在,在NotificationMessage类的onReceive中放入一个日志,以确保按时触发警报。 希望,现在你的程序将运行。