วันพฤหัสบดีที่ 28 มิถุนายน พ.ศ. 2555

intent Android example

--> Intent Android Example

 Package Explorer

Intent Android Activity.java

package android.example.intent;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class IntentAndroidActivity extends Activity {
   
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
       
        final Button btn1 = (Button)findViewById(R.id.button1);      
        btn1.setOnClickListener(new View.OnClickListener() {
           
            public void onClick(View v) {                  
                  startActivity(new Intent("android.example.intent.activity2"));
                  }
            });
       
        // Calling Built-In Applications Using Intents
       
        final Button btn2 = (Button)findViewById(R.id.button2);      
        btn2.setOnClickListener(new View.OnClickListener() {                 
            public void onClick(View v) {
                 
                  Intent i = new Intent(android.content.Intent.ACTION_VIEW,                          
                                          Uri.parse("http://www.google.com"));                 
                  startActivity(i);
                  }
        });
       
        final Button btn3 = (Button)findViewById(R.id.button3);      
        btn3.setOnClickListener(new View.OnClickListener() {                 
            public void onClick(View v) {
                 
                  Intent i = new Intent(android.content.Intent.ACTION_DIAL,
                              Uri.parse("tel:+651234567"));                  
                  startActivity(i);
                  }
        });
    }
}

Activity2.java

package android.example.intent;

import android.app.Activity;
import android.os.Bundle;

public class activity2 extends Activity {
     
     
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity2);
                                      
    }// onCreate
      
}

 main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@drawable/bg"
    android:orientation="vertical" >

    <TextView
        android:layout_width="224dp"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:text="Android Intent"
        android:textColor="#FF00FF"
        android:textSize="30dp" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="50dp"
        android:layout_marginTop="20dp"
        android:textSize="26dp"
        android:text="Intent to Activity2" />

<Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="50dp"
        android:layout_marginTop="20dp"
        android:textSize="26dp"
        android:text="Intent.ACTION_VIEW" />

<Button
        android:id="@+id/button3"
        android:layout_width="wrap_content"
        android:layout_height="50dp"
        android:layout_marginTop="20dp"
        android:textSize="26dp"
        android:text="Intent.ACTION_DIAL" />


 <TextView
     android:id="@+id/textView1"
     android:layout_width="match_parent"
     android:layout_height="match_parent"
     android:layout_marginLeft="20dp"
     android:layout_marginTop="180dp"
     android:text="main.xml"
     android:textAppearance="?android:attr/textAppearanceMedium"
     android:textColor="#FF0000"
     android:textSize="26dp" />

</LinearLayout>


 activity2.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#FFBB33"
    android:orientation="vertical" >

    <ScrollView
        android:id="@+id/scrollView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <LinearLayout
            android:id="@+id/linearLayout2"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical" >

            <TextView
                android:id="@+id/textView1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_horizontal"
                android:text="Activity 2"
                android:textColor="#FF00FF"
                android:textSize="30dp" />

            <Button
                android:id="@+id/button1"
                android:layout_width="66dp"
                android:layout_height="38dp"
                android:background="#00FF00"
                android:text="Button 1" />

            <LinearLayout
                android:id="@+id/linearLayout1"
                android:layout_width="match_parent"
                android:layout_height="match_parent" >

                <Button
                    android:id="@+id/button3"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="2"
                    android:background="@drawable/ic_launcher"
                    android:text="Button" />

                <Button
                    android:id="@+id/button4"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="2"
                    android:text="Button" />

                <Button
                    android:id="@+id/button5"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="2"
                    android:text="Button" />

                <Button
                    android:id="@+id/button6"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="2"
                    android:text="Button" />

                <Button
                    android:id="@+id/button7"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="2"
                    android:text="Button" />
            </LinearLayout>
        </LinearLayout>
    </ScrollView>


    <TextView
        android:id="@+id/textView2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="20dp"
        android:layout_marginTop="300dp"
        android:text="activity2.xml"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:textColor="#FF0000" />

</LinearLayout>

AndroidMainifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="android.example.intent"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="8" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:name=".IntentAndroidActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
       
        <activity
            android:name=".activity2"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.example.intent.activity2" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
       
    </application>

</manifest>


Calling Built-In Applications Using Intents

 
 
In the first 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);

Intent i = new Intent(android.content.Intent.ACTION_DIAL,
Uri.parse(tel:+651234567)); 
startActivity(i);

Understanding the Intent Object
You can also create an Intent object by passing in an action constant and data, such as the following:

Intent i = new Intent(android.content.Intent.ACTION_VIEW,
Uri.parse(http://www.amazon.com));
startActivity(i);

The action portion defines what you want to do, while the data portion contains the data for the target
activity to act upon. You can also pass the data to the Intent object using the setData() method:

Intent i = new Intent(android.content.Intent.ACTION_VIEW); 
i.setData(Uri.parse(http://www.amazon.com));

For some intents, there is no need to specify the data. For example, to select a contact from the Contacts
application, you specify the action and then indicate the MIME type using the setType() method:

Intent i = new Intent(android.content.Intent.ACTION_PICK);
i.setType(ContactsContract.Contacts.CONTENT_TYPE);

To summarize, an Intent object can contain the following information:
➤➤ Action
➤➤ Data
➤➤ Type


Android Control ควบคุมอุปกรณ์ต่างๆ ด้วย Android

สอนเขียน Android  สอนเขียนโปรแกรม Android
-->