Android Tutorials for Beginner Part-5

Android Fetching from called activity
The next logical step in learning the Android development is to look at how can you call or invoke one activity from another and get back data from the called activity back to the calling activity. For simplicity sake, let us name the first calling activity as parent activity and the invoked activity as the child activity.

For simplicity sake, I use an explicit intent for invoking the child activity. For simple invocation without expecting any data back, we use the method startActivity(). However, when we want a result to be returned by the child activity, we need to call it by the method startActivityForResult(). When the child activity finishes with the job, it should set the data in an intent and call the method setResult(resultcode, intent) to return the data through the intent.

The parent activity should have overridden the method onActivityResult(…) in order to be able to get the data and act upon it.

NOTE: for successful execution of this sequence of events, the child activity should call finish() after setResult(..) in order to give back the handle to the parent activity.

In summary, here are the methods to implement in the parent activity:


* 1. startActivtyForResult(..)
* 2. onActivityResult(…)

The child Activity should complete the work as usual and finally call:
* 1. setResult(…)
* 2. finish()

Let us delve into the example downloadable here:

The calling Activity is providing 2 buttons to view books and pens. On selecting one of them, either BooksActivity or PensActivity is called, which displays a list (using ListView) of the selected type of objects. The user can select one and the selected object is returned to the parent for display. (Note: this could be extended into a shopping cart example. I have kept it simple for the tutorial’s sake)

Since we are expecting to get back the selected object, the calling Activity’s code is like this:
Intent bookIntent = new Intent(); bookIntent.setClass(CallingActivity.this,BooksActivity.class);
startActivityForResult(bookIntent,BOOK_SELECT);
where BOOK_SELECT is just a constant to help us identify from which child activity the is result obtained, when there is more than 1 child activity, as in this case.


At this point the control is handed over to the BooksActivity. This displays the list of books and the user can scroll through and select a book. When the user selects a book, the selected book needs to be passed back to the CallingActivity. This is how it is done:
Object o = this.getListAdapter().getItem(position);
String book = o.toString();
Intent returnIntent = new Intent();
returnIntent.putExtra("SelectedBook",book);
setResult(RESULT_OK,returnIntent);
finish();

The first 2 lines show how to get the selected book from the ListView. Then, you create a new intent object, set the selected book as an extra and pass it back through the setResult(…) method call. The result code is set to RESULT_OK since the job has been successfully done. After that the finish() method is called to give the control back to the parent activity.

In the parent, the method that gets the control is onActivityResult(…)
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
switch(requestCode) {
case BOOK_SELECT:
if (resultCode == RESULT_OK) {
String name = data.getStringExtra("SelectedBook");
Toast.makeText(this, "You have chosen the book: " + " " + name, Toast.LENGTH_LONG).show();
break;
}
……….
}
}

Here you notice that the BOOK_SELECT constant is used to act upon the result. If the result code is RESULT_OK, we take the book selected from the “extra” of the intent that is returned from the child activity. data.getStringExtra("SelectedBook") is called to and the name returned is displayed through a Toast.

Android Tutorials for Beginner Part-4(Pre-Packed applications)

Android Tutorials for Beginner Part-4(Android Pre-Packed applications)
There are many applications that can pre-packaged into the android platform that can be reused by custom built applications because of the power of the android design that is based on implicit intents.(See part 3 of this series for implicit intents).

Here we will explore how to invoke the pre-packaged applications from our own through implicit intents. Note that in none of the snippets below, we actually call the pre-packaged or system applications. We just declare intents and pass them to an activity while starting the activity through startActivity() method. However, for each of these intents, the android platform finds the most befitting activity and invokes the same:

1. Call a number
Intent callNumber = new Intent();
callNumber.setAction(android.content.Intent.ACTION_CALL);
callNumber.setData(Uri.parse("tel:9440012345"));
startActivity(callNumber);

This will call the number 9440012345. For calling custom numbers, you could provide the user with a edit text field from which you can access the number and set it to the data above instead of a hard-coded number.

2. Browse the web for a given url:

