Posts

Showing posts from August, 2012

TabHost without extending TabActivity...Example

TabDemo.java
package com.hcl.tab;
import android.app.Activity;
import android.app.ActivityGroup;
import android.content.Intent;
import android.os.Bundle;
import android.os.Bundle;
import android.util.Log;
import android.widget.TabHost;

public class TabDemo extends ActivityGroup
{  
 TabHost host;
   /** Called when the activity is first created. */

   @Override
   public void onCreate(Bundle savedInstanceState)
   {
      Log.i(this.toString(), "OnCreate");

      super.onCreate(savedInstanceState);
      setContentView(R.layout.main);

      Log.i(this.toString(), "get tab host");

      this.host = (TabHost) findViewById(R.id.tabhost);
      this.host.setup(getLocalActivityManager());
      host.setup();

      Log.i(this.toString(), "add tabs");
      host.addTab(host.newTabSpec("one")
                      .setIndicator("First Results")
                      .setContent(new Intent(this, FirstResultsListViewActivity.cl…

Why StAx Parser?

XML Parsing in Java

Parsing XML
If you are a beginner to XML using Java then this is the perfect sample to parse a XML file create Java Objects and manipulate them.

The idea here is to parse the employees.xml file with content as below

<?xml version="1.0" encoding="UTF-8"?>
<Personnel>
  <Employee type="permanent">
        <Name>Seagull</Name>
        <Id>3674</Id>
        <Age>34</Age>
   </Employee>
  <Employee type="contract">
        <Name>Robin</Name>
        <Id>3675</Id>
        <Age>25</Age>
    </Employee>
  <Employee type="permanent">
        <Name>Crow</Name>
        <Id>3676</Id>
        <Age>28</Age>
    </Employee>
</Personnel> From the parsed content create a list of Employee objects and print it to the console. The output would be something like Employee Details - Name:Seagull, …

Simplest way to read data from an XML file into a Java program....Using DOM parser

This is the simplest way to read data from an XML file into a Java program. I have also included some basic error checking, so you can directly cut-paste this code with a few changes ofcourse. All you have to do is change the XML tags within the program to match those that are present in your XML file.
XML File<book> <person> <first>Kiran</first> <last>Pai</last> <age>22</age> </person> <person> <first>Bill</first> <last>Gates</last> <age>46</age> </person> <person> <first>Steve</first> <last>Jobs</last> <age>40</age> </person> </book> Program OutputRoot element of the doc is book
Total no of people : 3
First Name : Kiran
Last Name : Pai
Age : 22
First Name : Bill
Last Name : Gates
Age : 46
First Name : Steve
Last Name : Jobs
Age : 40
Program ListingThe Java program to read the above XML file is shown below. Go thro…

Inter process communication(IPC)

Image
What is IPC? Inter-Process Communication (IPC) is a set of techniques for the exchange of data among multiple threads in one or more processes. Processes may be running on one or more computers connected by a network. IPC techniques are divided into methods for message passing, synchronization, shared memory, and remote procedure calls (RPC). The method of IPC used may vary based on the bandwidth and latency of communication between the threads, and the type of data being communicated.
A Socket-based IPC Tutorial http://people.cis.ksu.edu/~singh/CIS725/Fall99/programs/sock_ipc_tut.html

JIPC - Java Interprocess Communication Server http://www.garret.ru/jipc/jipc.html
Inter-Process Communication Module Index http://www.cs.gmu.edu/cne/modules/ipc/term-index.html

Thread

Image
process: A process consists of the memory space allocated by the operating system that can contain one or more threads. A thread cannot exist on its own; it must be a part of a process. A process remains running until all of the non-daemon threads are done executing.
Multithreading enables you to write very efficient programs that make maximum use of the CPU, because idle time can be kept to a minimum.
Life Cycle of a Thread:A thread goes through various stages in its life cycle. For example, a thread is born, started, runs, and then dies. Following diagram shows complete life cycle of a thread.
Above mentioned stages are explained here:
New: A new thread begins its life cycle in the new state. It remains in this state until the program starts the thread. It is also referred to as a born thread.Runnable: After a newly born thread is started, the thread becomes runnable. A thread in this state is considered to be executing its task.Waiting: Sometimes a thread transitions to the waiting st…

Implementation differences in GET and POST:

Differences in GET and POST:
they have different encoding schemes. multipart/form-data is for POST onlythe result of POST may not result in an actual page.url limit necessitates use of POSTIf you are using HIDDEN inputs in form then submitting a GET request reveals those inputs GET is for data retrieval only. You can refine what you are getting but it is a read only setup and yes, as you mentioned anything used for refinement are part of the URL.POST is meant for sending data, but is generally a way to 'break' the simple workings of HTML because you are neither guaranteed of anything that is happening, it can just fetch data, send data or delete data.

AsyncTask in Android Expample

Image
AsyncTask is an abstract class that provides several methods managing the interaction between the UI thread and the background thread. it’s implementation is by creating a sub class that extends AsyncTask and implementing the different protected methods it provides.
Let’s demonstrate how to user AsyncTask by creating a simple activity that has two buttons and a progress bar:
<?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">
 <ProgressBar android:id="@+id/progressBar"
  style="?android:attr/progressBarStyleHorizontal" android:layout_width="match_parent"
  android:layout_height="wrap_content" android:indeterminate="false"
  android:max="10" android:padding="10dip">
 </Progre…

MNC: Android and Java interview questions

Java Questions: 1)What is the difference between Java IPC and Android IPC? 2)What is the main specific difference between GET and POST? 3)Factory design pattern 4)What are all the design patterns you know? 5)What is throw, throws and Throwable? 6)Explain, How synchronization concepts works internally in java ?(nottechnically) 7)Explain synchronization through operating system concepts? 8)What will happen if Object obj=null; Synchronized(obj){ } 9)Rules to use synchronization? 10)About Synchronization concept of static and non- static method using Class level lock ? a) If more than one threads are running then how threads they can operate on same static synchronized method simultaneously? b) Class level lock, can have more than 1 threads are able to execute in non-static synchronized method 11) Difference of Hashtable , Hashset andHashmap? Android questions: 1)Life cycle of Activity 2)What is Empty process? 3)How many types of broadcast receivers and explain their features and main difference betwwen t…

Serialization

Image
Serialization is the process of converting a set of object instances that contain references to each other into a linear stream of bytes, which can then be sent through a socket, stored to a file, or simply manipulated as a stream of data. Serialization is the mechanism used by RMI to pass objects between JVMs, either as arguments in a method invocation from a client to a server or as return values from a method invocation. In the first section of this book, I referred to this process several times but delayed a detailed discussion until now. In this chapter, we drill down on the serialization mechanism; by the end of it, you will understand exactly how serialization works and how to use it efficiently within your applications. The Need for SerializationEnvision the banking application while a client is executing a withdrawal. The part of the application we're looking at has the runtime structure shown in Figure 10-1. Figure 10-1.Runtime structure when making a withdrawal
What does …