使用Android通知时出现奇怪的错误

时间:2013-12-10 08:03:01

标签: android notifications broadcastreceiver

Helo,我有一个Android应用程序,每天在特定时间提醒用户服用他的药物。因此我使用Notifications和BroadcastReceiver。当我现在将通知设置为在特定时间(例如,在6小时内)出现时,我将仅在该特定时间“[应用]已停止”对话。但是当我进入调试模式并将通知设置为出现时在1分钟内,它将通过所有方法没有任何错误,我得到我的通知。我的BroadcastReceiver类如下

public class AlarmReceiver extends BroadcastReceiver {

String notificationTitle;
String notificationText;

@Override
public void onReceive(Context context, Intent intent) {
        //Monday = 1, Tuesday = 2, etc.
        int actualDay = new DateTime().getDayOfWeek();
        if (getTypeOfNotification() == NotificationType.TYPE_DOSE) {
            notificationTitle = context.getString(R.string.notificationPills);
            notificationText = context.getString(R.string.notificationDose1)+ " " + DataStorage.getInstance().getSelectedWeeklyDose()[actualDay - 1] + " " + context.getString(R.string.notificationDose2);
        }
        else {
            notificationTitle = context.getString(R.string.notificationMeasurement);
            notificationText = context.getString(R.string.notificationMeasurementText);
        }
        showNotification(context);
}

public void setRecurringAlarm(Context context) {
    Calendar updateTime = Calendar.getInstance();
        updateTime.set(Calendar.HOUR_OF_DAY, DataStorage.getInstance().getSelectedNotificationTime().getHourOfDay());
        updateTime.set(Calendar.MINUTE, DataStorage.getInstance().getSelectedNotificationTime().getMinuteOfHour());
        updateTime.set(Calendar.SECOND, 0);
        //check if that time is already reached and if yes, start a new alarm tomorrow
        Calendar rightNow = Calendar.getInstance();
        if (rightNow.after(updateTime)) {
            updateTime.add(Calendar.DAY_OF_MONTH, 1);
        }
        setAlarm(AlarmManager.INTERVAL_DAY, updateTime, context);
}

private void setAlarm(long interval, Calendar updateTime, Context context) {
    Intent notificator = new Intent(context, AlarmReceiver.class);
    PendingIntent recurringNotification = PendingIntent.getBroadcast(context,
            0, notificator, 0);
    AlarmManager alarms = (AlarmManager) context.getSystemService(
            Context.ALARM_SERVICE);
    alarms.cancel(recurringNotification);
    alarms.setInexactRepeating(AlarmManager.RTC_WAKEUP,
            updateTime.getTimeInMillis(), interval, recurringNotification);
}

public void cancelAlarm(Context context) {
    Intent notificator = new Intent(context, AlarmReceiver.class);
    AlarmManager alarms = (AlarmManager) context.getSystemService(
            Context.ALARM_SERVICE);
    PendingIntent cancelNotification = PendingIntent.getBroadcast(context,
            0, notificator, PendingIntent.FLAG_CANCEL_CURRENT);
    alarms.cancel(cancelNotification);  
}

private void showNotification(Context context){
    NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    Intent myIntent = new Intent(Intent.ACTION_VIEW);
    PendingIntent pendingIntent = PendingIntent.getActivity(context, 0,
            myIntent, Intent.FLAG_ACTIVITY_NEW_TASK);
    Notification noti = new NotificationCompat.Builder(context)
            .setContentTitle(notificationTitle)
            .setContentText(notificationText)
            .setSmallIcon(R.drawable.pills)
            .setContentIntent(pendingIntent).build();
    noti.defaults |= Notification.DEFAULT_SOUND;
    noti.flags |= Notification.FLAG_AUTO_CANCEL;
    notificationManager.notify(0, noti);
}

private NotificationType getTypeOfNotification() {
    DateTime latestMeasurement = DataStorage.getInstance().getLatestMeasurementDate();
    //First check special case if there is no latest measurement. Then set actual date.
    if (latestMeasurement != null) {
        Calendar today = Calendar.getInstance();
        if (today.get(Calendar.DAY_OF_YEAR) - latestMeasurement.getDayOfYear() == DataStorage.getInstance().getSelectedMeasureInterval()) {
            return NotificationType.TYPE_MEASUREMENT;
        }
        else return NotificationType.TYPE_DOSE;
    }
    else {
        return NotificationType.TYPE_DOSE;
    }
}

}

警报按以下方式设置(在另一个活动中):

AlarmReceiver receiver = new AlarmReceiver();
receiver.cancelAlarm(NotificationActivity.this);
receiver.setRecurringAlarm(NotificationActivity.this);

任何人都可以帮助我吗?

1 个答案:

答案 0 :(得分:0)

我在你的帮助下找到了它。不知道你可以在控制台上发出logcat错误。该错误是在收到通知时发生的错误数据库查找。

由于