我创建了一个日历对象,代码如下:
Intent intent=new Intent(ListDemoActivity.this, Demo1.class);
PendingIntent pi =PendingIntent.getBroadcast(ListDemoActivity.this, 0, intent,0);
AlarmManager am = (AlarmManager)getSystemService(ALARM_SERVICE);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR_OF_DAY,8);
calendar.set(Calendar.MINUTE,0);
calendar.set(Calendar.SECOND,2);
Log.d("Test", "Current time: " + System.currentTimeMillis() );
Log.d("Test", "Calendar time: " + calendar.getTimeInMillis() );
//calendar.add(Calendar.SECOND,8);
am.setRepeating(AlarmManager.RTC_WAKEUP,calendar.getTimeInMillis(),10*1000,pi);
我首先尝试每10秒触发一次警报,但警报只触发一次。 我还在添加后打印当前时间和新时间,但当前时间大于新时间。
12-12 12:57:29.506: D/Test(783): Current time: 1323674849507
12-12 12:57:29.506: D/Test(783): Calendar time: 1323657002506
为什么代码不起作用?
答案 0 :(得分:2)
如果您使用广播接收器捕获pendingintent,请确保您已在清单中添加此行
<receiver android:name="com.example.YourReceiver" android:enabled="true" android:process=":remote"></receiver>
答案 1 :(得分:1)
答案 2 :(得分:0)
您需要在PendingIntent对象创建时设置标志。
PendingIntent pi =PendingIntent.getBroadcast(ListDemoActivity.this, 0, intent,PendingIntent.FLAG_UPDATE_CURRENT);