使用Intent 和IntentFilter进行通信

一、Component属性

为Intent中的一个属性。

ComponentName comp = new ComponentName(ComponentAttr.this
					, SecondActivity.class);
				Intent intent = new Intent();
				//为Intent设置Component属性
				intent.setComponent(comp);
//				Intent intent = new Intent(ComponentAttr.this
//					, SecondActivity.class);
				startActivity(intent);

 //获取该Activity对应的Intent的Component属性

		ComponentName comp = getIntent().getComponent();
		//显示该ComponentName对象的包名、类名
		show.setText("组件包名为:" + comp.getPackageName()
			+ "\n组件类名为:" + comp.getClassName());

 二、Action ,Category,intent-filter配置

有点像struct的Action 页面分离,通过xml配置

				Intent intent = new Intent();
				//为Intent设置Action属性(属性值就是一个普通字符串)
				intent.setAction(ActionAttr.CRAZYIT_ACTION);
				startActivity(intent);

 <activity android:name=".SecondActivity"

				  android:label="@string/app_name">
			<intent-filter>
				<!-- 指定该Activity能响应Action为指定字符串的Intent -->
				<action android:name="org.crazyit.intent.action.CRAZYIT_ACTION" />
				<!-- 指定该Action能响应Action属性为helloWorld的Intent -->
				<action android:name="helloWorld" />
				<!-- 指定该Action能响应Category属性为指定字符串的Intent -->
				<category android:name="android.intent.category.DEFAULT" />   
			</intent-filter>
		</activity>
<activity android:name=".SecondActivity"
				  android:label="@string/app_name">
			<intent-filter>
				<!-- 指定该Activity能响应Action为指定字符串的Intent -->
				<action android:name="org.crazyit.intent.action.CRAZYIT_ACTION" />
				<!-- 指定该Action能响应Action属性为helloWorld的Intent -->
				<action android:name="helloWorld" />
				<!-- 指定该Action能响应Category属性为指定字符串的Intent -->
				<category android:name="android.intent.category.DEFAULT" />   
			</intent-filter>
		</activity>

 获得上一个的action

String action = getIntent().getAction();
		//显示Action属性
show.setText("Action为:" + action);//值为org.crazyit.intent.action.CRAZYIT_ACTION

 category的用法 运行指定的action和包含此category的Activity

				Intent intent = new Intent();
				//设置Action属性
				intent.setAction(ActionCateAttr.CRAZYIT_ACTION);
				//添加Category属性
				  intent.addCategory(ActionCateAttr.CRAZYIT_CATEGORY);
				startActivity(intent);

  三、指定Action 、Category调用系统的Activity

Intent 和Category含有大量的常量

使用系统的常量action即可调用系统的Activity  获取联系人,注意需要配置访问联系人的权限

package org.crazyit.action;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

/**
 * Description: <br/>
 * site: <a href="http://www.crazyit.org">crazyit.org</a> <br/>
 * Copyright (C), 2001-2012, Yeeku.H.Lee <br/>
 * This program is protected by copyright laws. <br/>
 * Program Name: <br/>
 * Date:
 * 
 * @author Yeeku.H.Lee [email protected]
 * @version 1.0
 */
