我希望在系统时间戳到达特定时间时调用一个函数。 无论如何比CountDownTimer好吗? 应该在服务上调用它,因为我希望它在应用程序关闭时仍然运行。
非常感谢。
答案 0 :(得分:0)
你必须使用BroadcastReceiver和AlarmManager这样。
//Create alarm manager
AlarmManager malarmMngr = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
//Create pending intent & register it to your alarm notifier class
Intent intent = new Intent(this, yourBroadcastReceiver.class);
PendingIntent mPendInt = PendingIntent.getBroadcast(this, 0, intent, 0);
//set your time stamp (for once in future)
malarmMngr .set(AlarmManager.RTC_WAKEUP, yourtimestamp, mPendInt);
现在创建yourBroadcastReceiver类来调用函数。
public class yourBroadcastReceiver extends BroadcastReceiver {
public MyReceiver() {
}
@Override
public void onReceive(Context context, Intent intent) {
// This method is called when this BroadcastReceiver receives an Intent broadcast.
// Call your function here
}
}