我搜索并尝试了许多通知源代码。 现在我正在使用此代码发送通知:
AlarmMain
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);
AlarmReceiverActivity
@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小时重复一次。 但现在我还没有设置警报重复。
如何设置系统以发送一次通知。 有任何想法吗? 任何帮助都会很棒