Android中的提醒不再重复

时间:2017-10-02 07:55:13

标签: android alarmmanager android-notifications notificationmanager

我正在尝试在我的Android应用中设置提醒,该应用会在相同的设定时间在某些日子发生。 第一次报警时一切正常,但不会重复,也不会在其他日子发生。

以下是界面:

enter image description here

点击“保存更改”,它将被称为每个选定日期的scheduleAlarm方法:

private void scheduleAlarm(int dayOfWeek) {
    Intent myIntent = new Intent(this , NotifyService.class);
    PendingIntent pendingIntent = PendingIntent.getService(this, 0, myIntent, 0);

    AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);

    if (dayOfWeek == 0)
        alarmManager.cancel(pendingIntent);
    else {
        Calendar calendar = Calendar.getInstance();
        String time_str[] = reminder_time.getText().toString().split(":");
        calendar.set(Calendar.DAY_OF_WEEK, dayOfWeek);
        calendar.set(Calendar.HOUR_OF_DAY, Integer.parseInt(time_str[0]));
        calendar.set(Calendar.MINUTE, Integer.parseInt(time_str[1]));
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);

        Long time = calendar.getTimeInMillis();
        Long weekly = AlarmManager.INTERVAL_HOUR / 12; //AlarmManager.INTERVAL_DAY * 7;

        alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP, time, weekly, pendingIntent);
    }
}

正如您在此版本中所看到的,我试图每5分钟重复一次警报(Long weekly = AlarmManager.INTERVAL_HOUR / 12;

闹钟响起时调用的通知服务如下:

public class NotifyService extends Service {
public NotifyService() {
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    //TODO do something useful
    return Service.START_STICKY;
}

@Override
public IBinder onBind(Intent intent) {
    // TODO: Return the communication channel to the service.
    throw new UnsupportedOperationException("Not yet implemented");
}

@Override
public void onCreate() {
    Intent intent = new Intent(this , Splash.class);
    intent.putExtra(getString(R.string.NOTIFICATION), true);
    PendingIntent contentIntent = PendingIntent.getActivity(this, 0, intent, 0);

    Bitmap bm = BitmapFactory.decodeResource(getResources(), R.drawable.notification_icon_ensafe);
    long[] pattern = {500,500,500,500,500,500,500,500,500};

    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this)
            .setContentTitle(getString(R.string.reminder))
            .setContentText(getString(R.string.reminder_body))
            .setLargeIcon(bm)
            .setSmallIcon(R.drawable.notification_icon_ensafe)
            .setContentIntent(contentIntent)
            .setAutoCancel(true)
            .setVibrate(pattern)
            .setStyle(new NotificationCompat.InboxStyle())
            .setSound(Settings.System.DEFAULT_NOTIFICATION_URI);

    NotificationManager notificationManager  = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
    notificationManager.notify(1, mBuilder.build());
}
}

知道为什么它不起作用吗?

修改

正如@Frank建议的那样,我实现了一个BroadcastReceiver,但它从未被调用过。

在清单中:

<receiver android:name=".synchro.BootReceiver"
        android:enabled="false">
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED"/>
        </intent-filter>
    </receiver>

班级:

public class BootReceiver extends BroadcastReceiver {

List<Integer> selectedDays;
SharedPreferences preferences;

@Override
public void onReceive(Context context, Intent intent) {
    if (intent.getAction().equals("android.intent.action.BOOT_COMPLETED")) {

        **\\ STUFF HAPPENS HERE \\ **

    }
}
}

广播公司在上面列出的scheduleAlarm方法中启动,如下所示:

ComponentName receiver = new ComponentName(getApplicationContext(), BootReceiver.class);
    PackageManager pm = getApplicationContext().getPackageManager();
pm.setComponentEnabledSetting(receiver,
                PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
                PackageManager.DONT_KILL_APP);

还有什么想法?

2 个答案:

答案 0 :(得分:2)

如果您的设备熄灭,操作系统将取消所有预定的警报。可能是你的问题吗?

要解决此问题,您必须使用广播监听器监听设备启动,并在设备开始广播时再次安排警报。

*编辑*

您需要的所有信息都在此页面上:https://developer.android.com/training/scheduling/alarms.html

特别是在“设备启动时启动警报”部分:“默认情况下,当设备关闭时,所有警报都会被取消。为防止这种情况发生......”

设置和测试需要一些时间。不是很快。

答案 1 :(得分:0)

确定。我解决了这个问题。

在NotifyService类中,我使用onCreate()方法重新安排警报。此方法仅在第一次调用。之后只调用onStartCommand。 管理这两种方法我实现了一个有效的AlarmAcheduler。