Saturday, May 12, 2012

Push Notification for Android (C2DM)


There are many tutorials out there for getting started push notification for android, None of them is suitable for novice programmer like me. It has take two weeks of my time to create simple working push notification demo and that's why I decided to put together this tutorial of my own. A lot of my code and structures are  borrowed from other posts. Just follow my post step by step procedures, It will work like charm :)

After android 2.2 is possible to push notification to an Android app. This service is called "Cloud to Device messaging" or short C2DM. 

Step1: Sign-up for a C2DM account with Google
Follow the steps here. While filling the form, note below parameters somewhere, which we will be used it later in the application.

Package name of your Android app * 
Role (sender) account email *


 Step 2: Create Local server to receive registration ID from mobile


I have used Apache and PHP to receive registration ID from mobile (I will brief you about how to get registration ID from mobile below)

For ease of use, you can install xampp bundle from here. It will install everything which you need for the server.  After installing xampp , copy following index.php, notify.php files into int C:\xampp\htdocs\php_push folder.

 index.php

    header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
    header("Cache-Control: no-store, no-cache, must-revalidate");
    header("Cache-Control: post-check=0, pre-check=0", false);
    header("Pragma: no-cache");
    $fname="logfiles/push.log";
    if(!file_exists("logfiles"))
    {
        mkdir("logfiles", 0755);
    }
    if (!file_exists("logfiles/push.log")) {
            $myfile = fopen ($fname, 'a');
           fclose($myfile);
    }
     $myfile = fopen ($fname, 'a');
    if($_SERVER["REQUEST_METHOD"] == "POST"){
        fputs($myfile,"Device Id is :".$_POST["deviceid"]."\n");
        fputs($myfile,"Registration ID is  :".$_POST["registrationid"]."\n");
        echo "response from php file";
    }
    fclose($myfile);
?>


 notify.php
     header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
    header("Cache-Control: no-store, no-cache, must-revalidate");
    header("Cache-Control: post-check=0, pre-check=0", false);
    header("Pragma: no-cache");
    $fname="logfiles/push.log";
    if(!file_exists("logfiles"))
    {
        mkdir("logfiles", 0755);
    }
    if (!file_exists("logfiles/push.log")) {
            $myfile = fopen ($fname, 'a');
           fclose($myfile);
    }
     $myfile = fopen ($fname, 'a');
    if($_SERVER["REQUEST_METHOD"] == "POST"){
        fputs($myfile,"notification message is :".$_POST["message"]."\n");
        echo "response from php file";
    }
    fclose($myfile);
?>


We need to wait 12 - 24 Hours to google to activate C2DM service for you.

Step 3: Get the Authentication key from the C2DM server 
You can use any programming language e.g. Java, PHP, Python, etc. or CURL tool to get the authentication key. The tricky part is , while signing up for C2DM service, google ask only to provide google gmail id ,But google knows your gmail password. To get the authentication key, we need to provide gmail id and password together.

I am using CURL tool to get the authentication token. you can also use the same. If you are using windows download CURL tool here .Extract the zip file and open the command line (Start --> Run --> cmd and the enter) navigate to the exacted folder and execute the following command.

curl https://www.google.com/accounts/ClientLogin -d Email=registered_mail_id@gmail.com -d "Passwd=mypassword" -d accountType=GOOGLE -d source=Google-cURL-Example -d service=ac2dm

From the response get the part after "Auth=" , store it in notepad or somewhere else. we need this token at the time of sending push notification to device.
for Example mine is :
DQAAAMMAAABEBszojeqMG9e8uFpzvzSv_leYyAtWNxdxh804jUnzgljrrjNeFuhB2OjZc0pc625SANcA8q1-X5Jp9wd156BbcDab1w-cXczAKITXUQTshdke0pzU4KjCDXrTzuNqPR3ogcaPQ7ICCJ-pgfzUmmx_IayjKz8QSiAyCc3bxiksyB4j617T_K0jiwzpy_5cQPg11GptA36Q3w6GouUMz10cr7mwxn1c7tV-d9zCHdeCf9ivED99GO8461i8wHCSqSltf50dmVutXevCeS94Zqb- 

* We need this ID at the time of sening push message to device. This token is periodically refreshed. 


Step 4: Requirements 


C2DM is available as of Android 2.2 and requires that Android Market application is installed on the device.
To use C2DM on the Android simulator you also need to use a Google device with API 8 or higher and to register with a Google account on the emulator via the Settings. Emulator Cant receive message from C2DM server.

