11

我正在构建一个应用程序,它将在用户醒着的时间以特定时间间隔触发通知。

我有一个在服务内部运行的 alarmManager。该服务通过单击主要活动的按钮显式启动,并让 alarmManager 在特定时间反转期间执行通知。我将如何在一天中的某些时间停止通知?例如,我不希望在用户睡觉时触发这些通知。

我当前以用户设置的间隔触发通知的代码如下(已删除导入......这已经足够长了):

public class FartSmackinChunks extends Service {
    public Notification scheduleNotification;
    public AlarmManager alarmScheduleManager;
    public PendingIntent alarmScheduleIntent;
    private Boolean autoUpdateBoolean = true;
    private int intervalsGoneByInt = 0;
    private Notification notification;
    public static final int NOTIFICATION_ID = 1;

    @Override
    public void onCreate() {
        // TODO: Actions to perform when service is created.
        int icon = R.drawable.icon;
        String tickerText = "INTERVAL FIRED";
        long when = System.currentTimeMillis();
        scheduleNotification = new Notification(icon, tickerText, when);

        alarmScheduleManager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);

        String ALARM_ACTION;
        ALARM_ACTION = ScheduleAlarmReceiver.ACTION_REFRESH_SCHEDULE_ALARM;
        Intent intentToFire = new Intent(ALARM_ACTION);
        alarmScheduleIntent = PendingIntent.getBroadcast(this, 0, intentToFire,
        0);
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        SharedPreferences mySharedPreferences =
        PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
        boolean autoUpdateBoolean =
        mySharedPreferences.getBoolean("storedAutoUpdateBoolean", false);
        String updateFreq =
        mySharedPreferences.getString("storedInitialAverageTimeInterval", "00:00:00");

        SimpleDateFormat dfInterval = new SimpleDateFormat("hh:mm:ss");

        Date intervalTimeAsDateObject = null;
        long updateFreqMilliLong;
        try {
            intervalTimeAsDateObject = dfInterval.parse(updateFreq);
        } catch (ParseException e) {
            e.printStackTrace();
        }

        updateFreqMilliLong = intervalTimeAsDateObject.getTime() - 18000000;

        if (autoUpdateBoolean) {
            int alarmType = AlarmManager.ELAPSED_REALTIME_WAKEUP;
            long timetoRefresh = SystemClock.elapsedRealtime() +
            updateFreqMilliLong;
            alarmScheduleManager.setInexactRepeating(alarmType,
            timetoRefresh, updateFreqMilliLong, alarmScheduleIntent);
            notifications();
        } else alarmScheduleManager.cancel(alarmScheduleIntent);

        return Service.START_NOT_STICKY;
    };

    private void notifications() {
        **notification stuff in here***
    }

    @Override
    public IBinder onBind(Intent intent) {
        // TODO: Replace with service binding implementation.
        return null;
    }

    @Override
    public void onDestroy() {
        this.alarmScheduleManager.cancel(alarmScheduleIntent);
    }
}

.....和我的广播接收器实现在这里:

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

public class ScheduleAlarmReceiver extends BroadcastReceiver {

    public static final String ACTION_REFRESH_SCHEDULE_ALARM
    = "com.application.ACTION_REFRESH_SCHEDULE_ALARM";

    @Override
    public void onReceive(Context context, Intent intent) {
        Intent startIntent = new Intent(context, SmokerReducerService.class);
        context.startService(startIntent);
    }
}

我在思考应该如何实现这一点时遇到了一些困难。

我正在考虑重新编写此代码,以便在唤醒时间触发警报管理器并在睡眠时间停止,而在服务内部是一个计时器,它以特定的时间间隔触发通知方法?有没有更好的方法来做到这一点?

任何输入将不胜感激。几天来,我一直在努力解决这个问题。

谢谢

编辑:@任何遇到此打算使用计时器进行每日通知的人:

当设备进入睡眠状态(即......用户将手机置于待机状态)时,运行在服务内部的计时器将被运行时暂停。因此,使用 Timer 在特定时间间隔触发通知将无法在服务中正常工作,因为当 Android 暂停服务时,它也会暂停计时器,从而引发间隔。

正确的方法是使用带有一系列待处理意图的 AlarmManager 在一天中的特定时间设置警报。这确保了即使手机处于待机状态,通知(或您当时想要发生的任何事情)仍将被执行。

4

1 回答 1

3

我正在考虑重新编写此代码,以便在唤醒时间触发警报管理器并在睡眠时间停止,而在服务内部是一个计时器,它以特定的时间间隔触发通知方法?有没有更好的方法来做到这一点?

在我看来,忘记想一个“更好”的方法,这似乎是唯一的方法。使用计时器来控制(启用/禁用)另一个计时器并不奇怪,对我来说完全有意义。

于 2011-01-19T23:06:34.087 回答