天天看點

android元件通訊 Intent-Action屬性

Action屬性應用執行個體

1、自定義Action屬性

程式檔案

/Chapter06_Intent_TestAction/src/com/amaker/ch06/app/MainActivity.java

代碼  

package com.amaker.ch06.app;  

import android.app.Activity;  

import android.content.Intent;  

import android.os.Bundle;  

import android.view.View;  

import android.view.View.OnClickListener;  

import android.widget.Button;  

/**  

 *   

 * 測試Intent Action 屬性  

 */ 

public class MainActivity extends Activity {  

    // 定義Action 屬性常量  

    public static final String MY_ACTION="com.amaker.ch07.app.MY_ACTION";  

    // 聲明Button  

    private Button btn;  

    @Override 

    public void onCreate(Bundle savedInstanceState) {  

        super.onCreate(savedInstanceState);  

        // 設定布局視圖  

        setContentView(R.layout.main);  

        // 執行個體化Button  

        btn = (Button)findViewById(R.id.Button01);  

        btn.setOnClickListener(new OnClickListener() {  

            @Override 

            public void onClick(View v) {  

                 // 執行個體化Intent  

                Intent intent = new Intent();  

                // 為Intent設定Action屬性  

                intent.setAction(MY_ACTION);  

                // 啟動Activity  

                startActivity(intent);  

            }  

        });  

    }  

/Chapter06_Intent_TestAction/src/com/amaker/ch06/app/MyActivity.java

import android.widget.TextView;  

public class MyActivity extends Activity {  

    // 聲明TextView  

    private TextView tv;  

        // 設定視圖布局  

        setContentView(R.layout.my_layout);  

        // 獲得Intent對象  

        Intent intent = getIntent();  

        // 獲得Action  

        String action = intent.getAction();  

        // 獲得TextView  

        tv = (TextView)findViewById(R.id.TextView01);  

        // 設定内容  

        tv.setText(action);  

布局檔案

/Chapter06_Intent_TestAction/res/layout/main.xml

<?xml version="1.0" encoding="utf-8"?>  

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 

    android:orientation="vertical" 

    android:layout_width="fill_parent" 

    android:layout_height="fill_parent" 

    >  

    <Button   

        android:text="測試Intent的Action屬性"   

        android:id="@+id/Button01"   

        android:layout_width="wrap_content"   

        android:layout_height="wrap_content"></Button>  

</LinearLayout> 

/Chapter06_Intent_TestAction/res/layout/my_layout.xml

    <TextView   

        android:text="@+id/TextView01"   

        android:id="@+id/TextView01"   

        android:layout_height="wrap_content"></TextView>  

清單檔案

/Chapter06_Intent_TestAction/AndroidManifest.xml

<manifest xmlns:android="http://schemas.android.com/apk/res/android" 

      package="com.amaker.ch06.app" 

      android:versionCode="1" 

      android:versionName="1.0">  

    <application android:icon="@drawable/icon" android:label="@string/app_name">  

        <activity android:name=".MainActivity" 

                  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="MyActivity">  

                <action android:name="com.amaker.ch06.app.MY_ACTION" />  

                <category android:name="android.intent.category.DEFAULT" />  

    </application>  

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

</manifest> 

2、通路系統的Action屬性

/Chapter06_Intent_TestAction2/src/com/amaker/ch06/app/MainActivity.java

        // 設定目前布局視圖  

                // 建立Intent  

                // 設定Intent Action屬性  

                intent.setAction(Intent.ACTION_GET_CONTENT);  

                // 設定Intent Type 屬性  

                intent.setType("vnd.android.cursor.item/phone");  

/Chapter06_Intent_TestAction2/res/layout/main.xml

<?xml version="1.0" encoding="utf-8"?> 

    > 

        android:text="測試Intent的 Action屬性"   

        android:layout_height="wrap_content"></Button> 

/Chapter06_Intent_TestAction2/AndroidManifest.xml

      android:versionName="1.0"> 

    <application android:icon="@drawable/icon" android:label="@string/app_name"> 

                  android:label="@string/app_name"> 

            <intent-filter> 

                <action android:name="android.intent.action.MAIN" /> 

                <category android:name="android.intent.category.LAUNCHER" /> 

            </intent-filter> 

        </activity> 

    </application> 

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

本文轉自linzheng 51CTO部落格,原文連結:http://blog.51cto.com/linzheng/1080670

繼續閱讀