【问题标题】:Past notifications showing with current notifications过去的通知与当前通知一起显示
【发布时间】:2013-12-16 20:04:52
【问题描述】:

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

EDIT1 - 代码

主活动

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

我今天添加了日期部分,还没有检查它是否会起作用。感谢您帮我解决这个问题!

【问题讨论】:

    标签: java android notifications alarmmanager


    【解决方案1】:

    Alaram 服务如何工作?

    它会根据您的情况开始,就像您为一天、一周或任何工作日设置的一样。所以在每个指定的时间设置它接收命令。比如通知,音调。

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

    所以通知只针对特定的一天发出一次。

    【讨论】:

    • 谢谢,我使用了代码@stackoverflow.com/questions/20445481/… ...虽然现在我已经对其进行了修改,为每个警报创建了广播服务(我是业余爱好者)...如果你可以检查d代码,我会很高兴
    猜你喜欢
    • 2020-03-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-21
    相关资源
    最近更新 更多