Friday 15 March 2013

Android Google Cloud Messaging Server Side Code

GCM Server Side Application:

To create server side application, follow these steps:

1. Create a new java project as:


2. Enter name for new java project e.g. NotificationServer


3. Click on Next button. Add gcm-server.jar library and json-simple-1.1.1.jar library to this project by clicking on Add External JARs button. Specify your path of libraries to add to project.Then click on Finish button.


4. New java project is created now and available in Project Explorer.


5. Now create a package, by right click on src->new->package as:

6. Enter name of package and click on Finish button.


7. Now after creating package, add a class GCMServer to package and click on Finish button.


8. Finally, write the actual code to java project i.e. server side code. Here is complete code for server side implementation on java.


package com.push.notification.server;

import java.util.ArrayList;

import com.google.android.gcm.server.Message;
import com.google.android.gcm.server.MulticastResult;
import com.google.android.gcm.server.Result;
import com.google.android.gcm.server.Sender;

public class GCMServer {

    public static void main(String args[]) {

        new Thread(){

            public void run(){
    
                try {
                    //Please add here your project API key: "Key for browser apps (with referers)".
                    //If you added "API key Key for server apps (with IP locking)" or "Key for Android apps (with certificates)" here
                    //then you may get error responses.
                    Sender sender = new  Sender("AIzaSyB7Ej255tpTaemk_-Ljmn4GcklldT14Hp4");

                    // use this to send message with payload data
                    Message message = new Message.Builder()
                    .collapseKey("message")
                    .timeToLive(3)
                    .delayWhileIdle(true)
                    .addData("message", "Welcome to Push Notifications") //you can get this message on client side app
                    .build(); 
 
                    //Use this code to send notification message to a single device
                    Result result = sender.send(message,
                            "APA91bEbKqwTbvvRuc24vAYljcrhslOw-jXBqozgH8C2OB3H8R7U00NbIf1xp151ptweX9VkZXyHMik022cNrEETm7eM0Z2JnFksWEw1niJ2sQfU3BjQGiGMq8KsaQ7E0jpz8YKJNbzkTYotLfmertE3K7RsJ1_hAA",
                            1);
                    System.out.println("Message Result: "+result.toString()); //Print message result on console

                    //Use this code to send notification message to multiple devices
                    ArrayList<String> devicesList = new ArrayList<String>();
                    //add your devices RegisterationID, one for each device               
                    devicesList.add("APA91bEbKqwTbvvRuc24vAYljcrhslOw-jXBqozgH8C2OB3H8R7U00NbIf1xp151ptweX9VkZXyHMik022cNrEETm7eM0Z2JnFksWEw1niJ2sQfU3BjQGiGMq8KsaQ7E0jpz8YKJNbzkTYotLfmertE3K7RsJ1_hAA");   
                    devicesList.add("APA91bEVcqKmPnESzgnGpEstHHymcpOwv52THv6u6u2Rl-PaMI4mU3Wkb9bZtuHp4NLs4snBl7aXXVkNn-IPEInGO2jEBnBI_oKEdrEoTo9BpY0i6a0QHeq8LDZd_XRzGRSv_R0rjzzZ1b6jXY60QqAI4P3PL79hMg");   

                    //Use this code for multicast messages   
                    MulticastResult multicastResult = sender.send(message, devicesList, 0);
                    System.out.println("Message Result: "+multicastResult.toString());//Print multicast message result on console

                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }.start();   
    }
}

9. Copy this code to your project and then click on Run button to execute this java project.

This is all what you need to do on server side.For client end code please refer to "Google Cloud Messaging (GCM) Client Code".

In case of any query please don't hesitate to contact.
Please don't forget to give your feedback. Thanks.

































Thursday 14 March 2013

Frequently asked Android Interview Questions

Q.1 What is an activity?
An activity is a single screen in an application which supports Java code.

Q.2 Explain different phases of Activity Life Cycle.
Whole life cycle consist of Activity launched, Activity Running and Activity shutdown.
onCreate()
onStart()
onResume()
onPause()
onStop()
onDestroy()

Q.3 Is it true that android applications can only be programmed in Java?
No. android applications can also be programmed in C++ and C#.

Q.4 What is the architecture of Android?
On Top, Applications (Contact, Browser and phone)
Application framework (Activity Manager, Content providers, view system and package manager)
Below Application Frame work > System libraries like, webkit, SSL and OpenGL.
On Bottom, Linux Kernel (includes display and camera)

Q.5 What is APK format?
The APK file is compressed form of an elongated version which is AndroidManifest.xml, .dex files, resource files etc.

Q.6 Which virtual machine does Android runs on?
Android runs on Dalvik Virtual Machine(DVM).



Q.7 How is the nine patch image different from a regular bitmap?
Regular bitmaps are not re-sizable while nine patch allows drawing a bitmap image in nine sections.

Q.8 How to test and run android application during development?
Android app can be tested and run on emulator and/or real android phone during development. 

Q.9 Java compiled code also runs on android? Yes, how? No, Why?
No, java code compiled into bytecode by jvm but anroid code compiled into .dex format by dvm.

Q.10 What is a service in Android?
A service doesn’t have a visual user interface, instead it runs in the background for an indefinite period of time. For example, a service might play background.

Q.11 How many ways used to create the user interface in android?
There are two ways to create user interface, One is by xml and the other one is by coding.

Q.12 How does Android system track the applications on android phone?
Android system assigns each application a unique ID that is called Linux user ID. This ID is used to track each application.

Q.13 What is an Intent?
An Intent is basically a message to say something to happen. Depending on the type of intent, you can start another activity, you can make call through intent etc.

Q. 14 How many types of Intents?
There are two types of Intents. 
1. Implicit Intents
2. Explicit Intents

Q.15 What is a resource?
All Android applications contains some sort of resources,they often have the user interface layouts in the form of XML files, app related icons, strings etc are all Android resources.

Q.16 What is the difference between startActivity() and startActivityforResult()?
startActivity() is used to start another activity while startActivityforResult() starts another activity and also returns some data to the activity from which it was called.

Frequently asked Object-Oriented Programming (OOP) questions.

Q.1 What is multithreading?
Multithreading is the technique in which more than one thread run independent of each other within the process.

Q.2 Does a class inherit the constructors of its superclass?
No, a class does not inherit constructors from any of its super classes.

Q.3 Class declared without any access modifiers, where may the class be accessed?
The class can only be accessed by other classes and interfaces that are defined within the same package.

Q.4 What is difference between overloading and overriding?
In overloading, there is a relationship between methods available in the same class whereas while in  overriding, there is relationship between a superclass method and subclass method.

Q.5 What is a dangling pointer?
A dangling pointer arises when you use the address of an object after its lifetime is over. 

Q.6 Differentiate Abstract Class and Interface?
Abstract Class allows the other classes to inherit from it but cannot be instantiated. 
Interface has only the declaration of the methods without the body.

Q.7 What is the difference between const and static?
Static keyword defines the scope of variables whereas const keyword defines the value of variable that can't be changed during program execution.

Q.8 What is static method in java?
A static method is one that belongs to a class rather than an object of a class. It can be invoked with class name while Non-static method belongs to an object of a class and can be invoked by the object of that class.

Q.9 What is polymorphism? Briefly explain with at least one example.
Polymorphism allows a function, or an object to have more than one forms. e.g. overloading.

Q.10 Differentiate multiple inheritance and multilevel inheritance?
In multiple inheritance a class can be derived from multiple classes while in multilevel inheritance a class can be derived from only one base class and this derived class become the base class of other class.

Q.11 What is early (compile-time) and late (run-time) binding?
In early (compile-time) binding the assignment of values to variables occur during the design time.
In late (run-time) binding the assignment of values to variables occur at run-time.

Q.12 What is exception handling?
An exception is basically an event that can occur during the execution of a program. These exceptions are handled properly through exception handling i.e. throw, try and catch keywords.

Q.13 What is “super” keyword?
Super keyword is used to invoke overridden method of the super class in derived class.

Q.14 What is inline function?
Compiler inserts/copy complete body of the function wherever that function is used/called in the program.

Q.15 How multiple inheritance implemented in java?

In fact multiple inheritance is not allowed in java. Java interfaces can be implemented instead of multiple inheritance.