简体   繁体   English

Android AlarManager通知重复

[英]Android AlarManager Notification repeating

In My Android app.. I put a notification.. actually my app is a numerology app.. so I am planning to send a notification from app everyday 01.00 AM that new prediction is available for the user... I am able to send notification using alarm manager. 在我的Android应用中..我发出了通知..实际上我的应用是命理应用..因此,我计划每天01:00 AM从应用发送通知,通知用户可以使用新的预测...我能够发送使用警报管理器进行通知。 But my problem is whenever user open this notification again notification is coming repeatedly.. But I need notification to be send once in a day. 但是我的问题是,每当用户再次打开此通知时,通知就会反复出现。但是我需要每天发送一次通知。 Once user opens the notification no need of coming the notification in the same day. 用户打开通知后,无需在同一天收到通知。 If anyone can help please help. 如果有人可以帮助请帮助。 I am giving my code below. 我在下面给出我的代码。

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 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);



    }

}

The problem is that you're setting the alarm in onCreate() , so every time the Activity starts, it sets an alarm for the current time, which fires immediately, starts the Activity, sets another alarm... over and over again. 问题是您在onCreate()设置了警报,因此每次Activity启动时,它都会为当前时间设置一个警报,该警报将立即触发,启动Activity,一次又一次地设置另一个警报。 You should use the AlarmManager#setRepeating() method to set the alarm once, with a repeating interval of a day. 您应该使用AlarmManager#setRepeating()方法设置警报一次,并以一天为间隔重复一次。

alarmMgr.setRepeating(AlarmManager.RTC, calendar.getTimeInMillis(),
    AlarmManager.INTERVAL_DAY, intent);

You don't need the call to setRepeatingAlarm() each time the Activity is created, so you'll need to determine how you want to keep track of whether it is set. 您不需要在每次创建Activity时都调用setRepeatingAlarm() ,因此您需要确定如何跟踪是否设置了Activity。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM