我已经看过很多教程并且现在已经尝试了2个小时,尽管还有一些问题。我现在非常紧张:)我想设置一个警报,例如到16:25去,但没有任何反应。我有这段代码:
Calendar cur_cal = new GregorianCalendar();
cur_cal.setTimeInMillis(System.currentTimeMillis());
Calendar cal = new GregorianCalendar();
cal.add(Calendar.DAY_OF_YEAR, cur_cal.get(Calendar.DAY_OF_YEAR));
cal.set(Calendar.HOUR_OF_DAY, 16);
cal.set(Calendar.MINUTE, 25);
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MILLISECOND, 0);
alarmManager.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), pendingIntent);
我也试过这个:
cal.set(Calendar.AM_PM, cur_cal.get(Calendar.AM_PM));
cal.set(Calendar.HOUR, 4);
我的最终目标是让它成为重复警报,例如它应该在规定的时间每天发生。
更新01.17.2011。还是行不通。我有这段代码:
Calendar cal = Calendar.getInstance();
cal.set(Calendar.YEAR, 2011);
cal.set(Calendar.MONTH, Calendar.JANUARY);
cal.set(Calendar.DAY_OF_MONTH, 17);
cal.set(Calendar.HOUR_OF_DAY, 16);
cal.set(Calendar.MINUTE, 58);
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MILLISECOND, 0);
我也试过这个:
cal.set(Calendar.HOUR, 4);
cal.set(Calendar.AM_PM, Calendar.PM);
和此:
cal.set(Calendar.HOUR_OF_DAY, 4 );
cal.set(Calendar.AM_PM, Calendar.PM);
答案 0 :(得分:12)
package your.pack.name;
import java.util.Calendar;
import android.app.Activity;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
public class AlarmActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Calendar cal = Calendar.getInstance();
cal.setTimeInMillis(System.currentTimeMillis());
cal.clear();
cal.set(2012,2,8,18,16);
AlarmManager alarmMgr = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(this, AlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, intent, 0);
// cal.add(Calendar.SECOND, 5);
alarmMgr.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), pendingIntent);
}
}
答案 1 :(得分:11)
通常你不应该像你一样获得Calendar
,有Calendar.getInstance()
方法:
Calendar cal = Calendar.getInstance();
这会为您提供一个日历,其中所有字段都设置为当前日期,然后只是:
cal.set(Calendar.HOUR_OF_DAY, 16);
cal.set(Calendar.MINUTE, 25);
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MILLISECOND, 0);
alarmManager.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), pendingIntent);
答案 2 :(得分:5)
以下代码适用于闹钟。 我在这里提到的日期和时间是:2012年6月28日,上午11:20:00。 最重要的是,月份仅从0 t0 11指定。指6月应由5指定。
AlarmManager objAlarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Calendar objCalendar = Calendar.getInstance();
objCalendar.set(Calendar.YEAR, 2012);
//objCalendar.set(Calendar.YEAR, objCalendar.get(Calendar.YEAR));
objCalendar.set(Calendar.MONTH, 5);
objCalendar.set(Calendar.DAY_OF_MONTH, 28);
objCalendar.set(Calendar.HOUR_OF_DAY, 11);
objCalendar.set(Calendar.MINUTE, 20);
objCalendar.set(Calendar.SECOND, 0);
objCalendar.set(Calendar.MILLISECOND, 0);
objCalendar.set(Calendar.AM_PM, Calendar.AM);
Intent alamShowIntent = new Intent(this,AlarmActivity.class);
PendingIntent alarmPendingIntent = PendingIntent.getActivity(this, 0,alamShowIntent,0 );
objAlarmManager.set(AlarmManager.RTC_WAKEUP,objCalendar.getTimeInMillis(), alarmPendingIntent);
答案 3 :(得分:3)
您没有在Calendar
对象中设置年份。
另请注意,new GregorianCalendar()
已将值设置为当前日期和时间。