gpt4 book ai didi

android - 如何使用 Scheduler 进行 Http 请求

转载 作者:行者123 更新时间:2023-11-30 03:24:17 24 4
gpt4 key购买 nike

我想在 Android 中创建一个应用程序,一旦它安装在 deive 中。它每 5 分钟发送一个 HTTP rrequest。在收到响应后,它会显示通知。我该怎么做。

Activity 代码

public class AutoNotification extends Activity {
String url="";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_auto_notification);
postHttpRequest("Test","Test");

}

public void postHttpRequest(String userId,String pass){
RequestClient reqClient = new RequestClient(AutoNotification.this);
String AppResponse = null;
try {
url = "";
Log.d("URL", url);
AppResponse = reqClient.execute().get();
String status = "200";
Log.d("Status recived", status);

if(status.equals("200")){
autoNotify();
}
} catch (Exception e) {
Log.e("Exception Occured", "Exception is "+e.getMessage());
}
}
public void autoNotify(){
Intent intent = new Intent();
PendingIntent pIntent = PendingIntent.getActivity(AutoNotification.this, 0, intent, 0);
Builder builder = new NotificationCompat.Builder(getApplicationContext())
.setTicker("Test Title").setContentTitle("Content Test")
.setContentText("Test Notification.")
.setSmallIcon(R.drawable.ic_launcher)
.setContentIntent(pIntent);
Uri notificationSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
builder.setSound(notificationSound);
Notification noti = builder.build();
noti.flags = Notification.FLAG_AUTO_CANCEL;
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
notificationManager.notify(0, noti);

}
}

现在我怎么能每 5 分钟调用一次并显示通知。请帮助我

编辑感谢@chintan 的建议。我已经这样做了:

public class AutoNotification extends Activity {
String url="";
private Timer refresh = null;
private final long refreshDelay = 5 * 1000;
SendHttpRequestThread sendHttpRequestThread;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_auto_notification);

sendHttpRequestThread = new SendHttpRequestThread("Test","Test");
sendHttpRequestThread.start();

}



public void postHttpRequest(String userId,String pass){
RequestClient reqClient = new RequestClient(AutoNotification.this);
String AppResponse = null;
try {
url = "";
Log.d("URL", url);
AppResponse = reqClient.execute(url).get();
String status = "200";
Log.d("Status recived", status);

if(status.equals("200")){
autoNotify();
}
} catch (Exception e) {
Log.e("Exception Occured", "Exception is "+e.getMessage());
}
}
public void autoNotify(){
Intent intent = new Intent();
PendingIntent pIntent = PendingIntent.getActivity(AutoNotification.this, 0, intent, 0);
Builder builder = new NotificationCompat.Builder(getApplicationContext())
.setTicker("Test Title").setContentTitle("Content Test")
.setContentText("Test Notification.")
.setSmallIcon(R.drawable.ic_launcher)
.setContentIntent(pIntent);
Uri notificationSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
builder.setSound(notificationSound);
Notification noti = builder.build();
noti.flags = Notification.FLAG_AUTO_CANCEL;
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
notificationManager.notify(0, noti);

}

class SendHttpRequestThread extends Thread {

boolean sendHttpRequest;
String userId;
String pass;

public SendHttpRequestThread(String str1, String str2) {
this.userId = str1;
this.pass = str2;
sendHttpRequest = true;
}

public void stopSendingHttpRequest() {
sendHttpRequest = false;
}

protected void onStop() {
sendHttpRequestThread.stopSendingHttpRequest();
super.stop();
}

@Override
public void run() {
while (sendHttpRequest) {
postHttpRequest(userId, pass);

SystemClock.sleep(refreshDelay);
}
}
}
}

最佳答案

我创建了一个将休眠 5 秒的自定义线程类。

public class SendHttpRequestThread extends Thread {

boolean sendHttpRequest;
String userId;
String pass;

public SendHttpRequestThread(String str1, String str2) {
this.userId = str1;
this.pass = str2;
sendHttpRequest = true;
}

public void stopSendingHttpRequest() {
sendHttpRequest = false;
}

@Override
public void run() {
while (sendHttpRequest) {
postRequest(userId, pass);
//add code here to execute in background thread
autoNotify();
SystemClock.sleep(delayMillis);
}
}
}

我没有实现所有代码,你需要放在 while 循环中。这里 delayMillis 是包含 5000 的整数值,因为 sleep() 接受 mili 秒的输入。

要执行此线程,请编写以下代码。

sendHttpRequestThread = new SendHttpRequestThread("Test","Test");
sendHttpRequestThread.start();

要停止此线程,请编写以下代码。

sendHttpRequestThread.stopSendingHttpRequest();

如果你想在 Activity 停止时停止这个线程,那么像下面这样写。

@Override
protected void onStop() {
sendHttpRequestThread.stopSendingHttpRequest();
super.onStop();
}

关于android - 如何使用 Scheduler 进行 Http 请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18529802/

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