android - 如何设置提醒?
问题描述
我已经尝试了许多来自 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);
}
解决方案
添加以下代码以设置通知。
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)
,因为系统可以调整警报的传递时间以使它们同时触发,避免不必要地从睡眠中唤醒设备。
推荐阅读
- node.js - nodejs socket.io 在本地网络上不起作用
- java - 在 Java 中不使用构建器模式的不可变字段
- asynchronous - 异步 GRPC?
- jquery - JQuery 点击处理程序只允许匿名函数
- python - 在 pyspark UDF 中使用类方法
- mysql - 用户 'user1'@'localhost' goddady 的访问被拒绝
- cron - Mutt 因“无法锁定/发送”而作为 cron 作业失败
- c# - 如何删除我在单击另一个按钮时创建的按钮
- mysql - MySql:插入十进制值(失真)
- postgresql - 尝试在 Postgres 中捕获等效项