Auto-generate Flow Chart from Java/C++ Codes:

Raptor Flowchart Tutorial For Beginners

Monday, July 23, 2012

Android Intent (Implicit)



An implicit intent does not name a target component that should act upon the intent. I  also said that the android platform resolves as to which component is best suited to respond to an Implicit Intent. How does this happen?

Basically, an Intent object has the following information (among other things like Component name, extras and flags) which is of interest for implicit intents:
  • Action 
  • Category 
  • Data 
So, the android platform compares these 3 (action, category and data) to something called "Intent Filters" that are declared by probable target components who are willing to accept Implicit Intent calls.
 i.e. Intent Filters are the way of any component to advertise its own capabilities to the Android system. This is done declaratively in the AndroidManifest.xml file.

So here are some important points to remember:
  1. Implicit Intents do not specify a target component 
  2. Components willing to receive implicit intents have to declare their ability to handle a specific intent by declaring intent filters 
  3. A component can declare any number of Intent Filters 
  4. There can be more than one component that declares the same Intent Filters and hence can respond to the same implicit intent. In that case the user is presented both the component options and he can choose which one he wants to continue with 
  5. You can set priorities for the intent filters to ensure the order of responses. 

There are 3 tests conducted in order to match an intent with intent filters:
  1. Action Test 
  2. Category Test 
  3. Data Test 
For more details about them, you may visit the Android developer documentation here.

Finally we shall look at declaring an implicit intent in one activity which will invoke one of the native activities of the platform by matching the intent filters declared by the same.

The complete code for a very simple implicit intent example that has been described in this article is available for download here.

The InvokeImplicitIntent Activity creates an implicit intent object "contacts". This intent object's component is not set. However, the action is set to "android.content.intent.ACTION_VIEW" and the data's URI is set to "People.CONTENT_URI". 

Such an intent matches with the intent filter declared by the view contacts native activity.
So, when you run this application, it displays the native UI for viewing the existing contacts on the phone!

Here is the relevant piece of code for the same:

01.Button viewContacts = (Button)findViewById(R.id.ViewContacts);
02. 
03.viewContacts.setOnClickListener(new OnClickListener() {
04. 
05.public void onClick(View v) {
06.Intent contacts = new Intent();
07.contacts.setAction(android.content.Intent.ACTION_VIEW);
08.contacts.setData(People.CONTENT_URI);
09.startActivity(contacts);
10.}
11.});

In this manner many of the native applications can be seamlessly invoked as one of the activities in our applications through implicit intents.

---------------------------------------------------------------------------------------------------------
Updated for Android SDK 1.6 and above:
The above example uses Android SDK 1.5.
From SDK 1.6 and above, the Contact.People class has been deprecated and we need to use the ContactsContract class. So the line in code
      
1.contacts.setData(People.CONTENT_URI);

has to be replaced by 

1.contacts.setData(ContactsContract.Contacts.CONTENT_URI);


Here is the complete source code that has been tested with Android SDK 2.1




from:http://mobile.dzone.com/articles/implicit-intent-android

Android Intent (Explicit)





Communication between 2 activities happens through an intent. 

There are 2 types of intents that Android understands.
  1. Explicit Intent
  2. Implicit Intent

In an Explicit intent, you actually specify the activity that is required to respond to the intent. In other words, you explicitly designate the target component. This is typically used for application internal messages.

In an Implicit intent (the main power of the android design), you just declare an intent and leave it to the platform to find an activity that can respond to the intent. Here, you do not declare the target component and hence is typically used for activating components of other applications seamlessly

Note: Here for simplicity sake I tell an activity responds to an intent, it could as well be other types of components.

Now I will jump into the example which you can download from here:

This example has 2 activities:
  1. InvokingActivity
  2. InvokedActivity

The InvokingActivity has a button "Invoke Next Activity" which when clicked explicitly calls the "InvokedActivity" class.
The relevant part of the code is here:
1.Button invokingButton = (Button)findViewById(R.id.invokebutton);
2.invokingButton.setOnClickListener(new OnClickListener() {
3. 
4.public void onClick(View v) {
5.Intent explicitIntent = new Intent(InvokingActivity.this,InvokedActivity.class);
6.startActivity(explicitIntent);
7.}
8.});