Intent searchGivenText = new Intent(Intent.ACTION_WEB_SEARCH);
searchGivenText.putExtra(SearchManager.QUERY, "Android Examples);
startActivity(searchGivenText);

This will invoke the Google search engine to search the string "Android Examples" and return the results to you. This too can be generalized to accept a string from the user and set to the intent before starting the activity.

3. View google maps for a given location

Intent searchAddress = new Intent(Intent.ACTION_VIEW,
Uri.parse("geo:0,0?q=Bangalore"));
startActivity(searchAddress);

This shows the location of Bangalore on Google Maps.

4. View Contacts on the phone


Intent contacts = new Intent();
contacts.setAction(android.content.Intent.ACTION_VIEW);
contacts.setData(People.CONTENT_URI);
startActivity(contacts);

I have created an Android eclipse project which showcases all of these examples by taking inputs from the end user. You can access the same here.

Android Tutorials for Beginner Part-3(Implicit intent)

Android Tutorials for Beginner Part-3(Implicit intent)
We have seen in Part 2 how to use Explicit Intents to invoke activities through a very simple example. Now, we will move on to a more interesting concept of Implicit Intents and Intent Filters.

This requires a little of theoretical understanding before we move on to an example.

As described earlier, 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:
Button viewContacts = (Button)findViewById(R.id.ViewContacts);

viewContacts.setOnClickListener(new OnClickListener() {

public void onClick(View v) {
Intent contacts = new Intent();
contacts.setAction(android.content.Intent.ACTION_VIEW);
contacts.setData(People.CONTENT_URI);
startActivity(contacts);
}
});


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

Android Tutorials for Beginner Part-2(explicit intent )

Having introduced you to the basic anatomy of an android application in the Part 1 of the series, I would like to show you an example where communication between 2 activities happens through an intent.

However, just one more detail to be introduced as promised and that is -
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:

Button invokingButton = (Button)findViewById(R.id.invokebutton);
invokingButton.setOnClickListener(new OnClickListener() {

public void onClick(View v) {
Intent explicitIntent = new Intent(InvokingActivity.this,InvokedActivity.class);
startActivity(explicitIntent);
}
});

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.

In the next part of the series, we will see how to work with implicit intents which also needs us to understand intent-filters.

Android Tutorials for Beginner Part-1

Android Tutorials for Beginner Part-1
From a developer's perspective, the fundamental building blocks / components of Android are:
1. Activities
2. Services
3. Broadcast Receivers
4. Content Providers.

The means of communication between the above mentioned components is through

1. Intents
2. Intent Filters

The User Interface elements are by using what are called:

1. Views
2. Notifications

Now, having broadly classified the basics, I would like to give a simple definition for each of them, before we can appreciate the need for each of them.

Activity is the basic building block of every visible android application. It provides the means to render a UI. Every screen in an application is an activity by itself. Though they work together to present an application sequence, each activity is an independent entity.
Service is another building block of android applications which does not provide a UI. It is a program that can run in the background for an indefinite period.

Broadcast Receiver is yet another type of component that can receive and respond to any broadcast announcements.

Content Providers are a separate league of components that expose a specific set of data to applications.

While the understanding and knowledg
e of these four components is good enough to start development, the knowledge of the means of communication between the components is also essential. The platform designers have introduced a new conpect of communication through intents and intent filters.

Intents are messages that are passed between components. So, is it equivalent to parameters passed to API calls? Yes, it is close to that. However, the fundamental differences between API calls and intents' way of invoking components is

1. API calls are synchronous while intent-based invocation is asynchronous (mostly)
2. API calls are bound at compile time while intent-based calls are run-time bound (mostly)

It is these two differences that take Android platform to a different league.

NOTE: Intents can be made to work exactly like API calls by using what are called explicit intents, which will be explained later. But more often than not, implicit intents are the way to go and that is what is explained here.

One component that wants to invoke another has to only express its' "intent" to do a job. And any other component that exists and has claimed that it can do such a job through "intent-filters", is invoked by the android platform to accomplish the job. This means, both the components are not aware of each other's existence and can still work together to give the desired result for the end-user.

This dotted line connection between components is achieved through the combination of intents, intent-filters and the android platform.

This leads to huge possibilities like:
1. Mix and match or rather plug and play of components at runtime
2. Replacing the inbuilt android applications with custom developed applications
3. Component level reuse within and across applications
4. Service orientation to the most granular level, if I may say

Now that the concept of intent has been introduced, let me get down to a more formal definition of Intent.

Intent is a bundle of information, a passive data structure that holds an abstract description of the operation to be performed. (or in the case of broadcasts, a description of an event that has happened and is being announced).

There are 2 types of intents which I intend to detail in the next part of this series. Before winding up part 1, I would finally also give you a formal definition of Intent filters.

Intent filters are the means through which a component advertizes its own capabilities to handle specific job/operations to the android platform.

SAP Interview Faqs

Is Session Method, Asynchronous or Synchronous?AnsSynchronous
What are the different types of data dictionary objects?
Ans Different types of data dictionary objects:
1) Tables
2) Views
3) Data elements
4) Structure
5) Domains
6) Search Helps
7) Local Objects
8) Matchcode
How many types of tables exist and what are they in data dictionary?
Ans4 Types of Tables
What does an EXEC SQL stmt do in ABAP? What is the disadvantage of using it?
AnsTo use a Native SQL statement, you must precede it with the EXEC SQL statement, and follow it with the ENDEXEC statement as follows:
EXEC SQL PERFORMING form.

