Sunday, October 6, 2013

Android-Calling Built-In Applications Using Intents

Calling Built-In Applications Using Intents

Until this point, you have seen how to call activities within your own application. One of the key aspects of Android programming is using the intent to call activities from other applications. In particular, your application can call the many built-in applications that are included with an Android device. For example, if your application needs to enable a user to call a particular person saved in the Contacts application, you can simply use an Intent object to bring up the Contacts application, from which the user can select the person to call. This enables your application to present a consistent user experience, and enables you to avoid building another application to retrieve all the contacts in the Contacts application.
The following Try It Out demonstrates how to call some of the built-in applications commonly found on an Android device.

Calling Built-In Applications Using Intents

1. Using Eclipse, create a new Android project and name it Intents.
2. Add the following statements in bold to the main.xml file:

android:orientation=”vertical”
android:layout_width=”fill_parent”
android:layout_height=”fill_parent” >

Calling Built-In Applications Using Intents

3. Add the following statements in bold to the MainActivity.java file:
package net.learn2develop.Intents;
import android.app.Activity;
import android.os.Bundle;
import android.content.Intent;
import android.net.Uri;
import android.provider.ContactsContract;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends Activity {
Button b1, b2, b3, b4;
int request_Code = 1;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//---Web browser button---
b1 = (Button) findViewById(R.id.btn_webbrowser);
b1.setOnClickListener(new OnClickListener()
{
public void onClick(View arg0){
Intent i = new
Intent(android.content.Intent.ACTION_VIEW,
Uri.parse(“http://www.amazon.com”));
startActivity(i);
}
});
//---Make calls button---
b2 = (Button) findViewById(R.id.btn_makecalls);
b2.setOnClickListener(new OnClickListener()
{
public void onClick(View arg0){
Intent i = new
Intent(android.content.Intent.ACTION_DIAL,
Uri.parse(“tel:+651234567”));
startActivity(i);
}
});
//---Show Map button---
b3 = (Button) findViewById(R.id.btn_showMap);
b3.setOnClickListener(new OnClickListener()
{
public void onClick(View arg0){
Intent i = new
Intent(android.content.Intent.ACTION_VIEW,
Uri.parse(“geo:37.827500,-122.481670”));
startActivity(i);
}
});
//---Choose Contact button---
b4 = (Button) findViewById(R.id.btn_chooseContact);
b4.setOnClickListener(new OnClickListener()
{
public void onClick(View arg0){
Intent i = new
Intent(android.content.Intent.ACTION_PICK);
i.setType(ContactsContract.Contacts.CONTENT_TYPE);
startActivityForResult(i,request_Code);
}
});
}
public void onActivityResult(int requestCode, int resultCode, Intent data)
{
if (requestCode == request_Code)
{
if (resultCode == RESULT_OK)
{
Toast.makeText(this,data.getData().toString(),
Toast.LENGTH_SHORT).show();
Intent i = new Intent(
android.content.Intent.ACTION_VIEW,
Uri.parse(data.getData().toString()));
startActivity(i);
}
}
}
}

4. Press F11 to debug the application on the Android Emulator.
5 . Click the Web Browser button to load the Browser application on the emulator (see Figure)


6. Click the Make Calls button and the Phone application will load.
7 . Similarly, to load the Maps application, shown in Figure click the Show Map button.
8. Click the Choose Contact application to show a list of contacts that you can select (see Figure). Selecting a contact will show details about that contact.

Calling Built-In Applications Using Intents



Calling Built-In Applications Using Intents

How It Works
In this example, you saw how you can use the Intent class to invoke some of the built-in applications in Android (such as Maps, Phone, Contacts, and Browser).
In Android, intents usually come in pairs: action and data. The action describes what is to be performed, such as editing an item, viewing the content of an item, and so on. The data specifies what is affected, such as a person in the Contacts database. The data is specified as an Uri object.
Some examples of action are as follows:
➤ACTION_VIEW
➤ ACTION_DIAL
➤ ACTION_PICK
Some examples of data include the following:
➤ http://www.google.com
➤ tel:+651234567
➤ geo:37.827500,-122.481670
➤ content://contacts
Collectively, the action and data pair describes the operation to be performed. For example, to dial a phone number, you would use the pair ACTION_DIAL/tel:+651234567. To display a list of contacts stored in your phone, you use the pair ACTION_VIEW/content://contacts. To pick a contact from the list of contacts, you use the pair ACTION_PICK/content://contacts.
In the fi rst button, you create an Intent object and then pass two arguments to its constructor — the action and the data:
Intent i = new Intent(android.content.Intent.ACTION_VIEW,
Uri.parse(“http://www.amazon.com”));
startActivity(i);

The action here is represented by the android.content.Intent.ACTION_VIEW constant. You use the parse() method of the Uri class to convert an URL string into an Uri object.
The android.content.Intent.ACTION_VIEW constant actually refers to the “android.intent.action.VIEW” action, so the preceding could be rewritten as follows:
Intent i = new Intent(“android.intent.action.VIEW”,
Uri.parse(“http://www.amazon.com”));
startActivity(i);

The preceding code snippet can also be rewritten like this:
Intent i = new Intent(“android.intent.action.VIEW”);
i.setData(Uri.parse(“http://www.amazon.com”));
startActivity(i);

Here, you set the data separately using the setData() method.
For the second button, you dial a specific number by passing in the telephone number in the data portion:
Intent i = new Intent(“android.intent.action.VIEW”);
i.setData(Uri.parse(“http://www.amazon.com”));
startActivity(i);

Calling Built-In Applications Using Intents

In this case, the dialer will display the number to be called. The user must still press the dial button to dial
the number. If you want to directly call the number without user intervention, change the action as follows:
Intent i = new Intent(android.content.Intent.ACTION_CALL,
Uri.parse(“tel:+651234567”));
startActivity(i);

If you simply want to display the dialer without specifying any number, simply omit the data portion, like this:
Intent i = new Intent(android.content.Intent.ACTION_DIAL);
startActivity(i);
The third button displays a map using the ACTION_VIEW constant:
Here, instead of using “http” you use the “geo” scheme.
Intent i = new Intent(android.content.Intent.ACTION_VIEW,
Uri.parse(“geo:37.827500,-122.481670”));
startActivity(i);

The fourth button invokes the Contacts application to enable the user to pick a contact. Because you are asking the user to select a contact, you need the Contacts application to return a value; in this case, you need to set the type of data to indicate what kind of data needs to be returned:
Intent i = new   Intent(android.content.Intent.ACTION_PICK);
i.setType(ContactsContract.Contacts.CONTENT_TYPE);
startActivityForResult(i,request_Code);

If you want to view and select only those contacts with a phone number, you could set the type as follows:
i.setType( ContactsContract.CommonDataKinds.Phone.CONTENT_TYPE);
In this case, the contacts and their phone numbers are displayed (see Figure).

Calling Built-In Applications Using Intents


Because you are expecting a result from the Contacts application, you invoke it using the startActivityForResult() method, passing in the Intent object and a request code. You need to implement the onActivityResult() method in order to obtain a result from the activity:
public void onActivityResult(int requestCode, int resultCode, Intent data)
{
if (requestCode == request_Code)
{
if (resultCode == RESULT_OK)
{
Toast.makeText(this,data.getData().toString(),Toast.LENGTH_SHORT).show();
Intent i = new Intent(android.content.Intent.ACTION_VIEW,
Uri.parse(data.getData().toString()));
startActivity(i);
}
}
}

In the case of the Contacts application, when you choose a particular contact (using the ACTION_PICK constant), an URL containing the contact selected is returned, like this:
content://com.android.contacts/contacts/loopup/0r1-1234567890/1
Obtaining this URL is not very useful unless you know what to do with it. Therefore, in this case, you can create another Intent object to view it:
Intent i = new Intent( android.content.Intent.ACTION_VIEW,
Uri.parse(data.getData().toString()));
startActivity(i);
This will show details about the selected contact.


Upload UIImage as base64 String

Upload UIImage as Base64 String (Upload UIImage as string) //Select Pic From Camera or Gallery       @objc func btnPro...