java - 每天在特定时间使用 AlarmManager 触发通知时出现错误

标签 java android service notifications

在我的应用程序中,用户使用时间选择器设置闹钟以通知他们何时使用该应用程序。当他们设置提醒时,它会每 24 小时通知一次用户。我已经使用 AlarmManager 和 Service 来解决这个问题。当我设置时间时,我每 24 小时收到一次通知,但有时我会一次又一次地随机收到通知。这是我无法调试的错误,我不知道问题出在哪里。

我是这样做的:

设置提醒和取消提醒是我的代码: 我使用 AlarmManager 来设置时间。

private void setReminder(){
    Calendar calendar = Calendar.getInstance();
    Calendar currentCalendar = Calendar.getInstance();
    calendar.set(Calendar.HOUR_OF_DAY, timePicker.getCurrentHour());
    calendar.set(Calendar.MINUTE, timePicker.getCurrentMinute());

    SimpleDateFormat sdf = new SimpleDateFormat(TIME_FORMAT, Locale.US);
    String time = sdf.format(calendar.getTime());
    SavingData.setReminder(time, true);

    long intendedTime = calendar.getTimeInMillis();
    long currentTime = currentCalendar.getTimeInMillis();

    alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
    Intent intent = new Intent(this, MyReceiver.class);
    pendingIntent = PendingIntent.getBroadcast(this, 0, intent, 0);

    if((intendedTime >= currentTime) == false){
        // This will set the alarm for the next day, if time is below intentedTime
        calendar.add(Calendar.DAY_OF_MONTH, 1);
        intendedTime = calendar.getTimeInMillis();
    }

    alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, intendedTime, AlarmManager.INTERVAL_DAY, pendingIntent);

    Toast.makeText(this, "Alarm Scheduled!", Toast.LENGTH_LONG).show();
}

private void cancelId() {
    Calendar calendar = Calendar.getInstance();
    calendar.set(Calendar.HOUR_OF_DAY, timePicker.getCurrentHour());
    calendar.set(Calendar.MINUTE, timePicker.getCurrentMinute());

    // Time format to String
    SimpleDateFormat sdf = new SimpleDateFormat(TIME_FORMAT, Locale.US);
    String time = sdf.format(calendar.getTime());
    SavingData.setReminder(time, false);

    alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE); 
    Intent intent = new Intent(this, MyReceiver.class);
    pendingIntent = PendingIntent.getBroadcast(this, 0, intent, 0);

    // If the alarm has been set, cancel it.
    if (alarmManager!= null) {
        alarmManager.cancel(pendingIntent);
        Toast.makeText(this, "Cancel Alarm!", Toast.LENGTH_SHORT).show();
    }
}

我使用 BroadCastReceiver:

public class MyReceiver extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
    Toast.makeText(context, "MyReceiver!", Toast.LENGTH_SHORT).show();
    Intent service = new Intent(context, MyService.class);
    context.startService(service);
}

下面是我的服务类。在我的服务类中,我使用通知管理器创建通知信息。

 public class MyService extends Service {
private static final int notificationID = 0;

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

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

@Override
@Deprecated
public void onStart(Intent intent, int startId) {
    // TODO Auto-generated method stub
    super.onStart(intent, startId);
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);

    Intent notificationIntent = new Intent(this, MainActivity.class);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);

    int icon = R.drawable.icon;
    String tickerText = "HELLO";
    long when = System.currentTimeMillis();
    String contentTitle = "How Are You";
    String contentText = "Time to use the App";
    // Define sound URI, the sound to be played when there's a notification
    Uri soundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);

    Notification notification = new NotificationCompat.Builder(this)
    .setContentTitle(contentTitle)
    .setContentText(contentText)
    .setSmallIcon(icon)
    .setTicker(tickerText)
    .setWhen(when)
    .setContentIntent(pendingIntent)
    .setSound(soundUri)
    .build();

    notificationManager.notify(notificationID, notification);
    return super.onStartCommand(intent, flags, startId);
}

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

最佳答案

您的服务正在让 Android 系统在它死机时或在其他时间重新启动“空” Intent ,而您没有考虑这些。您在收到 Intent 时随时发送通知。来自文档:

intent The Intent supplied to startService(Intent), as given. This may be null if the service is being restarted after its process has gone away, and it had previously returned anything except START_STICKY_COMPATIBILITY.

如果您检查空 Intent 或其他特定于您的警报 Intent 的内容,您的问题应该会消失。

关于java - 每天在特定时间使用 AlarmManager 触发通知时出现错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24542565/

相关文章:

java - 如何在java中迭代数组[...][i](列)

java - 尝试 Autowiring 使用 MockitoJUnitRunner 运行的配置属性时出现 NullPointerException

android - fragment 内存泄漏

c# - 循环到 ListView 元素

android - 从编辑文本字符串中删除空格

java - 在 Java 中使用 VisualBasic DLL

java - java中同一类中的重复方法,在使用Eclipse时进行编译

android - 无法访问 Context.USAGE_STATS_SERVICE

android - Android-播放可在所有 Activity 中播放的背景音乐

linux - 关闭NFS服务: FAILED