ENDEXEC.
There is no period after Native SQL statements. Furthermore, using inverted commas (”) or an asterisk (*) at the beginning of a line in a native SQL statement does not introduce a comment as it would in normal ABAP syntax. You need to know whether table and field names are case-sensitive in your chosen database.

SAP Technical Interview Question and Answers

How to define Selection Screen?
Ans:-Parameters, Select-options & Selection-Screen
What are Check Tables and Value Tables?

Ans:-Check Table: The ABAP Dictionary allows you to define relationships between tables using foreign keys . A dependent table is called a foreign key table, and the referenced table is called the check table. Each key field of the check table corresponds to a field in the foreign key table. These fields are called foreign key fields. One of the foreign key fields is designated as the check field for checking the validity of values. The key fields of the check table can serve as input help for the check field.
How to debugg a script?
AnsGo to SE71, give layout set name, go to utilities select debugger mode on.
How many maximum sessions can be open in SAPgui?

AnsThere are maximum 6 sessions open in SAPgui.
SAP Scripts and ABAP programs are client dependent or not? Why?

What are System Variable?

Ans:-System variables have been predefined by SAP. We can use these variables in formulas or, for example, to pass on certain pieces of information to a function module. How the function called by the function module behaves depends on the type of information passed on.

Bluetooth Interview question and answers

1. What is Piconet? -
A collection of devices connected through Bluetooth technology in an ad hoc fashion.
2. What are the three main types of connection used by Bluetooth? -
Single slave, Master slave, Scatternet .
3. What is FEC in Bluetooth? -
Forward Error Correction is a method by which Bluetooth increases its noise immunity. FEC is a method that enables a receiver to not only detect, but also correct errors in a transmission.
4. Which method is primarily used for Voice transfer? -
Synchronous Connection Oriented (SCO) is a method primarily used for Voice transfer.
5. Which Bluetooth version uses adaptive frequency hopping? Why? -
In Version 1.2 Adaptive frequency hopping method is used, which improves resistance to radio interference, and provides higher transmission speed.
6. How many SCO links are there in a piconet? -
In a piconet, there can be up to three SCO links of 64,000 bits per second each.
7. what is the total number of masters and slaves in a piconet? -
1 Master and 7 Slaves.
8. Which method is used for Data transfer? -
Asynchronous Connectionless (ACL) is Data transfer method in Bluetooth

Interview on Bluetooth technology

1. Why is Bluetooth called a cable replacement technology? -
Bluetooth technology allows the creation of Personal Area Networks without cables or wires that are usual in home networks.
2. Give a generic description of Bluetooth? -
Bluetooth is a low-cost, short-range (RF) links between mobile PCs, mobile phones and other portable devices. Bluetooth can transmit through solid, non-metal objects.
3. Why can Bluetooth equipment integrate easily in TCP/IP network? - Because Bluetooth uses wireless LAN standards IEEE for data transmission.
4. Is it possible to connect multiple Bluetooth hubs? -
No, only one hub can be used at a time with a computer. USB or Serial devices can be added.
 
Top Blogs