尝试
public static final long DAY_IN_MILLISEC = 1000 * 60 * 60 * 24;
public void initAlarm(Context context) {
AlarmManager am = (AlarmManager) context
.getSystemService(Context.ALARM_SERVICE);
Intent i = new Intent(context, ReceiverClass.class);
i.setAction(ReceiverClass.ALARM_INVOKE_ACTION);
// /////Alarm Time
Calendar alarmCalendar = Calendar.getInstance();
alarmCalendar.set(Calendar.HOUR_OF_DAY, 20);
alarmCalendar.set(Calendar.MINUTE, 40);
alarmCalendar.set(Calendar.SECOND, 0);
long time = alarmCalendar.getTimeInMillis();
Calendar cal = Calendar.getInstance();
if (time <= cal.getTimeInMillis())
time = cal.getTimeInMillis() + DAY_IN_MILLISEC;// /Will set for the
// next day
PendingIntent pi = PendingIntent.getBroadcast(context, 0, i, 0);
am.setRepeating(AlarmManager.RTC_WAKEUP, time, DAY_IN_MILLISEC, pi); // MilliSec
// *
// Sec
// *
// Mnt
// *
// Hour
// /***OR
// am.setRepeating(AlarmManager.RTC_WAKEUP, time,
// AlarmManager.INTERVAL_DAY * 1, pi);
}
ReceiverClass.java
public class ReceiverClass extends BroadcastReceiver {
public static final String ALARM_INVOKE_ACTION = "com.test.alarm";
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
Log.e("action", "Action");
if (action.equalsIgnoreCase(ALARM_INVOKE_ACTION)) {
Log.e("Alarm Invoke", "YES");
} else if (action.equalsIgnoreCase(Intent.ACTION_TIME_CHANGED)
|| action.equalsIgnoreCase(Intent.ACTION_TIME_TICK)
|| action.equalsIgnoreCase(Intent.ACTION_TIMEZONE_CHANGED)
|| action.equalsIgnoreCase(Intent.ACTION_DATE_CHANGED)
|| action.equalsIgnoreCase(Intent.ACTION_REBOOT)) {
Log.e("RESET ALARM HERE.SET AGAIN", "YES");
}
}
}
AndroidManifest.xml 添加...
<receiver android:name="com.hiddenbrains.ui.screens.ReceiverClass" >
<intent-filter>
<action android:name="com.test.alarm" />
<action android:name="android.intent.action.DATE_CHANGED" />
<action android:name="android.intent.action.TIME_SET" />
<action android:name="android.intent.action.TIMEZONE_CHANGED" />
<action android:name="android.intent.action.TIME_TICK" />
<action android:name="android.intent.action.REBOOT" />
</intent-filter>
</receiver>