Playing with Android SDK

Posted by – March 20, 2008

Yesterday I wrote a small app using Android SDK, this app is called on every incoming call showing a image downloaded from internet, the first challenge was discover how can I catch a system event like a phone call, I could find the answer in this post, all the other questions could be answered by searching on Andoid Developers Google Groups.

You can try it by yourself, but you must download Android SDK first and unzip it on your filesystem, once the SDK is unzipped you can install the Andoid eclipse plugin to create an app called “GrabImage”, this project consists of 4 files:

AndoidManifest.xml:

This file contains some information required to register this app on Andoid runtime, all application components like Activities, Services and Intents must be declared into this file, application permissions must be declared in this file too.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
   package="br.eti.faces.grabimage">

    <!-- Allow this app to access phone contacts -->
    <uses-permission id="android.permission.READ_CONTACTS" />
    <uses-permission android:name="android.permission.INTERNET" />

    <application android:icon="@drawable/icon">
        <activity android:name=".GrabImage"
            android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category
                   android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <!-- The GrabImageIntentReceiver below will be called
       on phone state changes -->
        <receiver android:name=".GrabImageIntentReceiver"
                android:enabled="true">
            <intent-filter>
                <action
                    android:name="android.intent.action.PHONE_STATE" />
            </intent-filter>
        </receiver>
    </application>
</manifest>

In the file above we have the receiver tag, this tag allow to define the IntentReceiver implementation to be called when a system event occurs (phone call, time change etc), the implementation of GrabImageIntentReceiver will be discussed soon in this article.

main.xml

The activity layout can be defined in a xml file, you can add controls to the screen by inserting its own tags like we can see in the file below.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
<?xml version="1.0" encoding="utf-8"?>

<!--
A child control in this layout will be positioned based on other
child control or parent.
-->
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="fill_parent"
   android:layout_height="fill_parent">
   
        <!--
       Inserting a View control that will show
       the downloaded image.
       -->
    <br.com.druid.advertising.views.ImageView
             android:id="@+id/imageView"
             android:layout_width="fill_parent"
              android:layout_height="fill_parent"/>    
   
        <!--
       This activity will show in a TextView control
       some text.
       -->
    <TextView  
              android:layout_width="fill_parent"
              android:layout_height="wrap_content"
              android:text="Look this image!"/>
</RelativeLayout>

GrabImage.java:

Each application may consist of one or more Activity instances, an Activity is a single screen in your application, you can switch activities using intents.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
package br.com.druid.grabimage;

import java.io.BufferedInputStream;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;

import br.com.druid.grabimage.views.ImageView;
import android.app.Activity;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;

public class GrabImage extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
       
        setContentView(R.layout.main);

        ImageView banner = (ImageView) findViewById(R.id.banner);
       
        try {
            URL imageUrl = new URL("http://images.apple.com/home/2008/images/sdk_img_20080306.png");
       
            BufferedInputStream in = new BufferedInputStream(imageUrl.openStream());
           
            banner.setBackgroundDrawable(new BitmapDrawable(in));
           
            banner.invalidate();
        }
        catch(MalformedURLException error) {
           
        }
        catch(IOException error) {
            android.util.Log.e("IO", "Could not load image: " + this, error);
        }
    }
}

GrabImageIntentReceiver.java:

The class below establishes a connection with Android event system, this class will handle only phone state change events. You can trigger phone events on emulator by running commands in a telnet session as described here.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
package br.com.druid.grabimage;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

public class GrabImageIntentReceiver extends BroadcastReceiver {
    private static final String LOG_TAG = "GrabImageIntentReceiver";

    public void onReceive(Context context, Intent intent) {

      Log.i( LOG_TAG, "Starting app....");
     
      Intent grabImageIntent = new Intent();
      grabImageIntent.setClassName(context, "br.com.druid.grabimage.GrabImage");
      grabImageIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

      context.startActivity(grabImageIntent);    
    }
}

ImageView.java:

We must define this View class because it will be used to display the downloaded image on its background.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
package br.com.druid.grabimage.views;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;

public class ImageView extends View {

    public ImageView(Context context) {
        super(context);
        // TODO Auto-generated constructor stub
    }

    public ImageView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        // TODO Auto-generated constructor stub
    }

    public ImageView(Context context, AttributeSet attrs) {
        super(context, attrs);
        // TODO Auto-generated constructor stub
    }
}

You can download the project sources here. This project requires Eclipse 3.x and Android Plugin, installation instructions can be found here.

Share

1 Comment on Playing with Android SDK

  1. JoDogger says:

    Thanks for posting updated code!

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>