过去的通知显示当前通知

时间:2013-12-16 20:04:53

标签: java android notifications alarmmanager

我正在使用AlarmManager来重复闹钟。我已经为星期一,星期四和星期六设置了重复闹钟。警报在星期一正确显示,但在星期四到来时,它会同时显示周一和周四的两个通知,并在星期六显示所有三个通知。如何对其进行编码,以便在第一次警报完成时,它不显示在同一周的后续警报中,而是显示在下一周?

EDIT1 - 代码

MainActivity

public boolean onOptionsItemSelected(MenuItem item) {
        // TODO Auto-generated method stub

        switch (item.getItemId()) {
        case R.id.action_settings:
            EnableNoti();
            return true;
        }
        return super.onOptionsItemSelected(item);

    }

    private void EnableNoti() {
        Calendar calendar = Calendar.getInstance();
        Calendar calendar1 = Calendar.getInstance();
        Calendar calendar2 = Calendar.getInstance();
        Calendar calendar3 = Calendar.getInstance();

        calendar.set(Calendar.DAY_OF_WEEK, 1); // Sunday
        calendar1.set(Calendar.DAY_OF_WEEK, 4); // Wednesday
        calendar2.set(Calendar.DAY_OF_WEEK, 6); // Friday
        calendar3.set(Calendar.DAY_OF_WEEK, 5); // Thursday
        calendar3.set(Calendar.DAY_OF_WEEK_IN_MONTH, 1); // First Thursday of
                                                            // Each Month
        Date now = new Date(System.currentTimeMillis());
        if (calendar3.getTime().before(now)) {
            calendar3.add(Calendar.MONTH, 1);
        } else if (calendar.getTime().before(now)) {
            calendar.add(Calendar.HOUR, 168);
        } else if (calendar1.getTime().before(now)) {
            calendar1.add(Calendar.HOUR, 168);
        } else if (calendar2.getTime().before(now)) {
            calendar2.add(Calendar.HOUR, 168);
        }

        // Sunday
        calendar.set(Calendar.HOUR_OF_DAY, 9);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.AM_PM, Calendar.AM);

        // Wednesday
        calendar1.set(Calendar.HOUR_OF_DAY, 17);
        calendar1.set(Calendar.MINUTE, 30);
        calendar1.set(Calendar.SECOND, 0);
        calendar1.set(Calendar.AM_PM, Calendar.PM);
        // Friday
        calendar2.set(Calendar.HOUR_OF_DAY, 17);
        calendar2.set(Calendar.MINUTE, 30);
        calendar2.set(Calendar.SECOND, 0);
        calendar2.set(Calendar.AM_PM, Calendar.PM);

        // Thursday
        calendar3.set(Calendar.HOUR_OF_DAY, 22);
        calendar3.set(Calendar.MINUTE, 0);
        calendar3.set(Calendar.SECOND, 0);
        calendar3.set(Calendar.AM_PM, Calendar.PM);

        Intent myIntent = new Intent(TheBeginningEnglish.this,
                MyReceiverEnglish.class);
        pendingIntent = PendingIntent.getBroadcast(TheBeginningEnglish.this, 0,
                myIntent, 0);

        Intent myIntent1 = new Intent(TheBeginningEnglish.this,
                MyReceiver1English.class);
        pendingIntent1 = PendingIntent.getBroadcast(TheBeginningEnglish.this,
                0, myIntent1, 0);

        Intent myIntent2 = new Intent(TheBeginningEnglish.this,
                MyReceiver2English.class);
        pendingIntent2 = PendingIntent.getBroadcast(TheBeginningEnglish.this,
                0, myIntent2, 0);

        Intent myIntent3 = new Intent(TheBeginningEnglish.this,
                MyReceiver3English.class);
        pendingIntent3 = PendingIntent.getBroadcast(TheBeginningEnglish.this,
                0, myIntent3, 0);

        AlarmManager alarmMgr = (AlarmManager) getSystemService(ALARM_SERVICE);
        alarmMgr.setRepeating(AlarmManager.RTC_WAKEUP,
                calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY * 7,
                pendingIntent); // every Sunday
        alarmMgr.setRepeating(AlarmManager.RTC_WAKEUP,
                calendar1.getTimeInMillis(), AlarmManager.INTERVAL_DAY * 7,
                pendingIntent1); // every Wednesday
        alarmMgr.setRepeating(AlarmManager.RTC_WAKEUP,
                calendar2.getTimeInMillis(), AlarmManager.INTERVAL_DAY * 7,
                pendingIntent2); // every Friday
        alarmMgr.setRepeating(AlarmManager.RTC_WAKEUP,
                calendar3.getTimeInMillis(), AlarmManager.INTERVAL_DAY * 31,
                pendingIntent3); // every first Thursday
    }

