出于某种原因,我的闹钟仅在日期到来时才会触发,例如12.10 pm,如果是12.10pm。并且所有其他日期都不会被触发。我正在使用AlarmManager,有点困惑。任何人都可以帮忙
这是我在类
中设置闹钟的方法/**
* Set up alarm from user input
*/
public void setUpAlarm() {
// get the time reminder hour and minute from user's input time value
String[] timeVals = time.split(":");
int hourOfDay = Integer.parseInt(timeVals[0].trim());
int minOfDay = Integer.parseInt(timeVals[1].trim());
// get the Date from User date value
String[] dateVals = date.split("/");
int day = Integer.parseInt(dateVals[0].trim());
int month = Integer.parseInt(dateVals[1].trim());
int year = Integer.parseInt(dateVals[2].trim());
Calendar calendar = Calendar.getInstance();
// set calendar based on user input
if (month == 1) {
calendar.set(Calendar.MONTH, Calendar.JANUARY);
System.out.println("jan");
} else if (month == 2) {
calendar.set(Calendar.MONTH, Calendar.FEBRUARY);
System.out.println("feb");
} else if (month == 3) {
calendar.set(Calendar.MONTH, Calendar.MARCH);
System.out.println("march");
} else if (month == 4) {
calendar.set(Calendar.MONTH, Calendar.APRIL);
System.out.println("april");
} else if (month == 5) {
calendar.set(Calendar.MONTH, Calendar.MAY);
System.out.println("may");
} else if (month == 6) {
calendar.set(Calendar.MONTH, Calendar.JUNE);
System.out.println("june");
} else if (month == 7) {
calendar.set(Calendar.MONTH, Calendar.JULY);
System.out.println("july");
} else if (month == 8) {
calendar.set(Calendar.MONTH, Calendar.AUGUST);
System.out.println("august");
} else if (month == 9) {
calendar.set(Calendar.MONTH, Calendar.SEPTEMBER);
System.out.println("september");
} else if (month == 10) {
calendar.set(Calendar.MONTH, Calendar.OCTOBER);
System.out.println("october");
} else if (month == 11) {
calendar.set(Calendar.MONTH, Calendar.NOVEMBER);
System.out.println("november");
} else if (month == 12) {
calendar.set(Calendar.MONTH, Calendar.NOVEMBER);
System.out.println("december");
}
// set year
calendar.set(Calendar.YEAR, year);
// set day
calendar.set(Calendar.DAY_OF_MONTH, day);
// set hour of day
calendar.set(Calendar.HOUR_OF_DAY, hourOfDay);
// calendar set Minute of Day
calendar.set(Calendar.MINUTE, minOfDay);
// set AM or PM
if (hourOfDay < 12) {
calendar.set(Calendar.AM_PM, Calendar.AM);
} else {
calendar.set(Calendar.AM_PM, Calendar.PM);
}
// create intent for alarm
Intent myIntent = new Intent(confirmTaskForm.this,
MyAlarmReceiver.class);
// create pending intent
pendingIntent = PendingIntent.getBroadcast(confirmTaskForm.this, 0,
myIntent, 0);
// set-up alarm manager
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
// send pendingIntent to alarmManager
alarmManager.set(AlarmManager.RTC, calendar.getTimeInMillis(),
pendingIntent);
}
然后激发广播接收器
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Toast;
/**
* Feeder class sending data to MyAlarmService
* @author Aaron
*
*/
public class MyAlarmReceiver extends BroadcastReceiver{
int taskID ;
@Override
public void onReceive(Context context, Intent intent) {
Intent service1 = new Intent(context, MyAlarmService.class);
context.startService(service1);
}
}
然后是通知
package com.example.prototype4.alarm;
import com.example.prototype4.MainActivity;
import com.example.prototype4.R;
import com.example.prototype4.Points.Achievments;
import com.example.prototype4.database.DatabaseHelper;
import com.example.prototype4.home.TaskListView;
import com.example.prototype4.task.Task;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
import android.database.CursorIndexOutOfBoundsException;
import android.os.Bundle;
import android.os.IBinder;
import android.widget.Toast;
/**
* Main Alarm functionality for Alarm is performed here
*
* @author Aaron
*
*/
public class MyAlarmService extends Service {
NotificationManager mManager;
DatabaseHelper db;
int taskID;
Task currentTask;
String taskTitle;
@Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
}
@Override
public void onCreate() {
//instantiate database
db = new DatabaseHelper(this);
// TODO Auto-generated method stub
super.onCreate();
}
@SuppressWarnings({ "static-access", "deprecation" })
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
// get current task
try{
currentTask = db.getLastTaskFromDatabase();
taskTitle = currentTask.getTaskTitle();
// update task with deadline = true
currentTask.setDeadline("true");
db.updateTask(currentTask);
mManager = (NotificationManager) this.getApplicationContext()
.getSystemService(
this.getApplicationContext().NOTIFICATION_SERVICE);
Intent intent1 = new Intent(this.getApplicationContext(),
MainActivity.class);
Notification notification = new Notification(R.drawable.ic_app_logo,
"Task Deadline Has Passed!", System.currentTimeMillis());
intent1.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP
| Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingNotificationIntent = PendingIntent.getActivity(
this.getApplicationContext(), 0, intent1,
PendingIntent.FLAG_UPDATE_CURRENT);
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notification.setLatestEventInfo(this.getApplicationContext(), "Task: ",
"Make sure you complete task : " + taskTitle,
pendingNotificationIntent);
mManager.notify(0, notification);
}catch(CursorIndexOutOfBoundsException e){
System.out.println("Task has already been completed before deadline fired");
}catch(NullPointerException e){
System.out.println("Task cannot be obtained after it has been completed");
}
return super.onStartCommand(intent, flags, startId);
}
@Override
public void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
}
}