Thursday, September 29, 2011

Android: Calling Activity of other Application using Intent



Custom component is created with the notifyNumber in one application.Calling the custom component activity from one more application through intent

Intent intent = new Intent(Intent.ACTION_MAIN);
intent.setClassName("com.cc.notify", "com.cc.notify.NotifyNumber");
startActivity(intent);

ACTION_MAIN: Activity Action: Start as a main entry point, does not expect to receive data.
setClassName (String packageName, String className)
Parameters
  packageName : The name of the package implementing the desired component.
  className: The name of a class inside of the application package that will be used as the component for this Intent.
Returns
  Returns the same Intent object, for chaining multiple calls into a single statement.





Tuesday, September 20, 2011

Binary Search in a List of Objects

Search a value from list of object by using binary search.

There is an employee class where, we need to find out the employee details by passing employee name.


Created a EmpTest.java class.
Used interface Comparable, to sort through employee name.

public class EmpTest implements Comparable<EmpTest> {
private int id;
private String name;
private String age;

public EmpTest() {
}

public EmpTest(int id, String name, String age) {
super();
this.age = age;
this.id = id;
this.name = name;
}

// setters and getters


@Override
public int compareTo(EmpTest emp)
{
return this.name.compareTo(emp.getName());
}
}


Creating ListTest.java class, where created employee details and added into the list.
before performing binary search, sort the list values.
If the corresponding search value is not found, while using binary search, it throws java.lang.ArrayIndexOutOfBoundsException.


public class ListTest {

public static void main(String[] args) {

List<EmpTest> listValues = new ArrayList<EmpTest>();

listValues.add(new EmpTest(1, "Test1", "23"));
listValues.add(new EmpTest(6, "Test6", "28"));
listValues.add(new EmpTest(2, "Test2", "24"));
listValues.add(new EmpTest(5, "Test5", "27"));
listValues.add(new EmpTest(3, "Test3", "25"));

Collections.sort(listValues);

try
{
EmpTest empTest = listValues.get(Collections.binarySearch(
listValues, new EmpTest(0, "Test6", null)));
System.out.println("Emp Name : " + empTest.getName() + ", Age : "
+ empTest.getAge());
}
catch (ArrayIndexOutOfBoundsException e)
{
System.out.println("Employee Name is not Existing!!!");
}

}
}


Difference between ArrayList and CopyOnWriteArrayList

When list value is added dynamically when an Iterator is iterating, we will get an java.util.ConcurrentModificationException.


To avoid, we can go for CopyOnWriteArrayList.
Iterators created by a CopyOnWriteArrayList object cannot change the underlying array.Since, We did not get any java.util.ConcurrentModificationException as CopyOnWriteArrayList keeps a copy of original List.


Eg: 
List values are printing using iterator.




CopyOnWriteArrayList values printing using iterator.




for more understanding, refer URL: http://techvivek.wordpress.com/2009/08/29/difference-between-arraylist-and-copyonwritearraylist/

Monday, September 19, 2011

Android Basic things


1. What is Activity?
                A single screen in an application, with supporting Java code, derived from the Activity class.

2. Activity LifeCycle? and Explanation?
               

3. What is Intent? How u will call, Activity using intent from same *.apk file and different *.apk file?

An message object that you can use to launch or communicate with other applications/activities asynchronously. An Intent object is an instance of Intent.

4. what is adb? and the usage of commands?
Android Debug Bridge (adb) is a versatile command line tool that lets to communicate with an emulator instance or connected Android-powered device. It is a client-server program that includes three components:
  • A client, which runs on your development machine. We can invoke a client from a shell by issuing an adb command. Other Android tools such as the ADT plugin and DDMS also create adb clients.
  • A server, which runs as a background process on your development machine. The server manages communication between the client and the adb daemon running on an emulator or device.
  • A daemon, which runs as a background process on each emulator or device instance.
You can find the adb tool in <sdk>/platform-tools/.

5. Why DVM? when JVM is there?

Dalvik Virtual machine:
 (DVM) is Register Architecture, designed to run on low memory, uses its own byte code and runs .Dex file (Dalvik Executable File)

Java Virtual Machine:
 (JVM) is Stack Architecture, uses java byte code and runs .class file having JIT.