Receiver.java

public class MyReceiverEnglish extends BroadcastReceiver {
    @Override
     public void onReceive(Context context, Intent intent)
    {
       Intent service = new Intent(context, MyAlarmServiceEnglish.class);
       context.startService(service);

     }

}

AlarmService.java

public class MyAlarmServiceEnglish extends Service {
    int currentapiVersion = android.os.Build.VERSION.SDK_INT;
    private NotificationManager mManager;
    Notification notification;

    // Notification notification;

    @Override
    public IBinder onBind(Intent arg0) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public void onCreate() {
        // TODO Auto-generated method stub
        super.onCreate();
    }

    @Override
    public void onStart(Intent intent, int startId) {
        onStartCommand(intent, 0, startId);
    }

    @SuppressWarnings({ "static-access", "deprecation" })
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {

        mManager = (NotificationManager) this.getApplicationContext()
                .getSystemService(
                        this.getApplicationContext().NOTIFICATION_SERVICE);

        Intent intent1 = new Intent(this.getApplicationContext(),
                TheBeginning1English.class);
        if (currentapiVersion < android.os.Build.VERSION_CODES.HONEYCOMB) {
            Notification notification = new Notification(
                    R.drawable.ic_launcher,
                    "Its Sunday!.",
                    System.currentTimeMillis());
            intent1.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP
                    | Intent.FLAG_ACTIVITY_CLEAR_TOP);

            PendingIntent pendingNotificationIntent1 = PendingIntent
                    .getActivity(this.getApplicationContext(), 0, intent1,
                            PendingIntent.FLAG_UPDATE_CURRENT);
            notification.flags |= Notification.FLAG_AUTO_CANCEL;
            notification.setLatestEventInfo(this.getApplicationContext(),
                    "Testing", "Its Sunday.",
                    pendingNotificationIntent1);

            mManager.notify(0, notification);
        } else {
            PendingIntent pendingNotificationIntent1 = PendingIntent
                    .getActivity(this.getApplicationContext(), 0, intent1,
                            PendingIntent.FLAG_UPDATE_CURRENT);
            Notification notification = new NotificationCompat.Builder(this)
                    .setContentTitle("Test Test App")
                    .setContentText("Its Sunday!.")
                    .setWhen(System.currentTimeMillis())
                    .setContentIntent(pendingNotificationIntent1)
                    .setSmallIcon(R.drawable.ic_launcher).build();
            notification.flags |= Notification.FLAG_AUTO_CANCEL;
            mManager.notify(0, notification);

        }
        return START_NOT_STICKY;
    }

    @Override
    public void onDestroy() {
        // TODO Auto-generated method stub
        super.onDestroy();
    }

}

多数民众赞成在主要活动中,您将看到四个独立的日历变量,我必须创建一个Receiver,Receiver1,Receiver2,Receiver3和AlarmService1,2,3。每个日历,不是最好的方式,但只有我能想到的东西。我添加了:

Date now = new Date(System.currentTimeMillis());
if (calendar3.getTime().before(now)) {
    calendar3.add(Calendar.MONTH, 1);
} else if (calendar.getTime().before(now)) {
    calendar.add(Calendar.HOUR, 168);
} else if (calendar1.getTime().before(now)) {
    calendar1.add(Calendar.HOUR, 168);
} else if (calendar2.getTime().before(now)) {
    calendar2.add(Calendar.HOUR, 168);
}

我今天添加了日期部分,没有检查出来是否可以使用。谢谢你帮我这个!

1 个答案:

答案 0 :(得分:0)

Alaram服务如何运作?

它根据您的情况开始,就像您设定的日,周或任何工作日一样。所以在每个指定的时间设置它接收命令。喜欢通知,语气。

所以在onStartServiceCommand()方法中。你必须为此应用逻辑。

因此,通知只会在特定日期发出一次。