Tested on Eclipse Version: 3.7.2

 Step 5:Application development

Create new android project and provide the package which you gave at the time of signing up for the C2DM service and keep the activity name C2DMClientActivity ( or you many do the necessary changes in the following source code)

* Copy and past the code in the following order

You may need to rename  ic_laucher.png to  icon.png in the  project_name/rec/drawable folder.

Copy following main.xml and activity_result.xml  files into project_name/res/layout folder

main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="register"
        android:text="Register" >
   </Button>
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="showRegistrationId"
        android:text="Show" >
    </Button>
</LinearLayout>

activity_result.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    <TextView
        android:id="@+id/result"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="center"
        android:text="No info."
        android:textAppearance="?android:attr/textAppearanceLarge" >
    </TextView>
</LinearLayout>

Copy following C2DMClientActivity.java, C2DMMessageReceiver.java, C2DMRegistrationReceiver.java, MessageReceivedActivity.java, RegistrationResultActivity.java files into Project_name\src\your.package.com\  folder

C2DMClientActivity.java

package your.package.com;
import android.app.Activity;
import android.app.PendingIntent;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

public class C2DMClientActivity extends Activity {

    public final static String AUTH = "authentication";

    // Example Activity to trigger a request for a registration ID to the Google
    // server
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }

    public void register(View view) {
        Log.w("C2DM", "start registration process");
        Intent intent = new Intent("com.google.android.c2dm.intent.REGISTER");
        intent.putExtra("app",
                PendingIntent.getBroadcast(this, 0, new Intent(), 0));
        // Use registered Google email
        intent.putExtra("sender", "Role (sender) account email");
        startService(intent);
    }

    public void showRegistrationId(View view) {
        SharedPreferences prefs = PreferenceManager
                .getDefaultSharedPreferences(this);
        String string = prefs.getString(AUTH, "n/a");
        Toast.makeText(this, string, Toast.LENGTH_LONG).show();
        Log.d("C2DM RegId", string);

    }
}

C2DMMessageReceiver.java
Please replace  local_machine_ip with your local machine IP where you installed XAMPP


package your.package.com;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.provider.SyncStateContract.Columns;
import android.util.Log;