Java  source code is compiled by the Java compiler into .class files. Then the dx (dexer) tool, part of the Android SDK processes the .class files into a proprietary file format called DEX that contains Dalvik bytecode.
What is DVM(Dalvik Virtual Machine):
  • All Applications Written in java and are Converted into .dex file
  • dex means Dalvik Executable File
  • Every Application in Android will runs in own Process
  • Not a Traditional JVM.Its a Customized Virtual Machine which will run multiple VM's in a single Device.
  • VM uses Linux kernel to handle Low Level Functionality which is threading, memory management, Security, etc.  
  • .dex is a binary code 
  • VM is a Registered based
 Advantage of DVM over JVM
JVM requires lot of memory and in mobile resources are very limited. Therefore it is not possible to have all classes and methods which are present in core java used in Mobile applications, so that we are moving for DVM. 

6. What is android:minSdkVersion?
To specify API Level requirements, add a <uses-sdk> element in the application's manifest, with one or more of these attributes:
android:minSdkVersion — The minimum version of the Android platform on which the application will run, specified by the platform's API Level identifier.
android:targetSdkVersion — Specifies the API Level on which the application is designed to run. In some cases, this allows the application to use manifest elements or behaviors defined in the target API Level, rather than being restricted to using only those defined for the minimum API Level.
android:maxSdkVersion — The maximum version of the Android platform on which the application is designed to run, specified by the platform's API Level identifier.

7. What is service? Usage of bindservice?
  • A service can run in the background to perform work even while the user is in a different application
  • A service can allow other components to bind to it, in order to interact with it and perform interprocess communication
  • A service runs in the main thread of the application that hosts it, by default
A service can essentially take two forms:
Started
A service is "started" when an application component (such as an activity) starts it by calling startService(). Once started, a service can run in the background indefinitely, even if the component that started it is destroyed. Usually, a started service performs a single operation and does not return a result to the caller. For example, it might download or upload a file over the network. When the operation is done, the service should stop itself.
Bound
A service is "bound" when an application component binds to it by calling bindService(). A bound service offers a client-server interface that allows components to interact with the service, send requests, get results, and even do so across processes with interprocess communication (IPC). A bound service runs only as long as another application component is bound to it. Multiple components can bind to the service at once, but when all of them unbind, the service is destroyed.

8. What is intent-filter?
A filter object that an application declares in its manifest file, to tell the system what types of Intents each of its components is willing to accept and with what criteria. Through an intent filter, an application can express interest in specific data types, Intent actions, URI formats, and so on. When resolving Intent, the system evaluates all of the available intent filters in all applications and passes the Intent to the application/activity that best matches the Intent and criteria.

9. What is Activity stack?
When the current activity starts another, the new activity is pushed on the top of the stack and takes focus. The previous activity remains in the stack, but is stopped. When an activity stops, the system retains the current state of its user interface. When the user presses the BACK key, the current activity is popped from the top of the stack (the activity is destroyed) and the previous activity resumes (the previous state of its UI is restored). Activities in the stack are never rearranged, only pushed and popped from the stack—pushed onto the stack when started by the current activity and popped off when the user leaves it using the BACK key.  As such, the back stack operates as a "last in, first out" object structure.

10. What is Content Provider?
Content providers store and retrieve data and make it accessible to all applications. They're the only way to share data across applications; there's no common storage area that all Android packages can access.
Android ships with a number of content providers for common data types (audio, video, images, personal contact information, and so on).

11. What is DataStorage? How many ways are there?
Android provides several options for you to save persistent application data. The solution you choose depends on your specific needs, such as whether the data should be private to your application or accessible to other applications (and the user) and how much space your data requires.
Your data storage options are the following:
Shared Preferences
Store private primitive data in key-value pairs.
Internal Storage
Store private data on the device memory.
External Storage
Store public data on the shared external storage.
SQLite Databases
Store structured data in a private database.
Network Connection
Store data on the web with your own network server.

12. What are application components?
Android application consists of one or more activities, services, listeners, and intent receivers.

13. What are UI Guidelines?

Icon Design Guidelines and Android Icon Templates

The Icon Guidelines describe each kind of icon in detail, with specifications for the size, color, shading, and other details for making all your icons fit in the Android system

