Outofdate
Outofdate

Reputation: 695

Runnable in service working in a main thread

I'm new in android programming. Trying to get some useful data set from web json(network task) and parse it. So i choose for this Service, for making background job. How do i properly install repeated task with Handler.postDelayed(runnable, interval) not in the main thread?

Help me please to figure this out?

Upvotes: 0

Views: 270

Answers (4)

laalto
laalto

Reputation: 152847

IntentService intents run on a background worker thread.

However, you seem to be posting a runnable doing network operations to a handler which is likely on the UI thread since you're getting NetworkOnMainThreadException.

This explains the problem. Please clarify what you're trying to accomplish so we can suggest a better approach.

Upvotes: 1

Aniket Thakur
Aniket Thakur

Reputation: 68945

NetworkOnMainThreadException that is thrown when an application attempts to perform a networking operation on its main thread.

Also this is only thrown for applications targeting the Honeycomb SDK or higher. Applications targeting earlier SDK versions are allowed to do networking on their main event loop threads, but it's heavily discouraged.

So use a separate thread for network operations.

For more details visit developer android site.

Upvotes: 0

nKn
nKn

Reputation: 13761

One of the main behaviors of Android programming is that the UI thread (i.e., the one that interacts with the user) has to be as free as it can. This means that if you declared a Network Connection within the UI thread, you will collapse it and the user will get a nice ANR (Android Not Responding).

For tasks that involves network connections, I'd recommend one of these:

  • If your network task is limited in time and it ends within a short time after starting, I'd recommend using an AsyncTask.

  • If you need an undefined-in-time network connection, or a forever-loop socket and so, use a Service, they're intended exactly for this.

Upvotes: 0

Adam Arold
Adam Arold

Reputation: 30538

The exception name NetworkOnMainThreadException says it all. You should not do networking in the main thread. There are a lot of solutions for this problem using AsyncTask is one of them.

Here is an example from AndroidDevelopers:

 private class DownloadFilesTask extends AsyncTask<URL, Integer, Long> {
     protected Long doInBackground(URL... urls) {
         int count = urls.length;
         long totalSize = 0;
         for (int i = 0; i < count; i++) {
             totalSize += Downloader.downloadFile(urls[i]);
             publishProgress((int) ((i / (float) count) * 100));
             // Escape early if cancel() is called
             if (isCancelled()) break;
         }
         return totalSize;
     }

     protected void onProgressUpdate(Integer... progress) {
         setProgressPercent(progress[0]);
     }

     protected void onPostExecute(Long result) {
         showDialog("Downloaded " + result + " bytes");
     }
 }

Another option is to use a Service. Here you can find a good tutorial on services.

Upvotes: 0

Related Questions