public class C2DMMessageReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        Log.w("C2DM", "Message Receiver called");
        if ("com.google.android.c2dm.intent.RECEIVE".equals(action)) {
            Log.w("C2DM", "Received message");
            final String payload = intent.getExtras().getString("message");
            Log.d("C2DM", "dmControl: payload = " + payload);
            // Note: Send this to my application server to get the real data
            // Lets make something visible to show that we received the message
            sendRegistrationMessage(payload);
            createNotification(context, payload);

        }
    }
    // do this in an service and in an own thread
    public void sendRegistrationMessage(String message) {
        Log.d("C2DM", "Sending registration ID to my application server");
        HttpClient client = new DefaultHttpClient();
        HttpPost post = new HttpPost("http://local_machine_ip/php_push/notify.php");
        try {
            List nameValuePairs = new ArrayList(1);
            // Get the deviceID
            nameValuePairs.add(new BasicNameValuePair("message", message));
            post.setEntity(new UrlEncodedFormEntity(nameValuePairs));
            HttpResponse response = client.execute(post);
            BufferedReader rd = new BufferedReader(new InputStreamReader( response.getEntity().getContent()));

            String line = "";
            while ((line = rd.readLine()) != null) {
                Log.e("HttpResponse", line);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }  
  
    public void createNotification(Context context, String payload) {
        NotificationManager notificationManager = (NotificationManager) context
                .getSystemService(Context.NOTIFICATION_SERVICE);
        Notification notification = new Notification(R.drawable.icon,
                "Message received", System.currentTimeMillis());
        // Hide the notification after its selected
        notification.flags |= Notification.FLAG_AUTO_CANCEL;
        //adding sound to notification
        notification.defaults |= Notification.DEFAULT_SOUND;
      
        Intent intent = new Intent(context, MessageReceivedActivity.class);
        intent.putExtra("payload", payload);
        PendingIntent pendingIntent = PendingIntent.getActivity(context, 0,
                intent, 0);
        notification.setLatestEventInfo(context, "Message",
                "new notification", pendingIntent);
        notificationManager.notify(0, notification);

    }

}

C2DMRegistrationReceiver.java                                                                                                            
Please replace  local_machine_ip with your local machine IP where you installed XAMPP
 

package your.package.com;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.preference.PreferenceManager;
import android.provider.Settings.Secure;
import android.provider.SyncStateContract.Columns;
import android.util.Log;

public class C2DMRegistrationReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        Log.e("C2DM", "Registration Receiver called");
        if ("com.google.android.c2dm.intent.REGISTRATION".equals(action)) {
            Log.w("C2DM", "Received registration ID");
            final String registrationId = intent
                    .getStringExtra("registration_id");
            String error = intent.getStringExtra("error");

            Log.e("C2DM", "dmControl: registrationId = " + registrationId
                    + ", error = " + error);
            String deviceId = Secure.getString(context.getContentResolver(),
                    Secure.ANDROID_ID);
            createNotification(context, registrationId);
            sendRegistrationIdToServer(deviceId, registrationId);
            // Also save it in the preference to be able to show it later
            saveRegistrationId(context, registrationId);
        }
    }

    private void saveRegistrationId(Context context, String registrationId) {
        SharedPreferences prefs = PreferenceManager
                .getDefaultSharedPreferences(context);
        Editor edit = prefs.edit();
        edit.putString(C2DMClientActivity.AUTH, registrationId);
        edit.commit();
    }

    public void createNotification(Context context, String registrationId) {
        NotificationManager notificationManager = (NotificationManager) context
                .getSystemService(Context.NOTIFICATION_SERVICE);
        Notification notification = new Notification(R.drawable.icon,
                "Registration successful", System.currentTimeMillis());
        // Hide the notification after its selected
        notification.flags |= Notification.FLAG_AUTO_CANCEL;

        Intent intent = new Intent(context, RegistrationResultActivity.class);
        intent.putExtra("registration_id", registrationId);
        PendingIntent pendingIntent = PendingIntent.getActivity(context, 0,
                intent, 0);
        notification.setLatestEventInfo(context, "Registration",
                "Successfully registered", pendingIntent);
        notificationManager.notify(0, notification);
    }

    // Incorrect usage as the receiver may be canceled at any time
    // do this in an service and in an own thread
    public void sendRegistrationIdToServer(String deviceId,
            String registrationId) {
        Log.d("C2DM", "Sending registration ID to my application server");
        HttpClient client = new DefaultHttpClient();
        HttpPost post = new HttpPost("http://local_machine_ip/php_push/");
        try {
            List nameValuePairs = new ArrayList(1);
            // Get the deviceID
            nameValuePairs.add(new BasicNameValuePair("deviceid", deviceId));
            nameValuePairs.add(new BasicNameValuePair("registrationid",
                    registrationId));
            Log.e(Columns.DATA, "Device Id is "+deviceId);
            Log.e(Columns.DATA, "registration id is "+registrationId);
            post.setEntity(new UrlEncodedFormEntity(nameValuePairs));
            HttpResponse response = client.execute(post);
            BufferedReader rd = new BufferedReader(new InputStreamReader( response.getEntity().getContent()));

            String line = "";
            while ((line = rd.readLine()) != null) {
                Log.e("HttpResponse", line);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

MessageReceivedActivity.java

package your.package.com;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;

public class MessageReceivedActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        setContentView(R.layout.activity_result);
        super.onCreate(savedInstanceState);
    }

    @Override
    protected void onResume() {
        // TODO Auto-generated method stub
        super.onResume();
        Bundle extras = getIntent().getExtras();
        if (extras != null) {
            Log.e("TAG", "onstart2");
            String message = extras.getString("payload");
            if (message != null && message.length() > 0) {
                TextView view = (TextView) findViewById(R.id.result);
                view.setText(message);
            }
        }
    }

    @Override
    protected void onStart() {
        super.onStart();
        Log.e("TAG", "onstart");
        Bundle extras = getIntent().getExtras();
        if (extras != null) {
            Log.e("TAG", "onstart2");
            String message = extras.getString("payload");
            if (message != null && message.length() > 0) {
                TextView view = (TextView) findViewById(R.id.result);
                view.setText(message);
            }
        }
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        Log.e("TAG", "onstart2");
    }
}

RegistrationResultActivity.java

package your.package.com;

import android.app.Activity;
import android.os.Bundle;
import android.provider.SyncStateContract.Constants;
import android.util.Log;
import android.widget.TextView;

public class RegistrationResultActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        setContentView(R.layout.activity_result);
        Bundle extras = getIntent().getExtras();
        if (extras != null) {
            String registrationId = extras.getString("registration_id");
            if (registrationId != null && registrationId.length() > 0) {
                TextView view = (TextView) findViewById(R.id.result);
                view.setText(registrationId);
            }
        }else{
            Log.e(Constants.DATA,"Registration failed...");
        }

        super.onCreate(savedInstanceState);
    }
}

