gpt4 book ai didi

java - 我试图每小时进行一次通知,但想从数据库中填充,我有工作代码,但下一次通知最多不到一小时

转载 作者:太空狗 更新时间:2023-10-29 13:45:14 25 4
gpt4 key购买 nike

我想制作一个通知应用程序,它会根据数据库中的数据发出警报我已经完成了通知应用程序,我希望它每隔一小时一次,但通知不会在另一个触发之前一小时。这是代码

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 code

PendingIntent 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的onPostExcute下

Runnable 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();
}
}
}
};

private class AsyncDataClass extends AsyncTask<String, Void, String> {

@Override
protected String doInBackground(String... params) {
HttpParams httpParameters = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(httpParameters, 5000);
HttpConnectionParams.setSoTimeout(httpParameters, 5000);
HttpClient httpClient = new DefaultHttpClient(httpParameters);
HttpPost httpPost = new HttpPost(params[0]);
String jsonResult = "";
try {
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("email", params[1]));
nameValuePairs.add(new BasicNameValuePair("day", params[2]));
nameValuePairs.add(new BasicNameValuePair("time", params[3]));
httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
Log.v("Passing Data = ", nameValuePairs.toString());
HttpResponse response = httpClient.execute(httpPost);
jsonResult = inputStreamToString(response.getEntity().getContent()).toString();

} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return jsonResult;
}

@Override

protected void onPreExecute() {
super.onPreExecute();
}

@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
System.out.println("Resulted Value: " + result);
if(result.equals("") || result == null){

Toast.makeText(LoginActivity.this, "Server connection failed", Toast.LENGTH_LONG).show();
return;
}

int jsonResult = returnParsedJsonObject(result);

JSONObject resGet = null;
try {
resGet = new JSONObject(result);
course_code = resGet.getString("course_code");
course_title = resGet.getString("course_title");
} catch (JSONException e) {
e.printStackTrace();
}

if(jsonResult == 0){
Toast.makeText(LoginActivity.this, "No Notification for this hour", Toast.LENGTH_LONG).show();
return;
}
if(jsonResult == 1){
//Toast.makeText(LoginActivity.this, "Success", Toast.LENGTH_LONG).show();
alarmIntent = new Intent(LoginActivity.this, AlarmReceiver.class);
alarmIntent.putExtra("email", email);
alarmIntent.putExtra("time", hourToPass);
alarmIntent.putExtra("day", day);
alarmIntent.putExtra("loguser", loggedUser);
alarmIntent.putExtra("stat", status);
alarmIntent.putExtra("lv", level);
alarmIntent.putExtra("course_code", course_code);
alarmIntent.putExtra("course_title", course_title);
sendBroadcast(alarmIntent);

PendingIntent pendingIntent = PendingIntent.getBroadcast(LoginActivity.this, 0, alarmIntent, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), 3600000, pendingIntent);
}
}

最佳答案

您必须创建一个 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

关于java - 我试图每小时进行一次通知,但想从数据库中填充,我有工作代码,但下一次通知最多不到一小时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55355869/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com