天天看点

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

继续阅读