您的位置:首页 > 移动开发 > Android开发

精通Android3笔记--第五章

2011-11-25 17:16 330 查看
1、被调用的Activity能够发现调用它的Intent:

Intent intent = this.getIntent();

2、http://developer.android.com/guide/appendix/g-appintents.html记录了可用的Google应用程序和调用它们的Intent

3、当Intent带有组件名称时(ComponentName)时,它称为显式Intent,当其没有组件名称时,但依赖于其它部分(action,data)时,其为隐式Intent。有组件名称的Intent会忽略其它所有部分。

4、Intent的数据部分(data)并不是真正的数据,而是指向数据的指针。

5、Android查看URI的scheme,然后询问所有已注册的活动,看看哪个活动能理解此scheme。

<activity…...>

<intent-filter>

<action android:name="android.intent.action.VIEW" />

<data android:scheme="http"/>

<data android:scheme="https"/>

</intent-filter>

</activity>

6、Intent过滤器节点data的xml子节点的子元素包括:

host

mimeType

path

pathPattern

pathPrefix

port

scheme

7、Intent包括名为extra的附加特性,可以像收到Intent的组件提供更多信息。

//Get the Bundle from an Intent

Bundle extraBundle = intent.getExtras();

// Place a bundle in an intent

Bundle anotherBundle = new Bundle();

//populate the bundle with key/value pairs

...

//set the bundle on the Intent

intent.putExtras(anotherBundle);

putExtras方法会检查Intent中是否已经有Bundle,如果已经有了则把新的Bundle的键值附加到旧的Bundle上。

有许多方法像Bundle中添加数据:

putExtra(String name, boolean value);

putExtra(String name, int value);

putExtra(String name, double value);

putExtra(String name, String value);

And here are some not-so-simple extras:

//simple array support

putExtra(String name, int[] values);

putExtra(String name, float[] values);

//Serializable objects

putExtra(String name, Serializable value);

//Parcelable support

putExtra(String name, Parcelable value);

//Add another bundle at a given key

//Bundles in bundles

putExtra(String name, Bundle value);

//Add bundles from another intent

//copy of bundles

putExtra(String name, Intent anotherIntent);

//Explicit Array List support

putIntegerArrayListExtra(String name, ArrayList arrayList);

putParcelableArrayListExtra(String name, ArrayList arrayList);

putStringArrayListExtra(String name, ArrayList arrayList);

8、显示调用Intent

setComponent(ComponentName name);

setClassName(String packageName, String classNameInThatPackage);

setClassName(Context context, String classNameInThatContext);

setClass(Context context, Class classObjectInThatContext);

后三种方法实际最终都是调用setComponent方法。ComponentName 将一个包名和类名包装在一起。

new ComponentName("com.android.contacts","com.android.contacts.DialContactsEntryActivity“)

9、Category:

CATEGORY_DEFAULT: An activity can declare itself as a DEFAULT activity if it wants to be invoked by implicit intents. If you don’t define this category for your activity, that activity will need to be invoked explicitly every time through its class name. This is why you see activities that get invoked through generic actions or other action names use default category specification.

CATEGORY_BROWSABLE :An activity can declare itself as BROWSABLE by promising the browser that it will not violate browser security considerations when started.
CATEGORY_TAB: An activity of this type is embeddable in a tabbed parent activity.
CATEGORY_ALTERNATIVE: An activity can declare itself as an ALTERNATIVE activity for a certain type of data that you are viewing. These items normally show up as part of the options menu when you are looking at that document. For example, print view is considered an alternative to regular view.
CATEGORY_SELECTED_ALTERNATIVE:An activity can declare itself as an ALTERNATIVE activity for a certain type of data. This is similar to listing a series of possible editors for a text document or an HTML document.
CATEGORY_LAUNCHER: Assigning this category to an activity will allow it to be listed on the launcher screen.
CATEGORY_HOME: An activity of this type will be the home screen. Typically, there should be only one activity of this type. If there are more, the system will provide a prompt to pick one.
CATEGORY_PREFERENCE :This activity identifies an activity as a preference activity, so it will be shown as part of the preferences screen.
CATEGORY_GADGET: An activity of this type is embeddable in a parent activity.
CATEGORY_TEST :This is a test activity.
CATEGORY_EMBED :This category has been superseded by the GADGET category, but it’s been kept for backward compatibility.
10、检索与某Category匹配的主要活动的集合:
Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
PackageManager pm = getPackageManager();
List<ResolveInfo> list = pm.queryIntentActivities(mainIntent, 0);
11、Intent解析规则:
action:如果Intent 过滤器没有定义任何action,则匹配所有Intent
data:如果Intent 过滤器没有定义任何data,那么只匹配没有指定任何data的Intent
12、Android将所有传给startActivity()的隐式Intent视为好像它们包含至少一个类别:android.intent.category.DEFAULT,所以每个希望通过隐式Intent调用的Activity都需要在过滤器中声明这个默认的类别。
13、挂起Intent
根据具体情况,一个Android上下文有三种不同方法:
startActivty(intent)
startService(intent)
sendBroadcast(intent)
相应的有三种挂起Intent:
PendingIntent.getActivity(context, 0, intent, ...)
PendingIntent.getService(context, 0, intent, ...)
PendingIntent.getBroadcast(context, 0, intent, ...)
PendingIntent.getActivity(Context context, //originating context
int requestCode, //1,2, 3, etc
Intent intent, //original intent
int flags ) //flags
如果Intent相同,则用requestCode区分
flag表示如果已经存在挂起的Intent时执行的操作,具体解释如下: http://developer.android.com/reference/android/app/PendingIntent.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息