即使时间是未来时间,一旦设置了警报,就会显示警报通知

时间:2015-03-23 05:23:40

标签: android alarmmanager android-notifications

我在设置闹钟方面遇到了问题,我将时间用于alarmManager.Set方法,但是一旦闹钟被触发,我就会收到通知而不是在指定时间收到通知。请让我知道我在做什么错误。这是我第一次报警,请让我知道我的错误。欢迎所有建议。提前谢谢。

我的闹钟设置方法:

我正在接受用户输入的时间并格式化时间并将其提供给alarmManager.Set方法:

String givenDateString = dateTime;
    Log.d("Pana", "The value of Date Time is " +dateTime);
    SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy HH:mm", Locale.ENGLISH);
    sdf.setTimeZone(TimeZone.getTimeZone("GMT+0530"));
    try {
        Date mDate = sdf.parse(givenDateString);
        timeInMilliseconds = mDate.getTime();
        Log.d("Pana ", "Date in milli :: " + timeInMilliseconds);
    } catch (ParseException e) {
        e.printStackTrace();
    }




    // create an Intent and set the class which will execute when Alarm triggers, here we have
    // given AlarmReciever in the Intent, the onRecieve() method of this class will execute when
    // alarm triggers and
    //we will write the code to send SMS inside onRecieve() method pf Alarmreciever class
    Intent intentAlarm = new Intent(this, AlarmReceiverNotification.class);

    // create the object
    AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);

    //set the alarm for particular time
    alarmManager.set(AlarmManager.RTC_WAKEUP, timeInMilliseconds , PendingIntent.getBroadcast(this, 1, intentAlarm, PendingIntent.FLAG_UPDATE_CURRENT));
    Toast.makeText(this, "Alarm Scheduled for Tomorrow", Toast.LENGTH_LONG).show();


    Intent intent = new Intent(this, MyAlarmService.class);
    intent.putExtra("Time", timeInMilliseconds);
    startService(intent);

    boolean alarmUp = (PendingIntent.getBroadcast(getApplicationContext(), 0,
            new Intent("com.ms.t.tms.MY_UNIQUE_ACTION"),
            PendingIntent.FLAG_NO_CREATE) != null);

    if (alarmUp)
    {
        Log.d("myTag", "Alarm is already active");
    }

我的接收器类:

 public class MyAlarmService extends IntentService {

        private NotificationManager mManager;
        Notification notification;
        Intent intent;

        public MyAlarmService() {
            super("MyAlarmService");
        }

        /**
         * Creates an IntentService.  Invoked by your subclass's constructor.
         *
         * @param name Used to name the worker thread, important only for debugging.
         */
        public MyAlarmService(String name) {
            super(name);
        }

        @Override
        protected void onHandleIntent(Intent intent) {



        }

        @Override
        public void onStart(Intent intent, int startId) {
            super.onStart(intent, startId);




                Log.d("Pana", "Alarm is triggered");

                Long time = intent.getLongExtra("Time", 0);

                mManager = (NotificationManager) this.getApplicationContext().getSystemService(this.getApplicationContext().NOTIFICATION_SERVICE);
                Intent intent1 = new Intent(this.getApplicationContext(),MainActivity.class);

                notification = new Notification(R.mipmap.ic_launcher,"This is a test message!", time);
                intent1.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP| Intent.FLAG_ACTIVITY_CLEAR_TOP);

                PendingIntent pendingNotificationIntent = PendingIntent.getActivity( this.getApplicationContext(),0, intent1,PendingIntent.FLAG_UPDATE_CURRENT);
                notification.flags |= Notification.FLAG_AUTO_CANCEL;
                notification.setLatestEventInfo(this.getApplicationContext(), "AlarmManagerDemo", "This is a test message!", pendingNotificationIntent);

                mManager.notify(0, notification);
            }
        }

3 个答案:

答案 0 :(得分:3)

面临同样的问题,但终于能够解决它。

在我的情况下,我在使用SimpleDateFormat

解析时提供了错误的月份
SimpleDateFormat sdf = new SimpleDateFormat("d/M/yyyy HH:mm", Locale.ENGLISH); 

您可以使用

查看timeInMilliseconds是否正确的天气
Log.i(TAG, "Date in milli :: " + timeInMilliseconds);  

Log.i(TAG,"System.currentTimeMillis() = "+System.currentTimeMillis());  

如果timeInMilliseconds小于System.currentTimeMillis(),那么timeInMilliseconds就错了。

M 一年中的 1到12

在我的情况下,我从 0到11

开始提供月份

要解决此问题,我将月份增加1,然后使用SimpleDateFormat解析并解决问题。

答案 1 :(得分:1)

您绑定服务MyAlarmService时会立即显示您的通知,因为您在onStartCommand()方法中显示通知

您应该将Calender对象传递给MyAlarmService,以便您可以在该特定时间显示通知。您可以参考here

答案 2 :(得分:1)

以下是使用Alarm Manager类设置Alarm的代码。请记住,您的唯一ID应该是不同的,时间应该以毫秒为单位。

int uniqueId = (int) (System.currentTimeMillis() & 0xfffffff);

AlarmManager mAlarm = (AlarmManager) MainActivity.this
                                .getSystemService(MainActivity.this.ALARM_SERVICE);
ArrayList<PendingIntent> intentArray = new ArrayList<PendingIntent>();


// Unique id should be different all the time. So I used it with System current Millis seconds.

PendingIntent pendingIntent = PendingIntent.getBroadcast(MainActivity.this,
                                    uniqueId, intentR, 0);


// Give you time in milliseconds for the Alarm. I given Alarm time after 1 minute (1 * 60 * 10000

mAlarm.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, systemTime + 1 * 60 * 60 * 1000,
                                    pendingIntent);

intentArray.add(pendingIntent);