Action指Intent要完成的动作,是一个字符串常量。在Intent类里定义了大量的Action常见属性,例如ACTION_CALL(打电话)、ACTION_EDIT(编辑数据)、ACTION_BATTERY_LOW(电量低广播Action),这些是系统内置Action属性。也可以自定义Action属性,本例演示自定义Action属性,下个例子演示系统内置Action属性。
1. 布局文件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>
2. 布局文件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>
3. 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; /** * * @author 郭宏志 * 测试Intent Action 属性 */ public class MainActivity extends Activity { // 定义Action 属性常量 public static final String MY_ACTION="com.amaker.ch06.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); } }); } }
4. MyActivity.java
package com.amaker.ch06.app; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.widget.TextView; /** * @author 郭宏志 * 测试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); } }
5. 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>
6. 运行结果
Chapter06_Intent_TestAction实例源码:
最新评论