dil33pm
dil33pm

Reputation: 1643

Network programming in Service

I am writing a service that updates the textview in an AppWidget. It is about reading an RSS and displaying the text. So I need to set the text as "No Internet available" if the internet is not active on the device. So on the Service's onStart() method I put

ConnectivityManager cm =
        (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo netInfo = cm.getActiveNetworkInfo();
if(!netInfo)
{
 try-catch block to fetch XML using XMLPULLPARSER and set the textview
}
else
 set the textview using remoteview that there is no internet

But this is not working. The service is killed by background ANR. How do I check the network status?

Upvotes: 0

Views: 165

Answers (1)

Vladimir Mironov
Vladimir Mironov

Reputation: 30874

Although Service is designed for performing operations in the background, it still runs in the ui thread by default. If you want to do long running operations in the service you have to create a worker thread. Or you can use an IntentService that creates it for you.

Upvotes: 1

Related Questions