Edit AndroidManifest.xml file

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="your.package.com"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="8" />

    <permission
        android:name="your.package.com.permission.C2D_MESSAGE"
        android:protectionLevel="signature" />

    <uses-permission android:name="your.package.com.permission.C2D_MESSAGE" />
    <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
    <uses-permission android:name="android.permission.INTERNET" />

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

        <receiver
            android:name=".C2DMRegistrationReceiver"
            android:permission="com.google.android.c2dm.permission.SEND" >
            <intent-filter >
                <action android:name="com.google.android.c2dm.intent.REGISTRATION" >
                </action>
                <category android:name="your.package.com" />
            </intent-filter>
       </receiver>
        <receiver
            android:name=".C2DMMessageReceiver"
            android:permission="com.google.android.c2dm.permission.SEND" >
            <intent-filter >
                <action android:name="com.google.android.c2dm.intent.RECEIVE" >
                </action>
                <category android:name="your.package.com" />
            </intent-filter>
        </receiver>

        <activity
            android:launchMode="singleTop"
            android:name="RegistrationResultActivity" >
        </activity>
        <activity
            android:launchMode="singleTop"
            android:name="MessageReceivedActivity" >
        </activity>
    </application>

</manifest>

Step 6: Run the mobile application

Run the application on the eclips and and after installing app on the emulator , click on the register button and see the CatLog window on the clips (which is bottom of the window next to the console logs) and see the messages , you may notice that

device id is : d4dssjkd343jj3434
Registration Id is : null

then , application works fine. you cant get the registration id on the emulator.

Transfer project_name.apk from project_name\bin\ folder to android device and install it.

Turn ON WiFi and connect to the local network.

Step 7: Getting mobile registration ID

RUN the app and click on "register" button, now, you can see the notification massage saying, registration complete. then,

goto, C:\xampp\htdocs\php_push\ , now, you can see that new folder has been created called logfiles, inside that you can find push.log file. Open push.log files and you can device and registration id written

Device Id is :447ff1044f35c77d
Registration ID is  :APA91bGOwZvRecTKl1OeshYgYGujwQzrDeG2CjLiRB0qdyMx9_59Po1hCMq6cI90VtEjumXsPh8oYzalyZf06a7Ve_mx-uTOoGTwE1_o17r4tTR57GpZf0ILR2bHlVw1QnchRmhfI1o39jAZc5p1IlafqvtXRHmFrYxWtf9SY9917DjvIMRpCc8

Copy the registration ID from the push.log file and use following CURL command to send the message

Step 8: Sending Message  to mobile

curl --header "Authorization: GoogleLogin auth=DQAAAMMAAABEBszojeqMG9e8uFpzvzSv_leYyAtWNxdxh804jUnzgljrrjNeFuhB2OjZc0pc625SANcA8q1-X5Jp9wd156BbcDab1w-cXczAKITXUQTshdke0pzU4KjCDXrTzuNqPR3ogcaPQ7ICCJ-pgfzUmmx_IayjKz8QSiAyCc3bxiksyB4j617T_K0jiwzpy_5cQPg11GptA36Q3w6GouUMz10cr7mwxn1c7tV-d9zCHdeCf9ivED99GO8461i8wHCSqSltf50dmVutXevCeS94Zqb-
" "https://android.apis.google.com/c2dm/send" -d registration_id=APA91bGOwZvRecTKl1OeshYgYGujwQzrDeG2CjLiRB0qdyMx9_59Po1hCMq6cI90VtEjumXsPh8oYzalyZf06a7Ve_mx-uTOoGTwE1_o17r4tTR57GpZf0ILR2bHlVw1QnchRmhfI1o39jAZc5p1IlafqvtXRHmFrYxWtf9SY9917DjvIMRpCc8
 -d "data.message=message from third party server" -d collapse_key=0

After executing above command, you may received notifcation from C2DM server with the message "message from third party server"

Thats it, enjoy :) 






Thursday, May 3, 2012

Statusbar notification for android mobiles

