Wednesday, 6 May 2015

Lesson 10 Using Android Intents Tutorial ---- 3

Using Android Intents Tutorial

The Action You Know

So far we have been opening activities with Intents using the Activity name. Intents, however, are very powerful in that they can determine what activity should be run based on the criteria you provide. If there is a case where more than one activity can use that type of content, Android with ask the user which app they would prefer to use. I’m going to create a new class that gives some examples of common actions.
<?xml version="1.0" encoding="utf-8"?>
<TableLayout 
 android:layout_width="fill_parent" 
 android:layout_height="fill_parent" 
 xmlns:android="http://schemas.android.com/apk/res/android">
 <TableRow>
  <TextView
      android:text="Latitude"
      android:layout_width="150dp"
      android:layout_height="wrap_content"
      android:layout_column="0" />
  <TextView
      android:text="Longitude"
      android:layout_width="150dp"
      android:layout_height="wrap_content"
      android:layout_column="1" />      
 </TableRow>
 <TableRow>
     <EditText
      android:id="@+id/txtLatitude"
      android:width="100px"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="35.696471" />
      <EditText
      android:id="@+id/txtLongitude"
      android:width="100px"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="139.570472" /> 
 </TableRow>
 <TableRow>
  <Button 
      android:id="@+id/btnShowLocation"
      android:text="Show Location"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content" />
 </TableRow>
 <TableRow>
     <TextView      
      android:width="100px"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="Phone Number" />
      <EditText
      android:id="@+id/txtPhoneNumber"
      android:width="100px"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:inputType="phone"
      android:text="555-1212" /> 
 </TableRow>
 <TableRow>
  <Button 
      android:id="@+id/btnDial"
      android:text="Dial Number"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content" />
  <Button 
      android:id="@+id/btnCall"
      android:text="Call Number"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content" />
 </TableRow>
 <TableRow>
  <Button 
      android:id="@+id/btnContact"
      android:text="Pick Contact"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content" />
 </TableRow>
</TableLayout>
package com.learnandroid.intents;
 
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
 
public class UriActivity extends Activity {
 
 private EditText txtLatitude;
 private EditText txtLongitude;
 private EditText txtPhoneNumber;
 
 @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.uri_layout);
 
        //Show Location Example
        txtLatitude = (EditText) findViewById(R.id.txtLatitude);
        txtLongitude = (EditText) findViewById(R.id.txtLongitude);
        Button btnShow = (Button) findViewById(R.id.btnShowLocation);
        btnShow.setOnClickListener(new Button.OnClickListener() {
 
        @Override
  public void onClick(View v) {
   Uri location = Uri.parse("http://maps.google.com/maps?q=" 
     + txtLatitude.getText().toString() 
     + "," 
     + txtLongitude.getText().toString());
   startActivity(new Intent(Intent.ACTION_VIEW, location));
  } });
 
        //Dial Phone Example
        txtPhoneNumber = (EditText) findViewById(R.id.txtPhoneNumber);
        Button btnDial = (Button) findViewById(R.id.btnDial);
        btnDial.setOnClickListener(new Button.OnClickListener() {
 
        @Override
  public void onClick(View v) {    
   Uri phoneNum = Uri.parse("tel:" + txtPhoneNumber.getText().toString());
   startActivity(new Intent(Intent.ACTION_DIAL, phoneNum));
  } });
 
        //Make Call Example
        Button btnCall = (Button) findViewById(R.id.btnCall);
        btnCall.setOnClickListener(new Button.OnClickListener() {
 
        @Override
  public void onClick(View v) {    
   Uri phoneNum = Uri.parse("tel:" + txtPhoneNumber.getText().toString());
   startActivity(new Intent(Intent.ACTION_CALL, phoneNum));
  } });
 
        //Select from Contacts Example
        Button btnContact = (Button) findViewById(R.id.btnContact);
        btnContact.setOnClickListener(new Button.OnClickListener() {
 
        @Override
  public void onClick(View v) {    
   Uri content = Uri.parse("content://contacts/people");
   startActivity(new Intent(Intent.ACTION_PICK, content));
  } });
    }
}
We have a couple of EditText widgets that allow the user to enter in Latitude and Longitude (I went ahead a pre-populated them with the location of the Studio Ghibli museum in Japan). We have a button that when clicked shows the location on the map. We pass a URI to the startActivity method, instead of a class name, and Android uses the most appropriate app. We could have also passed in a URI like this, which would have launched the Google Maps app
Uri location = Uri.parse("geo:" + txtLatitude.getText().toString() + "," + txtLongitude.getText().toString());
The only reason I didn’t do it this way is I don’t have the Maps app on my emulator and so I didn’t want to assume you did either.
The next part of this page is another EditText widget where the user can enter a phone number, along with a button that will dial the number. You can see we used the ACTION_DIAL action this time, and passed in a tel: format URI. This will bring up the dialer with the number pre-populated. The next button demonstrates using ACTION_CALL in order to just place the call. Remember, though, when the user installs you app they will have to grant your app permission to make calls.
Finally, we show the action that allows us to have the user pick a person from their contacts. ACTION_PICK can be used for any collection, not just the contact list.

No comments:

Post a Comment