2017-07-29 12 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

カレンダーの月は、uが想定しているように1から12の代わりに0から11になります。 だから、のようなもの:

cal.set(Calendar.Month, 7); 

は、カレンダーの月は8月に設定します。

関連する問題