What Services Are For

OK, we admit it: Looking at those Logcat statements was boring. But this code is really exciting! Why? What can you do with it?

Time to go back to the Land of Make Believe, where we are no longer programmers but work in retail shoe sales with superheroes who do our bidding.

Your Flash workers can work in two kinds of places in a store: the front of the store, where they talk to customers, and the back of the store, where they do not. The back of the store may be larger or smaller, depending on the store.

So far, all of your code has run in activities. Activities are your Android app’s storefront. All this code is focused on a pleasant visual experience for your user/customer.

Services are the back end of your Android app. Things can happen there that the user never needs to know about. Work can go on there long after the storefront has closed, when your activities are long gone.

Enough about stores. What can you do with a service that you cannot do with an activity? Well, for one, you can run a service while the user is occupied elsewhere.

Safe background networking

Your service is going to poll Flickr in the background. To perform networking in the background safely, some additional code is required. Android provides the ability for a user to turn off networking for backgrounded applications. If the user has a lot of power-hungry applications, this can be a big performance improvement.

This does mean, however, that if you are doing networking in the background, you need to verify with the ConnectivityManager that the network is available.

Add the code in Listing 28.4 to perform this check.

Listing 28.4  Checking for background network availability (PollService.java)

public class PollService extends IntentService {
    private static final String TAG = "PollService";
    ...
    @Override
    protected void onHandleIntent(Intent intent) {
        if (!isNetworkAvailableAndConnected()) {
            return;
        }

        Log.i(TAG, "Received an intent: " + intent);
    }

    private boolean isNetworkAvailableAndConnected() {
        ConnectivityManager cm =
                (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE);

        boolean isNetworkAvailable = cm.getActiveNetworkInfo() != null;
        boolean isNetworkConnected = isNetworkAvailable &&
                cm.getActiveNetworkInfo().isConnected();

        return isNetworkConnected;
    }
}

The logic for checking network availability is in isNetworkAvailableAndConnected(). Toggling the background data setting to disallow downloading data in the background disables the network entirely for use by background services. In this case, ConnectivityManager.getActiveNetworkInfo() returns null, making it appear to the background service as though there is no active network available, even if there really is.

If the network is available to your background service, it gets an instance of android.net.NetworkInfo representing the current network connection. The code then checks whether the current network is fully connected by calling NetworkInfo.isConnected().

If the app does not see a network available, or the device is not fully connected to a network, onHandleIntent(Intent) will return without executing the rest of the method (and in turn will not try to download data, once you have added the code to do so). This is good practice because your app cannot download any data if it is not connected to the network.

One more thing. To use getActiveNetworkInfo(), you also need to acquire the ACCESS_NETWORK_STATE permission. As you have seen, permissions are managed in your manifest.

Listing 28.5  Acquiring network state permission (AndroidManifest.xml)

<manifest
    xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.bignerdranch.android.photogallery" >

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

    <application
        ... >
        ...
    </application>

</manifest>
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.129.39.55