1

我在我的應用程序中設置了一個鬧鐘。我爲此使用了報警管理器和廣播接收器。問題是警報不能按時提高。爲什麼警報不能按時提高?

有時它會按時提高,有時不會。如果設置了多個報警,則所有報警將同時上升。

我嘗試使用setInexactRepeating,setRepeating,這兩種情況下它發生這樣的情況。我希望鬧鐘每週重複一次。

請幫助不知道什麼是錯的。

通知功能

public void setNotificationTime(Calendar c) 
{ 

    Date dateFrom = new Date(); 
    df = new SimpleDateFormat("E MMM dd hh:mm:ss zzzz yyyy"); 
    try { 
     dateFrom = df.parse(startTime); 
    } 
    catch (ParseException ex) { 

    } 

    dateFrom.getTime(); 
    c.setTime(dateFrom); 

    hour = c.get(Calendar.HOUR_OF_DAY); 
    minute = c.get(Calendar.MINUTE); 


    if(notificationTime.equals("10 Minutes Before")) 
    { 


     c.set(Calendar.HOUR_OF_DAY, hour); 
     c.set(Calendar.MINUTE, minute - 10); 
     c.set(Calendar.SECOND, 0); 
     c.set(Calendar.MILLISECOND, 0); 
     c.set(Calendar.DATE, day); 
     // c.set(Calendar.DAY_OF_WEEK,); 

     SetDay(c); 

     notification = c.getTime(); 
     notificationTime = df.format(notification); 

     Toast.makeText(getApplicationContext(),notificationTime,Toast.LENGTH_SHORT).show(); 

     intent = new Intent(getBaseContext(),NotificationReceiver.class); 
     pendingIntent = PendingIntent.getBroadcast(getBaseContext(),RQS_1, intent, 0); 
     alarmManager = (AlarmManager)getSystemService(Context.ALARM_SERVICE); 
     alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(), AlarmManager.INTERVAL_DAY * 7, pendingIntent); 

    } 



    else if(notificationTime.equals("30 Minutes Before")) 
    { 

     c.set(Calendar.HOUR_OF_DAY, hour); 
     c.set(Calendar.MINUTE, minute - 30); 
     c.set(Calendar.SECOND, 0); 
     c.set(Calendar.MILLISECOND, 0); 
     c.set(Calendar.DATE, day); 
     // c.set(Calendar.DAY_OF_WEEK,); 

     SetDay(c); 

     notification = c.getTime(); 
     notificationTime = df.format(notification); 

     Toast.makeText(getApplicationContext(),notificationTime,Toast.LENGTH_SHORT).show(); 

     intent = new Intent(getBaseContext(),NotificationReceiver.class); 
     pendingIntent = PendingIntent.getBroadcast(getBaseContext(),RQS_1, intent, 0); 
     alarmManager = (AlarmManager)getSystemService(Context.ALARM_SERVICE); 
     alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(), AlarmManager.INTERVAL_DAY * 7, pendingIntent); 

    } 



    else if(notificationTime.equals("1 Hour Before")) 
    { 

     c.set(Calendar.HOUR_OF_DAY, hour - 1); 
     c.set(Calendar.MINUTE, minute); 
     c.set(Calendar.SECOND, 0); 
     c.set(Calendar.MILLISECOND, 0); 
     c.set(Calendar.DATE, day); 
     // c.set(Calendar.DAY_OF_WEEK,); 

     SetDay(c); 

     notification = c.getTime(); 
     notificationTime = df.format(notification); 

     Toast.makeText(getApplicationContext(),notificationTime,Toast.LENGTH_SHORT).show(); 

     intent = new Intent(getBaseContext(),NotificationReceiver.class); 
     pendingIntent = PendingIntent.getBroadcast(getBaseContext(),RQS_1, intent, 0); 
     alarmManager = (AlarmManager)getSystemService(Context.ALARM_SERVICE); 
     alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(), AlarmManager.INTERVAL_DAY * 7, pendingIntent); 

    } 



    else if(notificationTime.equals("2 Hours Before")) 
    { 

     c.set(Calendar.HOUR_OF_DAY, hour - 2); 
     c.set(Calendar.MINUTE, minute); 
     c.set(Calendar.SECOND, 0); 
     c.set(Calendar.MILLISECOND, 0); 
     c.set(Calendar.DATE, day); 
     // c.set(Calendar.DAY_OF_WEEK,); 

     SetDay(c); 

     notification = c.getTime(); 
     notificationTime = df.format(notification); 

     Toast.makeText(getApplicationContext(),notificationTime,Toast.LENGTH_SHORT).show(); 

     intent = new Intent(getBaseContext(),NotificationReceiver.class); 
     pendingIntent = PendingIntent.getBroadcast(getBaseContext(),RQS_1, intent, 0); 
     alarmManager = (AlarmManager)getSystemService(Context.ALARM_SERVICE); 
     alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(), AlarmManager.INTERVAL_DAY * 7, pendingIntent); 

    } 
} 

