試したこと
calender.setで現在日時以前を指定すると、datePickerが開いてokボタン押したときに通知が届くので、このコードが大きく間違ってることはないと考えています。
指定の日時にsetしてその時が来ても、何もエラーは出ません。
誰か解決につながるヒントをお願いします!
発生している問題・エラーメッセージ
今のところ何もない
DatePicker.class
Java
1public class DatePick extends DialogFragment implements DatePickerDialog.OnDateSetListener { 2 3 public static AlarmManager am; 4 private PendingIntent pending; 5 private int requestCode = 1; 6 7 @NonNull 8 @Override 9 public Dialog onCreateDialog(Bundle savedInstanceState){ 10 final Calendar c = Calendar.getInstance(); 11 int year = c.get(Calendar.YEAR); 12 int month = c.get(Calendar.MONTH); 13 int day = c.get(Calendar.DAY_OF_MONTH); 14 15 DatePickerDialog datePickerDialog = new DatePickerDialog(getActivity(),this, year, month, day); 16 return datePickerDialog; 17 } 18 19 @Override 20 public void onDateSet(DatePicker datePicker, int i, int i1, int i2) { 21 //日付が選択されたときの処理 22 MyDialogFragment.dateEditText.setText(String.format("%d / %02d / %02d", i, i1 + 1, i2)); 23 Calendar calendar = Calendar.getInstance(); 24 calendar.setTimeInMillis(System.currentTimeMillis()); 25 // 通知の設定 26 calendar.set(Calendar.YEAR,2021); 27 calendar.set(Calendar.MONTH,7); 28 calendar.set(Calendar.DAY_OF_MONTH,11); 29 calendar.set(Calendar.HOUR_OF_DAY,16); 30 calendar.set(Calendar.MINUTE,18); 31 calendar.set(Calendar.SECOND,0); 32 33 Intent intent = new Intent(getApplicationContext(),AlarmNotification.class); 34 intent.putExtra("RequestCode",requestCode); 35 36 pending = PendingIntent.getBroadcast(getApplicationContext(),requestCode,intent,0); 37 38 39 if(am != null){ 40 am.setExact(AlarmManager.RTC_WAKEUP,calendar.getTimeInMillis(),pending); 41 Toast.makeText(getApplicationContext(), "alarm start", Toast.LENGTH_SHORT).show(); 42 } 43 } 44} 45
AlarmNotification.class
Java
1public class AlarmNotification extends BroadcastReceiver { 2 3 @RequiresApi(api = Build.VERSION_CODES.O) 4 @Override 5 public void onReceive(Context context, Intent intent){ 6 int requestCode = intent.getIntExtra("RequestCode",0); 7 PendingIntent pendingIntent = PendingIntent.getActivity(context,requestCode,intent,PendingIntent.FLAG_UPDATE_CURRENT); 8 9 String channelID = "default"; 10 String title = context.getString(R.string.app_name); 11 12 long currentTime = System.currentTimeMillis(); 13 SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss", Locale.JAPAN); 14 String cTime = dateFormat.format(currentTime); 15 16 String message = "時間になりました" + cTime; 17 18 NotificationManager notificationManager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE); 19 Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); 20 21 //Notificationのチャンネル設定 22 NotificationChannel channel = new NotificationChannel(channelID,title,NotificationManager.IMPORTANCE_DEFAULT); 23 channel.setDescription(message); 24 channel.enableVibration(true); 25 channel.canShowBadge(); 26 channel.enableLights(true); 27 channel.setLightColor(Color.BLUE); 28 // the channel appears on the lockscreen 29 channel.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE); 30 channel.setSound(defaultSoundUri,null); 31 channel.setShowBadge(true); 32 33 if(notificationManager != null){ 34 Toast.makeText(context,"通知しました",Toast.LENGTH_SHORT).show(); 35 notificationManager.createNotificationChannel(channel); 36 Notification notification = new Notification.Builder(context,channelID).setContentTitle(title).setSmallIcon(android.R.drawable.ic_lock_idle_alarm).setContentText(message).setAutoCancel(true).setContentIntent(pendingIntent).setWhen(System.currentTimeMillis()).build(); 37 notificationManager.notify(R.string.app_name,notification); 38 } 39 } 40}
回答1件
あなたの回答
tips
プレビュー