0

I am trying to make a very little application. My app will fetch an url:

example.com/response.json

when this response "true" my app will send a notification message:

Your thing is ready! Let's see.

But I have no idea how to make it? Do I need a Timer or what?

I found this method:

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

But how to check every 5min the response and send the notification?

1 Answers1

0

download this file using i.e. HttpURLConnection or HttpClient inside AsyncTask doInBackground method. example of fetching data:

HttpGet reqest = new HttpGet(yourURL);
HttpClient client = new DefaultHttpClient();
HttpResponse response = client.execute(reqest);

String result = EntityUtils.toString(response.getEntity());

for parsing JSON use JSONObject(String json) passing result (and/or optionally JSONArray if needed)

if result matches your expectations use Toast.makeText(...) or Notifications (example here)

snachmsm
  • 17,866
  • 3
  • 32
  • 74
  • Thank you, but how can I make it with Timer? Check every x minutes the response and send the notification if response is true? – Paul Beckers Dec 04 '14 at 22:00
  • if you want to poll server sequentialy then you should run your downloading stuff (`AsyncTask`) inside `Service` and then: yes, you might use timer as here http://stackoverflow.com/questions/4597690/android-timer-how (lot to do ahead of you...) or `Handler` and its method `postDelayed(Runnable r, int delayInMillis)` – snachmsm Dec 04 '14 at 22:10