我想制作一个通知应用程序,该应用程序将根据我已完成正在通知的应用程序的数据库中的数据发出警报,我希望它每一小时一次,但通知不超过另一个触发前一个小时。这是代码Runnable myRunnable = new Runnable(){ @Override public void run() { while(true){ try { runOnUiThread(new Runnable(){ @Override public void run() { AsyncDataClass asyncRequestObject = new AsyncDataClass(); asyncRequestObject.execute(serverUrl, email, day, hourToPass); } }); counter++; Thread.sleep(3600000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } };Here is the trigger codePendingIntent pendingIntent = PendingIntent.getBroadcast(LoginActivity.this, 0, alarmIntent, 0); AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE); alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), 3600000, pendingIntent);正在执行 AsyncTask 的 PostExcuteRunnable myRunnable = new Runnable(){ int counter = 0; @Override public void run() { while(true){ try { runOnUiThread(new Runnable(){ @Override public void run() { AsyncDataClass asyncRequestObject = new AsyncDataClass(); asyncRequestObject.execute(serverUrl, email, day, hourToPass); } }); counter++; Thread.sleep(3600000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }};
2 回答
狐的传说
TA贡献1804条经验 获得超3个赞
您必须创建一个 as :Handler
Handler handler = new Handler();
在你打电话的结尾:Runnable()handler.postDelayed(runnable, 1000*60*60);
工作示例可以是:
Handler handler = new Handler();
Runnable runnable = new Runnable() {
@Override
public void run() {
AsyncDataClass asyncRequestObject = new AsyncDataClass();
asyncRequestObject.execute(serverUrl, email, day, hourToPass);
//To repeat it every hour
handler.postDelayed(runnable, 1000*60*60);
}
};
handler.postDelayed(runnable, 1000*60*60); //call this when you want to start doing the work
添加回答
举报
0/150
提交
取消