我想在 Android 中创建一个应用程序,一旦它安装在 deive 中。它每 5 分钟发送一个 HTTP 请求。得到响应后它显示通知。我怎么能这样做。
活动代码
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);
}
}
}
}