首页 > 解决方案 > 如何设置提醒?

问题描述

我已经尝试了许多来自 stackoverflow 的解决方案,但都没有奏效...我想设置重复警报以收到未决账单的通知...

到目前为止,我已经尝试过了,

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_pending_sales_purchase);
    manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

    Calendar cal=Calendar.getInstance();
    cal.set(Calendar.MONTH,5);
    cal.set(Calendar.YEAR,2018);
    cal.set(Calendar.DAY_OF_MONTH,8);
    cal.set(Calendar.HOUR_OF_DAY,15);
    cal.set(Calendar.MINUTE,35);
    Intent intent = new Intent(this, MyBroadcastReceiver.class);
    PendingIntent pendingIntent = PendingIntent.getBroadcast(this.getApplicationContext(), 1253, intent, 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();

}

MyBroadcastReceiver 类

@Override
public void onReceive(Context context, Intent intent) {
    Toast.makeText(context, "Alarm worked1111.", Toast.LENGTH_LONG).show();


    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        CharSequence name = "dddd";
        String description = "aaaa";
        int importance = NotificationManager.IMPORTANCE_DEFAULT;
        NotificationChannel channel = new NotificationChannel("a", name, importance);
        channel.setDescription(description);

        NotificationManager notificationManager =context.getSystemService(NotificationManager.class);
        notificationManager.createNotificationChannel(channel);
    }

     intent = new Intent(context, PendingSalesPurchaseActivity.class);
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
    PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);

    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context, "a")
            .setSmallIcon(R.drawable.ic_search_commit)
            .setContentTitle("My notification")
            .setContentText("Hello World!")
            .setPriority(NotificationCompat.PRIORITY_DEFAULT)
                         .setContentIntent(pendingIntent)
            .setAutoCancel(true);
}

标签: android

解决方案


添加以下代码以设置通知。

    NotificationManager notificationManager =
            (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    notificationManager.notify(0, notificationBuilder.build());

对于每天重复警报,您需要使用setRepeating()

 alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent );

当然,如果您不需要在准确的时间重复警报,那么最好使用setInexactRepeating()

 alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent );

安排触发时间要求不准确的重复警报;例如,每小时重复一次的警报,但不一定在每小时的开头。这些警报比 传统上提供的严格重复警报更节能setRepeating(int, long, long, PendingIntent),因为系统可以调整警报的传递时间以使它们同时触发,避免不必要地从睡眠中唤醒设备。


推荐阅读