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

代码
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>

 

posted on 2011-01-19 23:42  linzheng  阅读(1396)  评论(0编辑  收藏  举报

导航