Android:设置特定时间的通知

问题描述

我正试图在第二天早上7点收到通知以提醒我,我以为我已经进行了一次此操作,但一定做了一些更改,但没有意识到。谁能告诉我我要怎么做?我已经包括设置提醒和通知发布者的活动。

活动

public class Reminders extends MainActivity {
    public static final String NOTIFICATION_CHANNEL_ID = "10001";
    final static int req1 = 1;
    private final static String default_notification_channel_id = "default";
    private static final int RQS_PICK_CONTACT = 0;
    public String a = "0";
    EditText eText;
    EditText reminder;
    DatePickerDialog picker;


@SuppressLint("WrongViewCast")
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_reminders);
    BottomNavigationView navigation = findViewById(R.id.navigation);
    navigation.setonNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem item) {
            switch (item.getItemId()) {
                case R.id.home:
                    Intent a = new Intent(Reminders.this,MainActivity.class);
                    startActivity(a);
                    break;
                case R.id.recipes:
                    Intent b = new Intent(Reminders.this,RecipeSearch.class);
                    startActivity(b);
                    break;
                case R.id.shoppingList:
                    Intent c = new Intent(Reminders.this,ShoppingList.class);
                    startActivity(c);
                    break;
                case R.id.mealPlan:
                    Intent d = new Intent(Reminders.this,MenuPlan.class);
                    startActivity(d);
                    break;
                case R.id.reminder:
                    Intent e = new Intent(Reminders.this,Reminders.class);
                    startActivity(e);
                    break;
            }
            return false;
        }
    });


    reminder = findViewById(R.id.reminderText);
    eText = findViewById(R.id.reminderDate);
    eText.setInputType(InputType.TYPE_NULL);
    eText.setonClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            final Calendar cldr = Calendar.getInstance();
            final int day = cldr.get(Calendar.DAY_OF_MONTH);
            final int month = cldr.get(Calendar.MONTH);
            int year = cldr.get(Calendar.YEAR);
            // date picker dialog
            picker = new DatePickerDialog(Reminders.this,new DatePickerDialog.OnDateSetListener() {

                        @SuppressLint("SetTextI18n")
                        @Override
                        public void onDateSet(DatePicker view,int year,int monthOfYear,int dayOfMonth) {
                            eText.setText(year + "/" + (monthOfYear + 1) + "/" + dayOfMonth);
                        }
                    },year,month,day);
            picker.show();
        }

    });
}

private void scheduleNotification(Notification notification,long delay) {
    Intent notificationIntent = new Intent(Reminders.this,NotificationPublisher.class);
    notificationIntent.putExtra(NotificationPublisher.NOTIFICATION_ID,1);
    notificationIntent.putExtra(NotificationPublisher.NOTIFICATION,notification);
    PendingIntent pendingIntent = PendingIntent.getbroadcast(this,notificationIntent,0);
    AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
    alarmManager.set(AlarmManager.RTC_WAKEUP,delay,pendingIntent);
}

private Notification getNotification(String content) {
    String description = reminder.getText().toString();
    NotificationCompat.Builder builder = new NotificationCompat.Builder(this,NOTIFICATION_CHANNEL_ID);
    builder.setContentTitle("Reminder Service");
    builder.setContentText(description);
    builder.setSmallIcon(R.mipmap.ic_launcher);
    builder.setStyle(new NotificationCompat.BigTextStyle());
    builder.setVibrate(new long[]{1000,1000})
            .setSound(Settings.System.DEFAULT_NOTIFICATION_URI)
            .setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
            .setLights(Color.RED,500,500);
    builder.setAutoCancel(true);
    builder.setPriority(PRIORITY_HIGH);
    builder.setChannelId(NOTIFICATION_CHANNEL_ID);
    Intent intent = new Intent(getApplicationContext(),MainActivity.class);
    Context context;
    PendingIntent pendingIntent = PendingIntent.getActivity(this,intent,0);
    builder.setContentIntent(pendingIntent);
    return builder.build();
}


public void saveReminder(View view) {
    String myFormat = "yyyy/MM/dd";
    SimpleDateFormat sdf = new SimpleDateFormat(myFormat,Locale.getDefault());
    String date = eText.getText().toString();
    Date date1 = null;
    long milli = 0;
    try {
        date1 = sdf.parse(date);
        milli = date1.getTime();
    } catch (ParseException e) {
        e.printstacktrace();
    }

    Log.d("date",date);
    // Set date at 7AM
    long delay = (milli + 25200000);
    scheduleNotification(getNotification(reminder.getText().toString()),delay);

    Intent intentHome = new Intent(getApplicationContext(),MainActivity.class);
    startActivity(intentHome);
}

}

NotificationPublisher

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

public void onReceive(Context context,Intent intent) {
    notificationmanager notificationmanager = (notificationmanager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    Notification notification = intent.getParcelableExtra(NOTIFICATION);
    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
        int importance = notificationmanager.IMPORTANCE_HIGH;
        NotificationChannel notificationChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID,"NOTIFICATION_CHANNEL_NAME",importance);
        assert notificationmanager != null;
        notificationmanager.createNotificationChannel(notificationChannel);
    }
    int id = intent.getIntExtra(NOTIFICATION_ID,0);
    assert notificationmanager != null;
    notificationmanager.notify(id,notification);
}

}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...