我正在开发一个调度应用程序,其中用户从列表视图中选择时间,如下午9:30或列表中的任何时间点击按钮停止计划,我想要在9:30时停止计划9:30(我将在数据库中更新方法并更新用户结束时间)或用户从列表中选择的内容,用户也可以同时停止多个时间表,我尝试使用调度重复警报但没有找到任何解。 请帮帮我,我该怎么做?。
这是我在特定时间发出警报的代码
@TargetApi(Build.VERSION_CODES.N)
public void scheduleAlarm(View view, String timeEnd,String id) {
StringTokenizer tokens = new StringTokenizer(timeEnd, ":");
String first = tokens.nextToken();
String second = tokens.nextToken();
StringTokenizer token = new StringTokenizer(second, " ");
String two = token.nextToken(); // just changing given time into like 9:30 to 9 and 30
AlarmManager am = (AlarmManager) getApplicationContext().getSystemService(Context.ALARM_SERVICE);
android.icu.util.Calendar calendar = android.icu.util.Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(android.icu.util.Calendar.HOUR_OF_DAY, Integer.parseInt(first));
calendar.set(android.icu.util.Calendar.MINUTE, Integer.parseInt(two));
Intent intent = new Intent(getApplicationContext(), AlarmReciever.class);
intent.putExtra("ID",id);
intent.putExtra("EndTime",timeEnd);
PendingIntent sender = PendingIntent.getBroadcast(getApplicationContext(), 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
am.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), sender);
}
这是广播接收器的工作原理,但是当我在给定时间内没有给出报警时间表的时候,广播类触发。
public class AlarmReciever extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
SQLITEDBTABLE sqLiteDatabase = new SQLITEDBTABLE(context);
String id = intent.getStringExtra("ID");
String time_end = intent.getStringExtra("EndTime");
sqLiteDatabase.update(time_end, id);
/* sqLiteDatabase.deleteOlderData(laterId);
sqLiteDatabase.deleteInactive(laterId);
sqLiteDatabase.updateEmployee(laterId, "Inactive");*/
//Toast.makeText(context, "Alarm Triggered and SMS Sent", Toast.LENGTH_LONG).show();
}
}
答案 0 :(得分:0)
删除此行
calendar.setTimeInMillis(System.currentTimeMillis());
从代码..... Calendar.getInstance()
已经是最新的。所以你有。
@TargetApi(Build.VERSION_CODES.N)
public void scheduleAlarm(View view, String timeEnd,String id) {
StringTokenizer tokens = new StringTokenizer(timeEnd, ":");
String first = tokens.nextToken();
String second = tokens.nextToken();
StringTokenizer token = new StringTokenizer(second, " ");
String two = token.nextToken(); // just changing given time into like 9:30 to 9 and 30
AlarmManager am = (AlarmManager) getApplicationContext().getSystemService(Context.ALARM_SERVICE);
android.icu.util.Calendar calendar = android.icu.util.Calendar.getInstance();
calendar.set(android.icu.util.Calendar.HOUR_OF_DAY, Integer.parseInt(first));
calendar.set(android.icu.util.Calendar.MINUTE, Integer.parseInt(two));
Intent intent = new Intent(getApplicationContext(), AlarmReciever.class);
intent.putExtra("ID",id);
intent.putExtra("EndTime",timeEnd);
PendingIntent sender = PendingIntent.getBroadcast(getApplicationContext(), 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
am.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), sender);