NotificationReceiver

public class NotificationReceiver extends BroadcastReceiver { 


    public static int MY_NOTIFICATION_ID = 0; 
    NotificationManager notificationManager; 
    Notification myNotification; 

    EventTableHelper db; 

    @Override 
    public void onReceive(Context context, Intent intent) { 


     Toast.makeText(context, "Time is set", Toast.LENGTH_LONG).show(); 

     db = new EventTableHelper(context); 

     List<EventData> testSavings = db.getAllEvents(); 

     for (EventData ts : testSavings) { 
      String log = "from date:" + ts.getFromDate() 
        + " ,to date: " + ts.getToDate() 
        + " ,location: " + ts.getLocation() 
        + " ,title " + ts.getTitle(); 

      Calendar c = Calendar.getInstance(); 
      Date date = new Date(); 
      Date date1 = new Date(); 
      Log.d("Result: ", log); 

      SimpleDateFormat df = new SimpleDateFormat("E MMM dd hh:mm:ss zzzz yyyy"); 
      SimpleDateFormat df2 = new SimpleDateFormat("hh:mm a"); 

      try { 
       date = df.parse(ts.getFromDate()); 
       date1 = df.parse(ts.getToDate()); 
      } catch (ParseException ex) { 

      } 
      String timeFrom = df2.format(date); 
     // String startTime = String.valueOf(timeFrom); 

      String timeTo = df2.format(date1); 
      // String endTime = String.valueOf(timeTo); 


      String location = ts.getLocation(); 
      String title = ts.getTitle(); 


      Intent myIntent = new Intent(context, MainActivity.class); 
      PendingIntent pendingIntent = PendingIntent.getActivity(
        context, 
        0, 
        myIntent, 
        PendingIntent.FLAG_UPDATE_CURRENT); 

      if(location.equals("")) 
      { 
       String msg = "From : " + timeFrom + "\nTo : " + timeTo; 

       myNotification = new NotificationCompat.Builder(context) 
         .setContentTitle("Event : " + title) 
         .setContentText(msg) 
         .setWhen(System.currentTimeMillis()) 
         .setContentIntent(pendingIntent) 
         .setAutoCancel(true) 
         .setSmallIcon(R.drawable.eventicon) 
         .setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION)) 
         .setStyle(new NotificationCompat.BigTextStyle().bigText(msg)) 
         .setDefaults(Notification.DEFAULT_SOUND) 
         .build(); 

      } 

      else 
      { 
       String msg = "From : " + timeFrom + "\nTo : " + timeTo + "\nAt : " + location; 
       myNotification = new NotificationCompat.Builder(context) 
         .setContentTitle("Event : " + title) 
         .setContentText(msg) 
         .setWhen(System.currentTimeMillis()) 
         .setContentIntent(pendingIntent) 
         .setAutoCancel(true) 
         .setSmallIcon(R.drawable.eventicon) 
         .setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION)) 
         .setStyle(new NotificationCompat.BigTextStyle().bigText(msg)) 
         .setDefaults(Notification.DEFAULT_SOUND) 
         .build(); 

      } 

      Log.i("Notify", "Notification"); 
      notificationManager = 
        (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); 
      notificationManager.notify(MY_NOTIFICATION_ID, myNotification); 

      myNotification.flags=Notification.FLAG_AUTO_CANCEL; 

      Intent i = new Intent(); 
      i.putExtra("notificationId",MY_NOTIFICATION_ID); 

      MY_NOTIFICATION_ID ++; 

     } 
    } 
} 

任何人都可以請幫助..

回答

1

你的目標一4.4+設備上的API級別19+?如果是這樣,沒有辦法安排一個確切的重複事件。見AlarmManager.setRepeating()

注:爲API 19,所有重複報警是不準確的。如果您的 應用程序需要精確的交貨時間,那麼它必須使用一次性的精確警報,每次重新計劃如上所述。傳統 targetSdkVersion早於API 19的應用程序 將繼續具有其所有警報,包括重複警報, 視爲確切。

我認爲這是設計,勸阻開發者:喚醒設備更快地消耗電池。

+0

是的,我讀過這個。但是爲什麼有時候所有的警報都會同時出現?這是相同的原因呢,還是我錯誤地在我的代碼中的東西? – user5881997

+0

我能做些什麼來避免這種不精確的時間? @bwt – user5881997

+0

不精確的警報允許設備喚醒一次並處理多個警報。在19+上,你可以使用'setExact()'來處理你自己的重複,例如每一次下個星期的時間。 – bwt