This is an old revision of the document!


05 - Android Connectivity & Google APIs

  • Description:
  • Practical part:

Lecture

Practical

Resources

Task 1 - Downloading remote content

Create a new project with a blank main Activity. Add an EditText, a Button and a TextView to the Activity's layout. When the user enters a URL into the EditText and clicks the Button download the remote HTTP content at that URL and display it into the TextView.

By design, the Android framework will not allow long-running or potentially blocking operations (such as network operations) to be carried out on the main UI thread. As such, within the onClick() method of the Button we want to start a new thread in which to download the HTTP data.

The simplest solution would be using a Thread instance. However, we want to modify a UI element (the TextView) right after the thread has finished downloading data, which cannot be done from a Thread's run() method. For such a scenario, the framework provides the AsyncTask class.

When defining the AsyncTask instance we can provide 3 parameters. Since we want to give our instance a String (the URL) to work with and have it return another String (the downloaded content) the first and third parameters will be String while the second one can be left Void:

button.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v){
        AsyncTask<String, Void, String> task = new AsyncTask<String, Void, String>(){
        ...
        };
    }
});

The most important method within the AsyncTask is doInBackground(), which is executed on another thread. Since we have defined the AsyncTask as having a String as the first and third parameters, the signature of the doInBackground() method will be:

@Override
protected String doInBackground(String... params){
...
}

In order to retrieve HTTP data, we can use the HttpURLConnection API, using params[0] as the remote URL:

URL url = new URL(params[0]);
HttpURLConnection connection = (HttpURLConnection)url.openConnection();

The actual contents can be retrieved by using the InputStream provided by the HttpURLConnection instance:

InputStream is = connection.getInputStream();

The String contents returned from the doInBackground() method are passed as the parameter to the onPostExecute() method, which will be executed on the main UI thread. As such, within this method we can safely update the contents of the TextView:

@Override
protected void onPostExecute(String content) {
  textView.setText(content);
}

Having defined the AsyncTask launch it using the execute method, which receives the text from the EditText element as a parameter:

task.execute(editText.getText().toString());

If you try to run the application at this time, it will stop since, by default, applications are not granted Internet access by the framework. In order to be able to connect to remote resources, you need to add the necessary permission to the AndroidManifest.xml:

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

Task 2 - Getting the current location

For this task, you will need to have enabled either the GPS of your mobile device or an active WiFi connection (or both).

On Android, there the location can be gathered using three providers:

  • GPS provider which uses the GPS driver of your mobile device to track your connection (it is slow and not very precise)
  • network provider which relies on having an active internet connection (it has high levels of accuracy)
  • passive provider which relies on having the current location determined by other applications

Add a button and a TextView to the activity. When clicking on the button, the TextView will have to show the current coordinates (latitude and longitude).

In order to get the current location, the main activity will have to implement the LocationListener interface which provides methods for detecting when the location has changed or when a location provider has been enabled or disabled.

Getting the location requires sending a request to the location providers to determine the current location. The request is sent using a LocationManager object. Make sure to send a request for every active network provider. These requests will be sent upon clicking the button.

The LocationListener interface offers a callback through which the user can perform operations if the location coordinates have changed. Implement this callback to set the text of the TextView object to the current coordinates.

Task 3 - Getting the last known location periodically

Task 4 - Scaning for Bluetooth devices periodically

Task 5 - Scaning for WiFi devices periodically

Task 6 - Wakelocks

osp/lectures/lecture-connectivity.1478037423.txt.gz ยท Last modified: 2016/11/01 23:57 by vlad.traista
CC Attribution-Share Alike 3.0 Unported
www.chimeric.de Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0