2017-07-29 91 views
0

我有這個應用程序,我想在用戶定義的特定日期設置通知。我在這裏看到很多代碼和相關答案,但由於某種原因,目前爲止沒有任何工作。鬧鐘成功設置爲將來的時間,但不是日曆日期

在這個測試項目中,我有2種方法。一個設置鬧鐘響5秒鐘,另一個分配所需的日期。

問題:

如果我報警設置爲它工作正常一秒鐘的延遲。警報分配後5秒內顯示通知。 但是如果我使用通過Calendar日期的方法,它什麼都不做。沒有觸發任何通知

MainActivity.java

public class MainActivity extends AppCompatActivity { 


    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
    } 

    public void setAlarmOnClick(View view){ 
     // this method call works fine 
     setAlarm(getNotification("Date test"), 3000); 
     // this one doesn't 
     // setAlarm(getNotification("Date test"), getDate()); 
    } 

    // Here nothing happens when the date comes 
    private void setAlarm(Notification notification, Calendar cal) { 

     Intent notificationIntent = new Intent(this, NotificationPublisher.class); 
     notificationIntent.putExtra(NotificationPublisher.NOTIFICATION_ID, 1); 
     notificationIntent.putExtra(NotificationPublisher.NOTIFICATION, notification); 
     PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT | Intent.FILL_IN_DATA); 

     AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE); 

     alarmManager.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(),pendingIntent); 
     Toast.makeText(this, "Alarm worked.", Toast.LENGTH_LONG).show(); 
    } 

    // This alarm get successfully trigger here showing the notification 
    private void setAlarm(Notification notification, int delay) { 

     Intent notificationIntent = new Intent(this, NotificationPublisher.class); 
     notificationIntent.putExtra(NotificationPublisher.NOTIFICATION_ID, 1); 
     notificationIntent.putExtra(NotificationPublisher.NOTIFICATION, notification); 
     PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT | Intent.FILL_IN_DATA); 

     AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE); 

     alarmManager.set(AlarmManager.RTC_WAKEUP, delay, pendingIntent); 
     Toast.makeText(this, "Alarm worked.", Toast.LENGTH_LONG).show(); 
    } 

    private Calendar getDate(){ 
     Calendar cal=Calendar.getInstance(); 
     cal.set(Calendar.MONTH,8); 
     cal.set(Calendar.YEAR,2017); 
     cal.set(Calendar.DAY_OF_MONTH,29); 
     cal.set(Calendar.HOUR_OF_DAY,11); 
     cal.set(Calendar.MINUTE,47); 
     return cal; 
    } 

    @TargetApi(Build.VERSION_CODES.JELLY_BEAN) 
    private Notification getNotification(String content) { 
     Notification.Builder builder = new Notification.Builder(this); 
     builder.setContentTitle("Scheduled Notification"); 
     builder.setContentText(content); 
     builder.setSmallIcon(R.mipmap.ic_launcher); 
     return builder.build(); 
    } 
} 

NotificationPublisher.java

public class NotificationPublisher extends BroadcastReceiver { 
    public static String NOTIFICATION_ID = "notification-id"; 
    public static String NOTIFICATION = "notification"; 

    public void onReceive(Context context, Intent intent) { 

     Toast.makeText(context, "Alarm worked.", Toast.LENGTH_LONG).show(); 
     int id = intent.getIntExtra(NOTIFICATION_ID, 0); 
     NotificationManager notofManager = (NotificationManager)context. getSystemService(Context.NOTIFICATION_SERVICE); 
     Notification notification = intent.getParcelableExtra(NOTIFICATION); 
     notofManager.notify(id, notification); 

    } 
} 

我真的很感激anykind的幫助!

P.S.

我怎樣才能確定報警是成功設置的日期。即使代碼正常工作,並且現在已經設置了一個星期的日期。我如何測試它會觸發呢?顯然,我們不能等待一個星期才能看到。更改設備日期正在工作?謝謝

回答

-2

日曆月從0到11,而不是1到12,因爲你假設。 因此類似於:

cal.set(Calendar.Month, 7); 

會將您的日曆月設置爲8月。

相關問題