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

代码

package com.amaker.ch06.app;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;
/**
* 测试Intent Action 属性
*/
public class MyActivity extends Activity {
// 声明TextView
private TextView tv;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// 设置视图布局
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

代码

<?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"
>

<TextView
android:text="@+id/TextView01"
android:id="@+id/TextView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"></TextView>

</LinearLayout>

清单文件

/Chapter06_Intent_TestAction/AndroidManifest.xml

代码

<?xml version="1.0" encoding="utf-8"?>
<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">
<intent-filter>
<action android:name="com.amaker.ch06.app.MY_ACTION" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>

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

</manifest>

2、访问系统的Action属性

程序文件

/Chapter06_Intent_TestAction2/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 {
// 声明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(Intent.ACTION_GET_CONTENT);
// 设置Intent Type 属性
intent.setType("vnd.android.cursor.item/phone");
// 启动Activity
startActivity(intent);
}
});
}
}

布局文件

/Chapter06_Intent_TestAction2/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_TestAction2/AndroidManifest.xml

代码

<?xml version="1.0" encoding="utf-8"?>
<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>

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

</manifest>

转载于:https://www.cnblogs.com/linzheng/archive/2011/01/19/1939727.html

android组件通讯 Intent-Action属性相关推荐

  1. android组件通讯 Intent- 系统标准的Activity Action应用

    标准的Activity Actions ACTION_M AIN 作为一个主要的进入口,而并不期望去接受数据 ACTION_VIEW 向用户去显示数据 ACTION_ATTACH_DATA 别用于指定 ...

  2. Android开发之Intent.Action

    1 Intent.ACTION_MAIN String: android.intent.action.MAIN 标识Activity为一个程序的开始.比较常用. Input:nothing Outpu ...

  3. jsf tree组件_JSF和“立即”属性–命令组件

    jsf tree组件 JSF中的即时属性通常被误解. 如果您不相信我,请查看Stack Overflow . 造成混淆的部分原因可能是由于输入(即<h:inputText />)和命令(即 ...

  4. html中的form action属性,HTML form action 属性

    当提交表单时,发送表单数据到名为 "demo_form.html" 的文件(处理输入): First name: Last name: 菜鸟教程(runoob.com) First ...

  5. Android组件的通讯-Intent

    1.概述 一个应用程序的三个核心组件--activities.services.broadcast receivers,都是通过叫做intents的消息激活.Intent消息是一种同一或不同应用程序中 ...

  6. Android开发笔记(四十)组件通讯工具Intent

    Intent的用途与组成 Intent用于处理Android各组件之间的通讯.Intent完成的工作主要有三部分: 1.Intent需标明本次通讯请求是从哪里来,到哪里去,要怎么走: 2.发起方携带上 ...

  7. 3.1 Android组件intent filter

    3.1.0 Intent filter基本概念 前面章节我们说到了Intent,Intent对象中除了ComponentName可以直接指定目标组件外,其它的属性都无法直接指定目标组件.当然我们这里不 ...

  8. Android应用开发—Intent组件详解

    转载自:Android中Intent组件详解 Intent是不同组件之间相互通讯的纽带,封装了不同组件之间通讯的条件. Intent本身是定义为一个类别(Class),一个Intent对象表达一个目的 ...

  9. 3.0 Android组件之间的信使Intent

    应用的三个核心组件:activity,service 和broadcast receiver 都是通过intent来触发的.Intent直译为"意图.意向".通常也可以理解为不同组 ...

最新文章

  1. c枚举类型enum例题_C语言--enum,typedef enum 枚举类型详解
  2. 北大林宙辰:追求机器学习研究的美感
  3. hadoop面试记录(一)
  4. Vmware ESX server CPU掩码导致的挂起
  5. python数据库优化_Python学习(二十九)—— pymysql操作数据库优化
  6. 吸尘器电机拆解图解_老少皆宜居家清理更轻松?吉米A6上手把无线吸尘器体验...
  7. 统计HDFS文件数量,大小,以及在某范围大小的文件数量
  8. LintCode 158: Anagram
  9. 基于JAVA+SpringMVC+MYSQL的家政服务平台
  10. vue-cli代理开发
  11. Zookeeper的一些概念
  12. 在PC上使用苹果蓝牙无线键盘
  13. 核磁共振重建算法综述
  14. 什么是软件的生命周期?
  15. 【审稿意见】科研菜鸟如何攥写审稿意见?万能模板!!!
  16. Calcite优化规则之ProjectAggregateMergeRule
  17. 家禽也能有身份,XFS在畜牧业落地
  18. 用Java语言实现余弦定理和修正余弦
  19. 使用python玩阴阳师,自动抽到SSR
  20. 微信小程序--自定义组件(超详细 从新建到使用)

热门文章

  1. 操作系统(二十)进程互斥的硬件实现方法
  2. Android Framework系统服务详解
  3. Android APP终极瘦身指南
  4. mach-o格式分析
  5. xp系统粘贴是灰色的_全方位升级 好听更好看!——乂度XP-2 Pro蓝牙独立解码耳放体验...
  6. jupyter lab文档位置默认system_九大神招,让Python里数据分析神器Jupyter,完美升华...
  7. JZOJ 5489. 【清华集训2017模拟11.28】海明距离
  8. mysql php页面流量统计_PHP学习笔记:php网络流量统计系统
  9. 一个表格中文字怎么换行_Excel表格中怎样快速将阿拉伯数字转化为大写文字?这样操作一键完成...
  10. c语言入门自学书籍推荐6,菜鸟自学嵌入式之C语言基础No6 算法分析