Widget Design Guidelines

A widget displays an application's most important or timely information at a glance, on a user's Home screen. These design guidelines describe how to design widgets that fit with others on the Home screen. They include links to graphics files and templates

Activity and Task Design Guidelines

These guidelines describe how activities work and describes important underlying principles and mechanisms, such as multitasking, activity reuse, intents, the activity stack, and tasks. It covers this all from a high-level design perspective.

Menu Design Guidelines

These guidelines describe the difference between Options and Context menus, how to arrange menu items, when to put commands on-screen, and other details about menu design
               
14. How Bundle is used?
Intent mIntent = new Intent(this, Example.class);
Bundle mBundle = new Bundle();
mBundle.putString(key, value);
mIntent.putExtras(mBundle);

15. How many Layouts are there? and what are there?

alternativeLayout

16. What is difference between ListView and ListActivity?

ListActivity hosts a ListView object that can be bound to different data sources, typically either an array or a Cursor holding query results.
ListActivity has a default layout that consists of a single, full-screen list in the center of the screen. However, if we desire, we can customize the screen layout by setting your own view layout with setContentView() in onCreate(). To do this, your own view MUST contain a ListView object with the id "@android:id/list"

17. What is manifest file?

The Manifest class is used to obtain attribute information for a JarFile and its entries.

This file must declare all activities, services, broadcast receivers and content provider of the application. It must also contain the required permissions for the application.


18. How android supports Multiple Screens?

<supports-screens android:resizeable=["true"| "false"]
                  android:smallScreens=["true" | "false"]
                  android:normalScreens=["true" | "false"]
                  android:largeScreens=["true" | "false"]
                  android:xlargeScreens=["true" | "false"]
                  android:anyDensity=["true" | "false"]
                  android:requiresSmallestWidthDp="integer"
                  android:compatibleWidthLimitDp="integer"
                  android:largestWidthLimitDp="integer"/>
  • xlarge screens are at least 960dp x 720dp
  • large screens are at least 640dp x 480dp
  • normal screens are at least 470dp x 320dp
  • small screens are at least 426dp x 320dp

res/layout/my_layout.xml             // layout for normal screen size ("default")

res/layout-small/my_layout.xml       // layout for small screen size

res/layout-large/my_layout.xml       // layout for large screen size

res/layout-xlarge/my_layout.xml      // layout for extra large screen size

res/layout-xlarge-land/my_layout.xml // layout for extra large in landscape orientation



res/drawable-mdpi/my_icon.png        // bitmap for medium density

res/drawable-hdpi/my_icon.png        // bitmap for high density

res/drawable-xhdpi/my_icon.png       // bitmap for extra high density
Terms and concepts
Screen size
Actual physical size, measured as the screen's diagonal.
For simplicity, Android groups all actual screen sizes into four generalized sizes: small, normal, large, and extra large.
Screen density
The quantity of pixels within a physical area of the screen; usually referred to as dpi (dots per inch). For example, a "low" density screen has fewer pixels within a given physical area, compared to a "normal" or "high" density screen.
For simplicity, Android groups all actual screen densities into four generalized densities: low, medium, high, and extra high.
Orientation
The orientation of the screen from the user's point of view. This is either landscape or portrait, meaning that the screen's aspect ratio is either wide or tall, respectively. Be aware that not only do different devices operate in different orientations by default, but the orientation can change at runtime when the user rotates the device.
Resolution
The total number of physical pixels on a screen. When adding support for multiple screens, applications do not work directly with resolution; applications should be concerned only with screen size and density, as specified by the generalized size and density groups.
Density-independent pixel (dp)
A virtual pixel unit that you should use when defining UI layout, to express layout dimensions or position in a density-independent way.
The density-independent pixel is equivalent to one physical pixel on a 160 dpi screen, which is the baseline density assumed by the system for a "medium" density screen. At runtime, the system transparently handles any scaling of the dp units, as necessary, based on the actual density of the screen in use. The conversion of dp units to screen pixels is simple: px = dp * (dpi / 160). For example, on a 240 dpi screen, 1 dp equals 1.5 physical pixels. You should always use dp units when defining your application's UI, to ensure proper display of your UI on screens with different densities.