我已经搜索并尝试了许多通知的源代码。现在我正在使用此代码发送通知:
报警主
System.out.println("Lancio sveglia");
Date dat = new Date();//initializes to now
Calendar cal_alarm = Calendar.getInstance();
Calendar cal_now = Calendar.getInstance();
cal_now.setTime(dat);
cal_alarm.setTime(dat);
cal_alarm.set(Calendar.HOUR_OF_DAY,18);//set the alarm time
cal_alarm.set(Calendar.MINUTE, 00);
cal_alarm.set(Calendar.SECOND,0);
//Create a new PendingIntent and add it to the AlarmManager
Intent intent2 = new Intent(this, AlarmReceiverActivity.class);
PendingIntent pendingIntent = PendingIntent.getService(this,
34567, intent2, PendingIntent.FLAG_CANCEL_CURRENT);
AlarmManager am =
(AlarmManager)getSystemService(Activity.ALARM_SERVICE);
am.cancel(pendingIntent);
am.set(AlarmManager.RTC_WAKEUP, cal_alarm.getTimeInMillis(), pendingIntent);
警报接收器活动
@Override
public void onCreate() {
//super.onCreate(savedInstanceState);
System.out.println("Arrivo in AlarmReceiverActivity");
Context context = this.getApplicationContext();
nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
CharSequence from = "[text]";
CharSequence message = "S[text]";
PendingIntent contentIntent = PendingIntent.getActivity(this, 001,
new Intent(), 0);
Notification notif = new Notification(R.drawable.ic_launcher,
"[text]", System.currentTimeMillis());
notif.setLatestEventInfo(context, from, message, contentIntent);
nm.notify(1, notif);
但是通知警报在某些设备中每 30 分钟重复一次,另一种则每 1 小时重复一次。但现在我还没有设置闹钟重复。
我如何设置系统以发送一次通知。有任何想法吗?任何帮助都会很棒