Java 如何在android studio中每天的不同时间设置每日重复通知?
我目前每天都有重复的通知,每天下午6点重复。我想做的不是在下午6点,而是在活动即将开始时显示通知。我有一个事件列表,它包含日期和时间,这是可能的。下面是我当前显示通知的方式 这是我的主要活动Java 如何在android studio中每天的不同时间设置每日重复通知?,java,android,android-studio,push-notification,notifications,Java,Android,Android Studio,Push Notification,Notifications,我目前每天都有重复的通知,每天下午6点重复。我想做的不是在下午6点,而是在活动即将开始时显示通知。我有一个事件列表,它包含日期和时间,这是可能的。下面是我当前显示通知的方式 这是我的主要活动 Calendar calendar = Calendar.getInstance(); calendar.set(Calendar.HOUR_OF_DAY, 18); calendar.set(Calendar.MINUTE, 30); calendar.set(Calendar.SECOND, 0)
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, 18);
calendar.set(Calendar.MINUTE, 30);
calendar.set(Calendar.SECOND, 0);
Intent intent1 = new Intent(MainActivity.this, AlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(MainActivity.this, 0,intent1, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager am = (AlarmManager) MainActivity.this.getSystemService(MainActivity.this.ALARM_SERVICE);
am.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent);
这是我的广播接收课
公共类AlarmReceiver扩展了BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
long when = System.currentTimeMillis();
NotificationManager notificationManager = (NotificationManager) context
.getSystemService(Context.NOTIFICATION_SERVICE);
Intent notificationIntent = new Intent(context, EVentsPerform.class);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0,
notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder mNotifyBuilder = new NotificationCompat.Builder(
context).setSmallIcon(R.drawable.applogo)
.setContentTitle("Alarm Fired")
.setContentText("Events To be PErformed").setSound(alarmSound)
.setAutoCancel(true).setWhen(when)
.setContentIntent(pendingIntent)
.setVibrate(new long[]{1000, 1000, 1000, 1000, 1000});
notificationManager.notify(MID, mNotifyBuilder.build());
MID++;
}
}
我不知道如何设置时间,以便它在发生事件时重复。任何有关的指针和帮助都将不胜感激。谢谢您可以为每个事件设置警报。从数据库中读取所有事件的时间,然后为这些事件设置单个警报。不要使用此设置重复。设置警报变量适用于不同的Android将手机从睡眠中唤醒的版本
private void setAlarm(Long dateInMillis) {
AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Intent alarmIntent = new Intent(this, AlarmReceiver.class);
PendingIntent pi = PendingIntent.getBroadcast(getApplicationContext(), 0, alarmIntent, PendingIntent.FLAG_UPDATE_CURRENT);
if (Build.VERSION.SDK_INT >= 23) {
am.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, dateInMillis, pi);
} else if (Build.VERSION.SDK_INT >= 19) {
am.setExact(AlarmManager.RTC_WAKEUP, dateInMillis, pi);
} else {
am.set(AlarmManager.RTC_WAKEUP, dateInMillis, pi);
}
}
dateInMillis是日历。getInstance().getTimeInMillis()用于设置报警的时间。因此,我希望您的数据库中有一个长期存储的值,该值可以使您的生活更轻松
public ArrayList<String> getAlarms() {
SQLiteDatabase db;
ArrayList<String> result = new ArrayList<String>();
String myPath = DATABASE_PATH + DATABASE_NAME;
db = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);
selectQuery = "SELECT * FROM TABLE";
Cursor cursor = db.rawQuery(selectQuery, null);
if (cursor.moveToFirst()) {
result.add(cursor.getString(cursor.getColumnIndex("WhateverYourColumnIs")));
while (cursor.moveToNext()) {
result.add(cursor.getString(cursor.getColumnIndex("WhateverYourColumnIs")));
}
}
cursor.close();
db.close();
return result;
}
公共类DatabaseHandler扩展了SQLiteOpenHelper{
私有静态最终字符串DATABASE_PATH=“/data/data/YOUR_PACKAGE_HERE/databases/”;
私有静态最终字符串数据库\u NAME=“YOUR\u DATABASE.db”;
私有静态最终int数据库_VERSION=1;
私有SQLiteDatabase myDataBase=null;
私有最终上下文myContext;
Context=null;
公共数据库处理程序(上下文){
super(上下文、数据库名称、null、数据库版本);
this.myContext=上下文;
//TODO自动生成的构造函数存根
myDataBase=this.getWritableDatabase();
myDataBase.close();
}
@凌驾
public void onCreate(SQLiteDatabase db){
//TODO自动生成的方法存根
db.execSQL(“如果不存在,则创建表信息(”+
id整数主键自动递增+
alarmTime整数+
"姓名及文字";;
}
public void addInfo(长时间,字符串名称){
SQLiteDatabase db=this.getWritableDatabase();
ContentValues=新的ContentValues();
值。put(“报警时间”,time);
值。put(“名称”,名称);
db.插入(“信息”,空,值);
db.close();
}
公共阵列列表getInfoAll(){
字符串选择查询;
sqlitedb数据库;
光标;
NotificationInfo ni=新的NotificationInfo();
ArrayList结果=新建ArrayList();
字符串myPath=数据库路径+数据库名称;
db=SQLiteDatabase.openDatabase(myPath,null,SQLiteDatabase.OPEN\u READONLY);
selectQuery=“选择*来自信息”;
cursor=db.rawQuery(selectQuery,null);
if(cursor.moveToFirst()){
ni.setAlarmTime(cursor.getLong(cursor.getColumnIndex(“alarmTime”));
ni.setName(cursor.getLong(cursor.getColumnIndex(“name”));
结果。添加(新通知信息(ni));
while(cursor.moveToNext()){
ni.setAlarmTime(cursor.getLong(cursor.getColumnIndex(“alarmTime”));
ni.setName(cursor.getLong(cursor.getColumnIndex(“name”));
结果。添加(新通知信息(ni));
}
}
cursor.close();
db.close();
返回结果;
}
}
公共类AlarmActivity扩展了活动{
//这里是活动的标准代码
私有无效按钮单击(){
Calendar c=Calendar.getInstance();
DatabaseHandler db=新的DatabaseHandler(AlarmActivity.this);
ArrayList alarms=db.getInfoAll();
db.close();
用于(通知信息ni:警报){
c、 setTimeInMillis(ni.getAlarmTime());
c、 添加(日历分钟,-30);
设置报警(c);
}
}
专用void setAlarm(长日期毫秒){
AlarmManager am=(AlarmManager)getSystemService(Context.ALARM\u服务);
意向alarmIntent=新意向(此为AlarmReceiver.class);
PendingEvent pi=PendingEvent.getBroadcast(getApplicationContext(),0,alarmIntent,PendingEvent.FLAG_UPDATE_CURRENT);
如果(Build.VERSION.SDK_INT>=23){
am.SetExactAndAllowHileIDLE(AlarmManager.RTC_WAKEUP,dateInMillis,pi);
}else if(Build.VERSION.SDK_INT>=19){
am.setExact(AlarmManager.RTC_WAKEUP,dateInMillis,pi);
}否则{
am.set(AlarmManager.RTC_唤醒,dateInMillis,pi);
}
}
}
公共类通知信息{
时间长;
字符串名;
公共无效通知信息(){
}
公共无效通知信息(通知信息ni){
this.name=ni.getName();
this.alarmTime=ni.getAlarmTime();
}
//接球手和接球手
公共无效设置alarmTime(长alarmTime){
this.alarmTime=alarmTime;
}
公共void集合名(字符串名){
这个。尼姆=尼姆;
}
公共长getAlarmTime(){
返回报警时间;
}
公共字符串getName(){
返回名称;
}
}
您好,我看到您删除了所有的评论,很抱歉再次打扰您。我之前问过您为什么要创建DatabaseHandler类,但您没有响应,我可以问一下,这是我已经拥有的sqlite表中的一个添加项还是代表了它吗?我非常感谢您的努力,但我无法将其全部放在一起,如果我能在编写代码的同时给我一些指导,我希望我能让它工作起来。我没有删除注释。可能是版主删除了。我使用数据库帮助器类是因为它实现了SQLiteHelper。SQLLiteHelper类是一个内置模块,用于处理android平台内的数据库操作。这是我可以做的例如db.insert()。查找Ok,那么您是否创建了另一个保存要传递的日期和名称的数据库
DatabaseHelper db = new DatabaseHelper();
ArrayList<String> alarms = db.getAlarms()
for(String alarm : alarms){
try{
setAlarm(Long.parseLong(alarm));
}catch(NumberFormatException nfe){
//Not a number
}
}
private Long calendarMillis(String date, String time){
Long result;
//Do A bunch of stuff to get the information to fill in below from what you bring in.
Calendar c = Calendar.getInstance();
c.set(Calendar.YEAR, year);
//...Calendar.HOUR, Calendar.MONTH, Calendar.DAY
c.set(Calendar.MINUTE, minute);
result = c.getTimeInMillis();
return result;
}
public class NotificationInformation{
Long alarmTime;
String name;
public void NotificationInformation(){
}
public void NotificationInformation(NotificationInformation ni){
this.name = ni.getName();
this.alarmTime = ni.getAlarmTime();
}
//getters and setters
public void setAlarmTime(Long alarmTime){
this.alarmTime = alarmTime;
}
public void setName(String name){
this.nime = nime;
}
public Long getAlarmTime(){
return alarmTime;
}
public String getName(){
return name;
}
}
public class DatabaseHandler extends SQLiteOpenHelper {
private static final String DATABASE_PATH = "/data/data/YOUR_PACKAGE_HERE/databases/";
private static final String DATABASE_NAME = "YOUR_DATABASE.db";
private static final int DATABASE_VERSION = 1;
private SQLiteDatabase myDataBase = null;
private final Context myContext;
Context context = null;
public DatabaseHandler(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
this.myContext = context;
// TODO Auto-generated constructor stub
myDataBase = this.getWritableDatabase();
myDataBase.close();
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
db.execSQL("CREATE TABLE IF NOT EXISTS INFO (" +
"id INTEGER PRIMARY KEY AUTOINCREMENT," +
"alarmTime INTEGER," +
"name TEXT)");
}
public void addInfo(long time, String name) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put("alarmTime", time);
values.put("name", name);
db.insert("INFO", null, values);
db.close();
}
public ArrayList<NotificationInfo> getInfoAll() {
String selectQuery;
SQLiteDatabase db;
Cursor cursor;
NotificationInfo ni = new NotificationInfo();
ArrayList<NotificationInfo> result = new ArrayList<NotificationInfo>();
String myPath = DATABASE_PATH + DATABASE_NAME;
db = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);
selectQuery = "SELECT * FROM INFO";
cursor = db.rawQuery(selectQuery, null);
if (cursor.moveToFirst()) {
ni.setAlarmTime(cursor.getLong(cursor.getColumnIndex("alarmTime"));
ni.setName(cursor.getLong(cursor.getColumnIndex("name"));
result.add(new NotificationInfo(ni));
while(cursor.moveToNext()){
ni.setAlarmTime(cursor.getLong(cursor.getColumnIndex("alarmTime"));
ni.setName(cursor.getLong(cursor.getColumnIndex("name"));
result.add(new NotificationInfo(ni));
}
}
cursor.close();
db.close();
return result;
}
}
public class AlarmActivity extends Activity{
//standard code for activity goes here
private void buttonClicked(){
Calendar c = Calendar.getInstance();
DatabaseHandler db = new Databasehandler(AlarmActivity.this);
ArrayList<NotificationInfo> alarms = db.getInfoAll();
db.close();
for(NotificationInfo ni : alarms){
c.setTimeInMillis(ni.getAlarmTime());
c.add(Calendar.MINUTE, -30);
setAlarm(c);
}
}
private void setAlarm(Long dateInMillis) {
AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Intent alarmIntent = new Intent(this, AlarmReceiver.class);
PendingIntent pi = PendingIntent.getBroadcast(getApplicationContext(), 0, alarmIntent, PendingIntent.FLAG_UPDATE_CURRENT);
if (Build.VERSION.SDK_INT >= 23) {
am.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, dateInMillis, pi);
} else if (Build.VERSION.SDK_INT >= 19) {
am.setExact(AlarmManager.RTC_WAKEUP, dateInMillis, pi);
} else {
am.set(AlarmManager.RTC_WAKEUP, dateInMillis, pi);
}
}
}
public class NotificationInformation{
Long alarmTime;
String name;
public void NotificationInformation(){
}
public void NotificationInformation(NotificationInformation ni){
this.name = ni.getName();
this.alarmTime = ni.getAlarmTime();
}
//getters and setters
public void setAlarmTime(Long alarmTime){
this.alarmTime = alarmTime;
}
public void setName(String name){
this.nime = nime;
}
public Long getAlarmTime(){
return alarmTime;
}
public String getName(){
return name;
}
}