public class SysAction extends Activity
{
	final int PICK_CONTACT = 0;

	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		Button bn = (Button) findViewById(R.id.bn);
		// 为bn按钮绑定事件监听器
		bn.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View arg0)
			{
				// 创建Intent
				Intent intent = new Intent();
				//设置Intent的Action属性
				intent.setAction(Intent.ACTION_GET_CONTENT);
				//设置Intent的Type属性
				intent.setType("vnd.android.cursor.item/phone");
				// 启动Activity,并希望获取该Activity的结果
				startActivityForResult(intent, PICK_CONTACT);
			}
		});
	}
	@Override
	public void onActivityResult(int requestCode, int resultCode, Intent data)
	{
		super.onActivityResult(requestCode, resultCode, data);
		switch (requestCode)
		{
			case (PICK_CONTACT):
				if (resultCode == Activity.RESULT_OK)
				{
					// 获取返回的数据
					Uri contactData = data.getData();
					// 查询联系人信息
					Cursor cursor = managedQuery(contactData, null
						, null, null, null);
					// 如果查询到指定的联系人
					if (cursor.moveToFirst())
					{
						String contactId = cursor.getString(cursor
							.getColumnIndex(ContactsContract.Contacts._ID));
						// 获取联系人的名字
						String name = cursor.getString(cursor
							.getColumnIndexOrThrow(
								ContactsContract.Contacts.DISPLAY_NAME));
						String phoneNumber = "此联系人暂未输入电话号码";
						System.out.println("-------------" + contactId);
						//根据联系人查询该联系人的详细信息
						Cursor phones = getContentResolver().query(
							ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
							null,
							ContactsContract.CommonDataKinds.Phone.CONTACT_ID
								+ " = " + contactId, null, null);
						System.out.println("===================" + phones);
						if (phones.moveToFirst())
						{
							//取出电话号码
							phoneNumber = phones
								.getString(phones
									.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
						}
						// 关闭游标
						phones.close();
						EditText show = (EditText) findViewById(R.id.show);
						//显示联系人的名称
						show.setText(name);
						EditText phone = (EditText) findViewById(R.id.phone);
						//显示联系人的电话号码
						phone.setText(phoneNumber);
					}
					// 关闭游标
					cursor.close();
				}
				break;
		}
	}
}

1、 返回桌面

				//创建Intent对象
				Intent intent = new Intent();
				//为Intent设置Action、Category属性
				intent.setAction(Intent.ACTION_MAIN);
				intent.addCategory(Intent.CATEGORY_HOME);

 2、Data的用法

Data分为二个部分 类型+数据

package org.crazyit.intent;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

/**
 * Description:
 * <br/>site: <a href="http://www.crazyit.org">crazyit.org</a> 
 * <br/>Copyright (C), 2001-2012, Yeeku.H.Lee
 * <br/>This program is protected by copyright laws.
 * <br/>Program Name:
 * <br/>Date:
 * @author  Yeeku.H.Lee [email protected]
 * @version  1.0
 */
public class DataAttr extends Activity
{
	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		Button bn = (Button)findViewById(R.id.bn);
		//为bn按钮添加一个监听器
		bn.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				//创建Intent
				Intent intent = new Intent();
				String data = "http://www.crazyit.org";
				//根据指定字符串解析出Uri对象
				Uri uri = Uri.parse(data);
				//为Intent设置Action属性
				intent.setAction(Intent.ACTION_VIEW);
				//设置Data属性
				intent.setData(uri);
				startActivity(intent);
			}
		});
		
		Button edit = (Button)findViewById(R.id.edit);
		//为edit按钮添加一个监听器
		edit.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				//创建Intent
				Intent intent = new Intent();
				//为Intent设置Action属性(动作为:编辑)
				intent.setAction(Intent.ACTION_EDIT);
				String data = "content://com.android.contacts/contacts/1";
				//根据指定字符串解析出Uri对象
				Uri uri = Uri.parse(data);				
				//设置Data属性
				intent.setData(uri);
				startActivity(intent);
			}
		});	
		Button call = (Button)findViewById(R.id.call);
		//为edit按钮添加一个监听器
		call.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				//创建Intent
				Intent intent = new Intent();
				//为Intent设置Action属性(动作为:拨号)
				intent.setAction(Intent.ACTION_DIAL);
				String data = "tel:13800138000";
				//根据指定字符串解析出Uri对象
				Uri uri = Uri.parse(data);				
				//设置Data属性
				intent.setData(uri);
				startActivity(intent);
			}
		});			
	}
}

 3、Extra属性

多个action之间进行数据交换,Bundle  像一个Map。。

猜你喜欢

转载自zhangfy068.iteye.com/blog/1731420