主体:
am = (AlarmManager) getSystemService(Service.ALARM_SERVICE);Intent intent = new Intent(this,MyAlarmBroadCast.class);PendingIntent pi = PendingIntent.getActivity(this, 0, intent, 0);am.set(AlarmManager.RTC_WAKEUP, 10*1000, pi);
接收器:
public class MyAlarmBroadCast extends BroadcastReceiver{ @Override public void onReceive(Context context, Intent intent) { Toast.makeText(context, "闹钟提示:时间到!", Toast.LENGTH_LONG).show(); } }
mainifest.xml<receiver android:name=".MyAlarmBroadCast"> <intent-filter> <action android:name="com.example.energyup.MyAlarmBroadCast" /> </intent-filter></receiver>
解决方案
am.set(AlarmManager.RTC_WAKEUP, 10*1000, pi);
的第二个参数的理解有问题,这里传入的时唤醒准确时间,并不是间隔,按你的需求,应该是
am.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis()+10*1000, pi);
看看文档中对RTC时间的解释