Statusbar notification for Android  - API Level upto 10 (upto Android 2.3)

                String ns = Context.NOTIFICATION_SERVICE;
                NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns);
               
                int icon = R.drawable.ic_launcher;
                CharSequence tickerText = "Hello";
                long when = System.currentTimeMillis();
                Notification notification = new Notification(icon, tickerText, when);
               
                Context context = getApplicationContext();
                CharSequence contentTitle = "My notification";
                CharSequence contentText = "Hello World!";
                Intent notificationIntent = new Intent(context,AlarmNotificationActivity.class);
                PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);
                notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
               
                final int HELLO_ID = 1;

                mNotificationManager.notify(HELLO_ID, notification);

Notification Builder for Android - API Level 11 and above (Android 3.0 and up)

 

                Context ctx = getApplicationContext();
                Toast toast = Toast.makeText(ctx, "Visual notification", Toast.LENGTH_LONG);
                toast.show();
               
                Intent notificationIntent = new Intent(ctx, NotificationBuilderActivity.class);
                PendingIntent contentIntent = PendingIntent.getActivity(ctx,
                        0, notificationIntent,PendingIntent.FLAG_CANCEL_CURRENT);
                NotificationManager nm = (NotificationManager) ctx.getSystemService(
                        Context.NOTIFICATION_SERVICE);
                Resources res = ctx.getResources();
                Notification.Builder builder = new Notification.Builder(ctx);
                builder.setContentIntent(contentIntent)
                .setSmallIcon(R.drawable.ic_launcher)
                .setLargeIcon(BitmapFactory.decodeResource(res, R.drawable.ic_launcher))
                .setTicker("ticker string is here")
                .setWhen(System.currentTimeMillis())
                .setAutoCancel(true)
                .setContentTitle("Title")
                .setContentText("Notification text");
                Notification n = builder.getNotification();
                nm.notify(0, n);


Saturday, April 14, 2012

Hello word !! - Your First Android Project

Hello World is a traditional first program because it shows the very simple task of letting the user know you are there (or letting them know that you know that they are there).  It’s simple I/O to send a simple message.  I am going to give a very brief tour of some of the ways you can communicate with a user in Android in this post.  Half Dozen Hello Worlds will highlight some simple I/O and get you started writing your first Android programs.  First let’s go ahead and open Eclipse, where you have already setup Android, and create a new Android Project.  Go to File -> New -> Project.  Select Android Project and click next.

Fill out the information on the next screen for your project.


































You can put whatever you like for the project name, and application name.  Note that I selected Android SDK 1.1.  It is a good idea to select the oldest SDK which has all of the features your program needs, to increase compatibility across devices.  You can think of Create Activity as being similar to create Main Method.  The name of the Activity you place here is the class that will be called when Android tries to run your code.  Once you click Finish you should see the Project.






















If you expand the project, then expand the src folder, and then the package you will see a Java file. This file will be named whatever you called your Activity (so mine is Hello.java).  For simplicity I am going to assume you used the same settings I did when creating your project.  If you did not just substitute your Activity name for mine.  Double click on Hello.java and look at the code that Eclipse has already provided you.

package com.learnandroid.hellowworld;
 
import android.app.Activity;
import android.os.Bundle;
 
public class Hello extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }
}

Just to give a quick overview.  The package declaration just places this class in the package we specified when we created the project.  The two import statements just import the minimum Android libraries needed for an Activity.  Since Eclipse created this as an Activity it is going to inherit from the Activity class, and we are overriding onCreate, which is called when an Activity is created.  We call onCreate of the superclass.  Finally, we are at the one line of code we really care about right now.
setContentView() tells android what to display to the user.  We are going to change this from the default so that our code looks like this:

package com.learnandroid.helloworld;
 
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
 
public class Hello extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        TextView helloText = new TextView(this);
        helloText.setText("Hello World");
        setContentView(helloText);
    }
}
 
Let’s briefly look at exactly what we did before moving on.  TextView is an android widget that displays text.  You can think of it as a label, something the user can read but cannot edit.  We instantiated a new TextView.  Then we called the setText method to set the text of it to “Hello World”.
Next we used setContentView to tell Android that our TextView was what we wanted to display in the main part of our application.
Once your code looks like mine, go to the Run Menu and select Run…  In the Run As Popup select Android Application.  Your Android Emulator should launch.  The Emulator will normally start on a lock screen, but as soon as you unlock the device you should see your application launch.



 Congratulations!  You have built the first Hello World. 
 
 
 



















Friday, April 13, 2012

Setting up the android environment with eclips






Before starting anything first we need to setup environment. Follow the simple steps to install all the prerequisites for android development. 
Click here for minimum system requirement 
1. Download and install JDK 1.6 or latest . you can get it from here
2. Download and install Android SDK and AVD, you can get it from here
3. Download Eclips, you can get it from here
4. Get the Eclips plugin for android.

