0

my app should check every X min. web content and I have to implement wakeLock as it should work when screen is off, but at the same time I have to use AsyncTask as it use network (which is banned in main thread).

What is good method to be sure that .release() would be called?

Block PostExecute is not allways called (when there is an error in doInBackground()), so does finally in main thread.

Michał Tajchert
  • 10,333
  • 4
  • 30
  • 47

2 Answers2

0

If you use Google Cloud Messaging you can control from the server when devices request a server data fetch vs using a defined X min period. Your mobile app will be most responsive to updated server data and we will be decreasing server loads to only load when needed.

GCM: Getting Started http://developer.android.com/google/gcm/gs.html

When you implement GCM, your server can send a Send-to-Sync or Messages with Payload to the device. Review the Pro's Con's here (http://developer.android.com/google/gcm/adv.html)

In your mobile app you will have a class that extends GCMBaseIntentService and you will receive a call onMessage, inside here you can make server requests and issue notifications.

@Override
protected void onMessage(Context context, Intent intent) {
    try {
        conn = (HttpURLConnection) url.openConnection();
        conn.setDoOutput(true);
        conn.setUseCaches(false);
        conn.setFixedLengthStreamingMode(bytes.length);
        conn.setRequestMethod("POST");
        conn.setRequestProperty("Content-Type",
                "application/x-www-form-urlencoded;charset=UTF-8");
        // post the request
        OutputStream out = conn.getOutputStream();
        out.write(bytes);
        out.close();
        // handle the response
        int status = conn.getResponseCode();            
...
Kyght
  • 617
  • 7
  • 8
0

Finally I found some similar questions:

android wait asynctask to finish

Android: how to wait AsyncTask to finish in MainThread?

Hope that will help someone.

Community
  • 1
  • 1
Michał Tajchert
  • 10,333
  • 4
  • 30
  • 47