As explained in part 1 of the series, this is very much like an API call with compile time binding.

NOTE: The layout for InvokingActivity is defined in main.xml and for InvokedActivity in InvokedActivity.xml. The downloadable example can be opened in Eclipse Ganymede as an android project and can be executed.


from:http://mobile.dzone.com/articles/explicit-intent-android

Android Project Structure in Eclipse



In this article, we will examine the Android project structure and file organization in detail, by creating a new Android project in Eclipse. Before that, it will be better to read my earlier post on how to setup a complete Android development environment in the Android development tutorial series.
When we start a new Android project in Eclipse, it will automatically creates a lot of files and folders for us. We will look at the meaning and functions of each of these folders in the Android project structure one by one.
You have to create a new Android project in Eclipse to see the project structure.
Follow these steps:
(The screens shown here may vary from yours as it depends on the version of your ADT and SDK)
Open Eclipse
Go to File→New→Project
Android Project Structure

Expand Android Folder, select Android Project and click Next
Android Project Structure

Enter Project Name as ‘TestApp’, leave other options unchanged and click Next
Android Project Structure

Select a build target here from the list of installed Platforms. I’ve selected here 2.3.3 API level 10. Click Next
Android Project Structure
Application Info window appears. Enter these values:
Application Name: ‘TestApp’
Package Name: com.’testapp’
Check Create Activity enter the activity name as ‘TestAppActivity’. Click Finish
Android Project Structure

You’ve created a new Android Project in Eclipse and now you can see the project structure in the Package Explorer window.

Android Project Structure Tree in Package Explorer

Android Project Structure
Here is the brief description of important files/folders in the Android project structure:

/SRC

The src folder contains the Java source code files of your application organized into packages. You can have more than one package in your Android application. Its always a good practice to break the source code of your application into different packages based on its core functionality.  All the source files of your Activities, Services etc. Goes into this folder. In the above screen, you can see the source file of the Activity that we created for our project.

/GEN

The files in the gen folder are automatically generated by the ADT. Here the R.java file contains reference/index  to all the resources in the res we use in our program. Each time we add a new resource to the project, ADT will automatically regenerate the R.java file containing reference to the newly added resource. You should not edit the contents of R.java file manually or otherwise your application may not compile.

/ANDROID

This folder is also called Android target library in Android project structure. The version number will be  same as the build target version that we choose while creating a new project. The android.jar file contains all the essential libraries required for our program.

/ASSETS

The assets folder is used to store raw asset files. You can keep any raw data in the assets folder and there’s an asset manager in Android to read the data stored in the folder. The raw data can be anything such as audio, video, images etc. On important point about assets folder is that the data stored in this folder can’t be referenced by an ID. To access a data in this folder, we have to work with bits and bytes.

/BIN

/bin folder is where our compiled application files go. When we successfully compile an application, this folder will contain java class files, dex files which are executable under Dalvik virtual machine, apk archives etc.

/RES

Res folder is where we store all our external resources for our applications such as images, layout XML files, strings, animations, audio files etc.
Sub folders:

/res/drawable

This folder contains the bitmap file to be used in the program. There are three different folders to store drawables. They are drawable-ldpidrawable-mdpi, drawable-hdpi. The folders are to provide alternative image resources to specific screen configurations. Ldpi, mdpi & hdpi stands for low density, medium density & high density screens respectively. The resources for each screen resolutions are stored in respective folders and the android system will choose it according to the pixel density of the device.

/res/layout

XML files that defines the User Interface goes in this folder.

/res/values

XML files that define simple values such as strings, arrays, integers, dimensions, colors, styles etc. are placed in this folder.

/res/menu

XML files that define menus in your application goes in this folder

Android Manifest file

AndroidManifest.xml is one of the most important file in the Android project structure. It contains all the information about your application. When an application is launched, the first file the system seeks is the AndroidManifest file. It actually works as a road map of your application, for the system.
The Android Manifest file contains information about:
  • Components of your application such as Activities, services etc.
  • User permissions required
  • Minimum level of Android API required