2015年10月4日 星期日

Activity and Intent

Intent

use to request an action from another app component
like evolve, can put same information in it

Two type of Intent

Use the intenExplicit intents vs Implicit intents

Explicit intents:spefiiced to which activity

Implicit intents: don’tspefiiced to which activity,but declare a general action to perform comparing the contents of the intent to the intent filters declared in the manifest file of other apps on the device.
to find what app can be call.

Explain:

 Illustration of how an implicit intent is delivered through the system to start another activity: [1] Activity creates an Intent with an action description and passes it to startActivity(). [2] The Android System searches all apps for an intent filter that matches the intent. When a match is found, [3] the system starts the matching activity (Activity B) by invoking its onCreate() method and passing it the Intent.

Android system will check all the intent filter in  manifest  in different app,to find the right apps,

to start another Activity by Explicit intents

if i am in Activity A, I want to start the Activity B


1
2
3
 Intent intent = new Intent(this,B.class);
 intent.putExtra("name","vlue");// put other information on to the intent
 startActivity(intent);


Send informatiom to other Activity

At Activity B, I want to get back the information that i put into the intent


1
2
Intent intent = getIntent();
String s=intent.getStringExtra("name");


Bundle

bundle is like a box, we can put many thing in it.


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
put bundle into the intent

 Bundle bundle=new Bundle();
 bundle.putChar("key", 'v');
 intent.putExtra("bundle", bundle);

get value form the bundle

Bundle bundle1=intent.getBundleExtra("bundle");
 bundle1.getChar("key");


Some common intent use in Implicit intents

explain Component name, Action, Data, Category

http://developer.android.com/guide/components/intents-filters.html



Intent and the Filter

ref:http://jasonandroid.blogspot.hk/2013/10/intents-and-intent-filters.html


Explicit intents vs Implicit intents and intent filter




A intent can hold Component name, Action, Data, Category,Extra and Flag
if not have Component name,the intent is a Implicit intents

Component name
Action
A string that specifies the generic action to perform (such as view or pick).
the defined action

Data

Category
kind of component that should handle the intent.
Extra
Flag




how to create a intent filter for a Activity?
we need to think abou whta action that the activity what to do,than set the app right data ,action and ategory

a intentFilter at last have on action

if a activity want to receive Implicit intents,must have

<category android:name="android.intent.category.DEFAULT"/>

example





How to compare the Intent and the rule of compare the Data?

https://developer.android.com/guide/components/intents-filters.html

action test,data test(type and data),Category test

Action test

in the filter,can have one or more action

How to pass?
if the intent want to pass the test,one action match is ok

if intent filter have no action,no intent can pass the test

if intent has no action,but the filter have one or more action,the intent pass the test

Data test(type and data)

data can


<scheme>://<host>:<port>/<path>

For example:


content://com.example.project:200/folder/subfolder/etc

example

<intent-filter>
    <data android:mimeType="video/mpeg" android:scheme="http" ... />
    <data android:mimeType="audio/mpeg" android:scheme="http" ... />
    ...
</intent-filter>

<intent-filter>
    <data android:mimeType="image/*" />
    ...
</intent-filter>


important:
Each of these attributes is optional in a <data> element, but there are linear dependencies:

If a scheme is not specified, the host is ignored.
If a host is not specified, the port is ignored.
If both the scheme and host are not specified, the path is ignored.

Compare the dtat

compared only to the parts of the URI included in the filter. For example:

If a filter specifies only a scheme, all URIs with that scheme match the filter.
If a filter specifies a scheme and an authority but no path, all URIs with the same scheme and authority pass the filter, regardless of their paths.
If a filter specifies a scheme, an authority, and a path, only URIs with the same scheme, authority, and path pass the filter.




The data test compares both the URI and the MIME type in the intent to a URI and MIME type specified in the filter. The rules are as follows:
  1. An intent that contains neither a URI nor a MIME type passes the test only if the filter does not specify any URIs or MIME types.
  2. An intent that contains a URI but no MIME type (neither explicit nor inferable from the URI) passes the test only if its URI matches the filter's URI format and the filter likewise does not specify a MIME type.
  3. An intent that contains a MIME type but not a URI passes the test only if the filter lists the same MIME type and does not specify a URI format.
  4. An intent that contains both a URI and a MIME type (either explicit or inferable from the URI) passes the MIME type part of the test only if that type matches a type listed in the filter. It passes the URI part of the test either if its URI matches a URI in the filter or if it has a content: or file: URI and the filter does not specify a URI. In other words, a component is presumed to support content: and file: data if its filter lists only a MIME type.



Category test

Filter can have 0 or more category

How to pass?

all the Category  in the intent must match the Category   in the filter,
the other Category   in the filter no need to consider
intent have no Category  ,must pass the test

important!!!!
if intent send by startActivity()

CATEGORY_DEFAUL will add,so the filter must have CATEGORY_DEFAULT
if filter already have CATEGORY.LAUNCGER and CATEGORY.DEFAULT,no need CATEGORY_DEFAULT