2013-12-10 72 views
0

Helo,我有一個Android應用程序,提醒用戶每天在特定的時間服用他的藥物。因此我使用Notifications和BroadcastReceiver。當我現在將通知設置爲在特定時間(例如6小時)出現時,我只會在該特定時間收到「[App]已停止」對話。但是當我進入調試模式並將通知設置爲顯示在像1分鐘,它會通過所有方法沒有任何錯誤,我得到我的通知。我的廣播接收器類是如下使用Android通知時出現奇怪的錯誤

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

誰能幫助我?

+0

發表您的logcat的錯誤。 –

+0

'adb logcat'你的手機插入你的電腦。 – Pedantic

回答

0

我在您的幫助下找到了它。不知道你可以在控制檯上發佈logcat錯誤。該錯誤是收到通知時發生的錯誤數據庫查找。

感謝

相關問題