Advertisement
Tech

Using Intents in the Google Android Programming Environment. Part II

Let’s continue talking about Intents in Android, this time more complex examples will be shown.

By Jbeerdev
Desk Tech
Reading time 3 min read
Word count 496
Google android Mobile Android sdk
Using Intents in the Google Android Programming Environment. Part II
Advertisement
Quick Take

Let’s continue talking about Intents in Android, this time more complex examples will be shown.

On this page

Binding Intents II

Example B: This example will be almost the same as in our first “example A” article, with the main difference that we need to get information back from the new Activity opened. This example will be different: We are creating a shopping list, in an Activity we have the product list (product name, price, product code), and we open a new Activity with a form to introduce the new product, when we press “enter” we go back to the products list and a new product is added (the one we have added in the previous Activity).

After calling the new Activity (the form one, lets call it FormActiviy) we need to add a method in the main Activity (lets call this ProductActivity), this method will receive the data from the FormActivity and here we will do whatever we want with this info.

Advertisement

protected void onActivityResult(int requestCode, int resultCode,Intent data) {

}

Advertisement

Perhaps we also want to receive information from more than one Activity, that’s what we can gather here by checking the resultCode. The resultCode gives us information about which Activity has been opened and gives us the result.

To call the FormActivity we have to do it in this way.

Advertisement

Intent intent = new Intent(“com.bright.hub.package.FormActivity”);

startActivityForResult(intent, RESULT_CODE_CONSTANT);

Advertisement

We create an Intent calling to the Activity, in this case the FormActivity, and we start the new Activity with the startActivityForResult, with this we are saying that we are expecting a response from the FormActivity.

In the FormActivity, once we have accomplished the task it was made for, and we want to go back, we use the following lines:

Advertisement

Intent i = new Intent();

i.putExtra(“NAME”,object.name);

Advertisement

I.putExtra(“KIND”,object.kind);

Advertisement

setResult(RESULT_CODE_CONSTANT, i);

We create a new intent, we add to this intent the information we want to return back to the ProductActivity, and then we call to the setResult method adding the RESULT_CODE_CONSTANT that we are going to receive in the ProductActivity.

Advertisement

This way in the onActivityResult we can obtain the info using the “data” intent from this method.

Predefined Intents

Let’s now see the Intents that Android gives us as “pre-defined”. With these intents we can perform standard operations, like “call to” or “send a sms to”.

Advertisement

To start a search on the Internet:

Intent intent = new Intent(Intent.ACTION_WEB_SEARCH );

Advertisement

intent.putExtra(SearchManager.QUERY, “Bright Hub Android”);

startActivity(intent);

Advertisement

To send an SMS to someone

Intent sendIntent = new Intent(Intent.ACTION_SENDTO, Uri.parse(“sms://”));

Advertisement

sendIntent.putExtra(“address”, “123456789”);

sendIntent.putExtra(“sms_body”, “Something to say”);

Advertisement

startActivity(sendIntent);

Dial some number

Intent dialIntent = new Intent(Intent.ACTION_DIAL, Uri.parse(“tel:666444666”));

startActivity(dialIntent);

More information about pre-defined intents can be found in the Google Android Reference page .

This post is part of the series: How-to develop Google Android Applications - Going further

Here more functionality and simple how-to guides to show you how to work with Google Android

  1. Dev Guide to Translating Apps in Android
  2. Guide to Intents in Android Development
  3. Guide to Intents for Android Developers. Part II
  4. How to Program the Google Android Camera to Take Pictures
  5. Android UI Java Programming Tutorial
Keep Exploring

More from Tech

Filed under
Google android Mobile
More topics
Android sdk
Advertisement