我正在努力做一个非常小的应用程序。我的应用会获取一个网址:
example.com/response.json
当这个回复" true"我的应用会发送一条通知消息:
Your thing is ready! Let's see.
但我不知道如何制作它?我需要定时器还是什么?
我找到了这个方法:
private static void generateNotification(Context context, String message) {
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification(R.drawable.ic_launcher, "Message received", System.currentTimeMillis());
// Hide the notification after its selected
notification.flags |= Notification.FLAG_AUTO_CANCEL;
//adding LED lights to notification
notification.defaults |= Notification.DEFAULT_LIGHTS;
Intent intent = new Intent(context, MessageReceivedActivity.class);
intent.putExtra("payload", payload);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, Intent.FLAG_ACTIVITY_NEW_TASK);
notification.setLatestEventInfo(context, "Message", "New message received", pendingIntent);
notificationManager.notify(0, notification);
}
但如何每5分钟检查一次响应并发送通知?
答案 0 :(得分:0)
使用HttpURLConnection
或HttpClient
AsyncTask
doInBackground
方法下载此文件。获取数据的示例:
HttpGet reqest = new HttpGet(yourURL);
HttpClient client = new DefaultHttpClient();
HttpResponse response = client.execute(reqest);
String result = EntityUtils.toString(response.getEntity());
用于解析JSON使用JSONObject(String json)
传递result
(和/或根据需要可选JSONArray
)
如果结果符合您的预期,请使用Toast.makeText(...)
或通知(example here)