Lets look at each step in detail


1. Installing JDK 1.6

You can get latest JDK from http://www.oracle.com/technetwork/java/javase/downloads/index.html. Download it and install if you do not have latest SDK. 

2. Installing Android SDK

First, we need to download the Android SDK.  You can get it here (just select your operating system).  Untar or Unzip this and put it in a folder of your choice (note the folder location, we’ll need it later).  Ironically, the latest Android SDK does not actually come with an Android SDK.  This is a little bit confusing, but Google is trying to provide us with tools to keep up to date instead of just giving us the actual SDK.  What that means for right now, however, is that we have a little bit more work to do before we actually have the SDK.  Go into the unpacked Android SDK folder and you should see a folder named Tools.  Open that and run the “Android” you find in this folder.  This should bring up the Android SDK Manager.

 If you click on Available Packages there should be a repository listed (in mine it lists https://dl-ssl.google.com/android/repository/repository.xml).  Check the repository and click on refresh.  It should give you a list of available Google APIs and Android SDKs.
If you get an error about not being able to access the repository via HTTPS click on the Settings Menu on the left.  Put a checkmark next to Force https://… sources to be fetched using http://… and click Save and Apply.  Go back to Available Packages and click refresh again.  Put a checkmark next to the SDK you want to install, and click install selected.  The SDK version you want is going to be determined by what is installed on your Android device (or your consumer’s android devices).  If you don’t know, just get the latest for now.  You can always come back and get other versions.
After it finishes downloading you should see the SDK listed in Installed Packages.



2. Installing Eclipse

Eclipse is a free, open source IDE. Basically, an environment that will give you everything you need to get started programming in Java. Now you might be saying, “Wait a minute! I want to program in Android, not Java!” Your Android programs are going to be written in Java and run by a Virtual Machine on the Android device. Just think of Android as the framework and Java as the programming language you use to access the framework.
You can download Eclipse by going here and clicking on “Eclipse IDE for Java Developers” on the page.  I am not giving you the direct download link because Eclipse is multiplatform and clicking on that link will take you to the correct version of the software.  You might not want the 64 bit Ubuntu version of the software that the link provides me.  At the time of this writing, the current version of Eclipse is Galileo (version 3.5).
Now untar or unzip Eclipse and run it directly from the folder (there is no need to install anything).  It will ask you for a workbench location.



You can safely accept the default value and just click OK here.
At this point you will be at a welcome screen for Eclipse.  It has a link for an overview of Eclipse, and another for Tutorial on using Eclipse.  If you have never used Eclipse before it might be a good idea to check these out.

3. Get the Android Eclipse Plugin

To get setup for Android development, we are going to open the help menu and select “Install New Software”.  On this screen click Add.  In the window that pops up put in a name for your reference (I just used “Android SDK”) and the URL https://dl-ssl.google.com/android/eclipse/



When you click OK there should be an entry for “Developer Tools” in the list.  Click the checkbox next to this and click on Next.  You will see a screen listing what is going to be installed (Android DDMS and Android Development Tools).  Click Next again, read the license agreement.  If you accept the agreement and click finish then you should see the software install into your Eclipse environment.  Finally, you should get a message recommending that you restart Eclipse.  Tell it to restart.
After you restart click on the Window menu and select Preferences (Eclipse Menu and Preferences if you’re on a Mac).  Click on Android and you will probably get a message complaining that Eclipse doesn’t know where the SDK is located.  Click on browse next to SDK Location and browse to the folder where you saved the SDK earlier.  Click Apply and you should see Android and the Version of the SDK you selected under Target Name.  Click OK.
Now the final step in setting up your Plugin is making a Virtual Device.  Under the Window Menu you should see Android SDK and AVD Manager.  This is a shortcut to the same Android SDK Manager we accessed earlier by running the Android executable in the SDK folder.  Click on Virtual Devices and Click on the New button on the right.  Give this whatever name you like (I called mine DebugAVD since that is how I will be using it) and select your SDK from the Target drop down.  Click on Create AVD and you should get a confirmation message.  Your AVD should now be listed on this screen.
That’s it, you’re ready to go.  Go to File, New, Project and you will see an Android folder.  Expand the folder, click on Android Project.  Fill out the form here, click finished, and you’ll be ready to program.

 Thats it !!!!. Now you are ready to do a magic with Android.