在我的Android应用程序中..我发出通知..实际上我的应用程序是数字命理应用程序..所以我计划每天凌晨01:00从应用程序发送通知,用户可以获得新预测...我是能够使用警报管理器发送通知。但我的问题是每当用户再次打开此通知时,通知会反复发生..但我需要通知一天发送一次。用户打开通知后,无需在同一天收到通知。如果有人可以提供帮助请帮助。我在下面给出了我的代码。
MainActivity
AlarmManager am;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_main);
//Context context=MainActivity.this;
am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
setRepeatingAlarm();
private void setRepeatingAlarm()
{
// TODO Auto-generated method stub
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, 1);
calendar.set(Calendar.MINUTE, 00);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.AM_PM,Calendar.AM);
Intent intent = new Intent(this, AlarmReceiver.class);
// PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0,
// intent, PendingIntent.FLAG_CANCEL_CURRENT);
PendingIntent pendingIntent = PendingIntent.getBroadcast(MainActivity.this, 0, intent,0);
// am.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(),
// (1000 * 1000), pendingIntent);
am.set(1, System.currentTimeMillis(),pendingIntent);
System.out.println("Calling Alaram...");
}
MyReceiver
public class AlarmReceiver extends BroadcastReceiver
{
private final String SOMEACTION = "com.jocheved.alarm.ACTION";
@Override
public void onReceive(Context context, Intent intent) {
generateNotification(context,"You have new predictions");
String action = intent.getAction();
if (SOMEACTION.equals(action)) {
//do what you want here
generateNotification(context,"You have new Predictions");
}
}
@SuppressWarnings("deprecation")
private void generateNotification(Context context, String message) {
System.out.println(message+"++++++++++2");
int icon = R.drawable.ic_launcher;
long when = System.currentTimeMillis();
NotificationManager notificationManager = (NotificationManager) context
.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification(icon, message, when);
String title = context.getString(R.string.app_name);
// String subTitle = context.getString(R.string.app_name);
String subTitle = "You have new Predictions";
Intent notificationIntent = new Intent(context, DirectCalculation.class);
notificationIntent.putExtra("content", message);
PendingIntent intent = PendingIntent.getActivity(context, 0,notificationIntent, 0);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
| Intent.FLAG_ACTIVITY_SINGLE_TOP);
notification.setLatestEventInfo(context, title, subTitle, intent);
//To play the default sound with your notification:
notification.defaults |= Notification.DEFAULT_SOUND;
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notification.defaults |= Notification.DEFAULT_VIBRATE;
notificationManager.notify(0, notification);
}
}
答案 0 :(得分:2)
问题在于您在onCreate()
中设置闹钟,因此每次活动开始时,它会设置当前时间的闹钟,立即触发,启动活动,设置另一个闹钟。 .. 一遍又一遍地。您应该使用AlarmManager#setRepeating()
方法设置一次警报,并重复一天的间隔。
alarmMgr.setRepeating(AlarmManager.RTC, calendar.getTimeInMillis(),
AlarmManager.INTERVAL_DAY, intent);
每次创建活动时,您都不需要拨打setRepeatingAlarm()
,因此您需要确定如何跟踪其是否已设置。