Intent类 Jiangdg_VIP http://blog.csdn.net/u012637501 1.概述 Intent直译是指意图,目的的意思,在Android中,它是一种用来执行一个操作的抽象描述,它可以用来启动一个Activity,实现Activity之间的跳转,还可以发送广播,启动服务。 Intent还可以作为连接每个Activity的纽带,在每个Activity之间传递数据。 public class java.lang.Object ↳ android.content.Intent 2.Intent数据结构 (1)action属性:代表该Intent所要完成的一个执行动作,比如 ACTION_VIEW, ACTION_EDIT, ACTION_MAIN等 (2)data属性:通常用于向Action属性提供操作的数据,注意Data属性只接受一个Uri对象。 博主笔记:action属性和data属性为Intent所传递信息的主要部分,action/data属性举例: ACTION_VIEW content://contacts/people/1 -- 传递的信息:显示号码为1的人相关信息 ACTION_DIAL content://contacts/people/1 --传递的信息:给编号为1的人电话 ACTION_VIEW tel:123 -- 传递的信息:将号码123显示 ACTION_DIAL tel:123 --传递的信息:拨打号码123 ACTION_EDIT content://contacts/people/1 --传递的信息:编辑编号为1的联系人 ACTION_VIEW content://contacts/people/ --传递的信息:列出显示所有联系人.如果希望在查看某个联系人,需要定义一个新的intent并且属性设置为{ ACTION_VIEW content://contacts/N } 传递到一个新的Activity。 总结:action属性、data属性是intent的主要属性。 (3)category属性:用于为Action增加额外的附加类别信息,CATEGORY常量默认为CATEGORY_DEFAULT,通常Action属性会与Category属性结合使用。 比如,CATEGORY_LAUNCHER表示Intent传递的Activity显示顶级程序列表中,即当启动程序时使这个界面第一个显示。 (4)type属性:显式指定Intent的数据类型(MIME)。一般Intent数据类型能够根据数据本身进行判定,但是假如设置了这个属性,会强制采用显式指定的类型。 (5)component属性:指定Intent的目标组件的类名称。通常 Android会根据Intent 中包含的其它属性的信息,比如action、data/type、category进行查找,最终找到一个与之匹配的目标组件。但是,如果 component这个属性有指定的话,将直接使用它指定的组件,而不再执行上述查找过程。指定了这个属性以后,Intent的其它所有属性都是可选的。 (6)extras属性:Intent的Extras属性为一个Bundle对象,用于在多个Action之间进行数据交换。是其它所有附加信息的集合。使用extras可以为组件提供扩展信息,比如,如果要执行“发送电子邮件”这个动作,可以将电子邮件的标题、正文等保存在extras里,传给电子邮件发送组件。 博主笔记:在Intent类中,定义了许多action和category常量。但是,在安卓应用程序中,其有自己的定义方式(java字符串形式)去使用这些常量以保证它们的唯一性。比如ACTION_VIEW常量对应的字符串为 "android.intent.action.VIEW"。
在AndroidManifest.xml 文件中: <intent-filter> <action android:name="android.intent.action.INSERT" /> <category android:name="android.intent.category.DEFAULT" /> <data android:mimeType="vnd.android.cursor.dir/vnd.google.note" /> </intent-filter> 3.构造函数:
Intent() Create an empty intent.

Intent( Intento) Copy constructor.

Intent( Stringaction) Create an intent with a given action.

Intent( Stringaction, Uriuri) Create an intent with a given actionand for a given data url.

Intent( ContextpackageContext, Class<?> cls) Create an intent for a specific component.

Intent( Stringaction, Uriuri, ContextpackageContext, Class<?> cls) Create an intent for a specific component with a specified action and data.
4.Intent的使用方法 (1)Intent可以从开发者自己的程序跳转到系统应用界面,比如点击一个按钮跳转到发短信的界面,其使用方式是通过uri的方式进行跳转,具体如下:
        Intent it = new Intent(Intent.ACTION_VIEW);         it.putExtra("sms_body", "The SMS text");        it.setType("vnd.android-dir/mms-sms");        startActivity(it);
(2)启动一个Activity,实现Activity之间的跳转
 <span style="white-space:pre"></span>Intent it = new Intent(Main.this,Second.class);        startActivity(it);
(3)设置需要发送的信息,通过广播将此Intent发送出去
       Intent it = new Intent();         it.setAction("message");        it.putExtra("message ", msg);        sendBroadcast(it);
(4)启动/关闭一个服务
      Intent it = new Intent(Main.this,Second.class);        startService(it);        stopService(it);
5.方法
Intent addCategory(Stringcategory) Add a new category to the intent.
Intent addFlags(int flags) Add additional flags to the intent(or with existing flags value).
Object clone() Creates and returns a copy of this Object.
Intent cloneFilter() Make a clone of only the parts of the Intent that are relevant for filter matching: the action, data, type, component, and categories.
static Intent createChooser(Intenttarget,CharSequencetitle) Convenience function for creating aACTION_CHOOSERIntent.
int describeContents() Describe the kinds of special objects contained in this Parcelable's marshalled representation.
int fillIn( Intentother, int flags) Copy the contents of otherin to this object, but only where fields are not defined by this object.
boolean filterEquals( Intentother) Determine if two intents are the same for the purposes of intent resolution (filtering).
int filterHashCode() Generate hash code that matches semantics of filterEquals().
String getAction() Retrieve the general actionto be performed, such as ACTION_VIEW.
boolean[] getBooleanArrayExtra( Stringname) Retrieve extended data from the intent.
boolean getBooleanExtra( Stringname, boolean defaultValue) Retrieve extended data from the intent.
Bundle getBundleExtra( Stringname) Retrieve extended data from the intent.
byte[] getByteArrayExtra( Stringname) Retrieve extended data from the intent.
byte getByteExtra( Stringname, byte defaultValue) Retrieve extended data from the intent.
Set< String> getCategories() Return the set of all categories in the intent.
char[] getCharArrayExtra( Stringname) Retrieve extended data from the intent.
char getCharExtra( Stringname, char defaultValue) Retrieve extended data from the intent.
CharSequence[] getCharSequenceArrayExtra( Stringname) Retrieve extended data from the intent.
ArrayList< CharSequence> getCharSequenceArrayListExtra( Stringname) Retrieve extended data from the intent.
CharSequence getCharSequenceExtra( Stringname) Retrieve extended data from the intent.
ClipData getClipData() Return the ClipDataassociated with this Intent.
ComponentName getComponent() Retrieve the concrete component associated with the intent.
Uri getData() Retrieve data this intent is operating on.
String getDataString() The same as getData(), but returns the URI as an encoded String.
double[] getDoubleArrayExtra( Stringname) Retrieve extended data from the intent.
double getDoubleExtra( Stringname, double defaultValue) Retrieve extended data from the intent.
Bundle getExtras() Retrieves a map of extended data from the intent.
int getFlags() Retrieve any special flags associated with this intent.
float[] getFloatArrayExtra( Stringname) Retrieve extended data from the intent.
float getFloatExtra( Stringname, float defaultValue) Retrieve extended data from the intent.
int[] getIntArrayExtra( Stringname) Retrieve extended data from the intent.
int getIntExtra( Stringname, int defaultValue) Retrieve extended data from the intent.
ArrayList< Integer> getIntegerArrayListExtra( Stringname) Retrieve extended data from the intent.
static Intent getIntent( Stringuri) This method was deprecated in API level 4. UseparseUri(String, int)instead.
static Intent getIntentOld( Stringuri)
long[] getLongArrayExtra( Stringname) Retrieve extended data from the intent.
long getLongExtra( Stringname, long defaultValue) Retrieve extended data from the intent.
String getPackage() Retrieve the application package name this Intent is limited to.
Parcelable[] getParcelableArrayExtra( Stringname) Retrieve extended data from the intent.
<Textends Parcelable> ArrayList<T> getParcelableArrayListExtra( Stringname) Retrieve extended data from the intent.
<Textends Parcelable> T getParcelableExtra( Stringname) Retrieve extended data from the intent.
String getScheme() Return the scheme portion of the intent's data.
Intent getSelector() Return the specific selector associated with this Intent.
Serializable getSerializableExtra( Stringname) Retrieve extended data from the intent.
short[] getShortArrayExtra( Stringname) Retrieve extended data from the intent.
short getShortExtra( Stringname, short defaultValue) Retrieve extended data from the intent.
Rect getSourceBounds() Get the bounds of the sender of this intent, in screen coordinates.
String[] getStringArrayExtra( Stringname) Retrieve extended data from the intent.
ArrayList< String> getStringArrayListExtra( Stringname) Retrieve extended data from the intent.
String getStringExtra( Stringname) Retrieve extended data from the intent.
String getType() Retrieve any explicit MIME type included in the intent.
boolean hasCategory( Stringcategory) Check if a category exists in the intent.
boolean hasExtra( Stringname) Returns true if an extra value is associated with the given name.
boolean hasFileDescriptors() Returns true if the Intent's extras contain a parcelled file descriptor.
static Intent makeMainActivity( ComponentNamemainActivity) Create an intent to launch the main (root) activity of a task.
static Intent makeMainSelectorActivity( StringselectorAction, StringselectorCategory) Make an Intent for the main activity of an application, without specifying a specific activity to run but giving a selector to find the activity.
static Intent makeRestartActivityTask( ComponentNamemainActivity) Make an Intent that can be used to re-launch an application's task in its base state.
static String normalizeMimeType( Stringtype) Normalize a MIME data type.
static Intent parseIntent( Resourcesresources, XmlPullParserparser, AttributeSetattrs) Parses(解析) the "intent" element (and its children) from XML and instantiates an Intent object.
static Intent parseUri( Stringuri, int flags) Create an intent from a URI.
Intent putCharSequenceArrayListExtra( Stringname, ArrayList< CharSequence> value) Add extended data to the intent.
Intent putExtra( Stringname, double[] value) Add extended data to the intent.
Intent putExtra( Stringname, int value) Add extended data to the intent.
Intent putExtra( Stringname, CharSequencevalue) Add extended data to the intent.
Intent putExtra( Stringname, char value) Add extended data to the intent.
Intent putExtra( Stringname, Bundlevalue) Add extended data to the intent.
Intent putExtra( Stringname, Parcelable[]value) Add extended data to the intent.
Intent putExtra( Stringname, Serializablevalue) Add extended data to the intent.
Intent putExtra( Stringname, int[] value) Add extended data to the intent.
Intent putExtra( Stringname, float value) Add extended data to the intent.
Intent putExtra( Stringname, byte[] value) Add extended data to the intent.
Intent putExtra( Stringname, long[] value) Add extended data to the intent.
Intent putExtra( Stringname, Parcelablevalue) Add extended data to the intent.
Intent putExtra( Stringname, float[] value) Add extended data to the intent.
Intent putExtra( Stringname, long value) Add extended data to the intent.
Intent putExtra( Stringname, String[]value) Add extended data to the intent.
Intent putExtra( Stringname, boolean value) Add extended data to the intent.
Intent putExtra( Stringname, boolean[] value) Add extended data to the intent.
Intent putExtra( Stringname, short value) Add extended data to the intent.
Intent putExtra( Stringname, double value) Add extended data to the intent.
Intent putExtra( Stringname, short[] value) Add extended data to the intent.
Intent putExtra( Stringname, Stringvalue) Add extended data to the intent.
Intent putExtra( Stringname, byte value) Add extended data to the intent.
Intent putExtra( Stringname, char[] value) Add extended data to the intent.
Intent putExtra( Stringname, CharSequence[]value) Add extended data to the intent.
Intent putExtras( Intentsrc) Copy all extras in 'src' in to this intent.
Intent putExtras( Bundleextras) Add a set of extended data to the intent.
Intent putIntegerArrayListExtra( Stringname, ArrayList< Integer> value) Add extended data to the intent.
Intent putParcelableArrayListExtra( Stringname, ArrayList<?extends Parcelable> value) Add extended data to the intent.
Intent putStringArrayListExtra( Stringname, ArrayList< String> value) Add extended data to the intent.
void readFromParcel( Parcelin)
void removeCategory( Stringcategory) Remove a category from an intent.
void removeExtra( Stringname) Remove extended data from the intent.
Intent replaceExtras( Bundleextras) Completely replace the extras in the Intent with the given Bundle of extras.
Intent replaceExtras( Intentsrc) Completely replace the extras in the Intent with the extras in the given Intent.
ComponentName resolveActivity( PackageManagerpm) Return the Activity component that should be used to handle this intent.
ActivityInfo resolveActivityInfo( PackageManagerpm, int flags) Resolve the Intent into an ActivityInfodescribing the activity that should execute the intent.
String resolveType( ContentResolverresolver) Return the MIME data type of this intent.
String resolveType( Contextcontext) Return the MIME data type of this intent.
String resolveTypeIfNeeded( ContentResolverresolver) Return the MIME data type of this intent, only if it will be needed for intent resolution.
Intent setAction( Stringaction) Set the general action to be performed.
Intent setClass( ContextpackageContext, Class<?> cls) Convenience for callingsetComponent(ComponentName)with the name returned by aClassobject.
Intent setClassName( ContextpackageContext, StringclassName) Convenience for callingsetComponent(ComponentName)with an explicit class name.
Intent setClassName( StringpackageName, StringclassName) Convenience for calling setComponent(ComponentName)with an explicit application package name and class name.
void setClipData( ClipDataclip) Set a ClipDataassociated with this Intent.
Intent setComponent( ComponentNamecomponent) (Usually optional) Explicitly set the component to handle the intent.
Intent setData( Uridata) Set the data this intent is operating on.
Intent setDataAndNormalize( Uridata) Normalize and set the data this intent is operating on.
Intent setDataAndType( Uridata, Stringtype) (Usually optional) Set the data for the intent along with an explicit MIME data type.
Intent setDataAndTypeAndNormalize( Uridata, Stringtype) (Usually optional) Normalize and set both the data Uri and an explicit MIME data type.
void setExtrasClassLoader( ClassLoaderloader) Sets the ClassLoader that will be used when unmarshalling any Parcelable values from the extras of this Intent.
Intent setFlags(int flags) Set special flags controlling how this intent is handled.
Intent setPackage( StringpackageName) (Usually optional) Set an explicit application package name that limits the components this Intent will resolve to.
void setSelector( Intentselector) Set a selector for this Intent.
void setSourceBounds( Rectr) Set the bounds of the sender of this intent, in screen coordinates.
Intent setType( Stringtype) Set an explicit MIME data type.
Intent setTypeAndNormalize( Stringtype) Normalize and set an explicit MIME data type.
String toString() Returns a string containing a concise(简洁), human-readable (可读)description of this object.
String toURI() This method was deprecated in API level 4. UsetoUri(int)instead.
String toUri(int flags) Convert this Intent into a String holding a URI representation of it.
void writeToParcel( Parcelout, int flags) Flatten this object in to a Parcel.
6.ACTION属性常量 (1)标准Activity Actions 以下actions常量,用于Intent定义用来操作各种Activity,通常使用 startActivity(Intent)方法实现。
  • ACTION_MAIN //传递返回到主Activity动作
  • ACTION_VIEW //传递显示动作
  • ACTION_ATTACH_DATA
  • ACTION_EDIT //传递编辑动作
  • ACTION_PICK
  • ACTION_CHOOSER //传递选择动作
  • ACTION_GET_CONTENT
  • ACTION_DIAL
  • ACTION_CALL
  • ACTION_SEND
  • ACTION_SENDTO
  • ACTION_ANSWER //传递接听电话动作
  • ACTION_INSERT
  • ACTION_DELETE
  • ACTION_RUN
  • ACTION_SYNC
  • ACTION_PICK_ACTIVITY
  • ACTION_SEARCH
  • ACTION_WEB_SEARCH
  • ACTION_FACTORY_TEST
(2) 标准 Broadcast Actions 以下"意图"的action属性常量,用于接收广播,通常使用 registerReceiver(BroadcastReceiver, IntentFilter)方法或者 在AndroidManifest.xml 文件中定义了<receiver>属性的Activity。
  • ACTION_TIME_TICK
  • ACTION_TIME_CHANGED
  • ACTION_TIMEZONE_CHANGED
  • ACTION_BOOT_COMPLETED
  • ACTION_PACKAGE_ADDED
  • ACTION_PACKAGE_CHANGED
  • ACTION_PACKAGE_REMOVED
  • ACTION_PACKAGE_RESTARTED
  • ACTION_PACKAGE_DATA_CLEARED
  • ACTION_UID_REMOVED
  • ACTION_BATTERY_CHANGED
  • ACTION_POWER_CONNECTED
  • ACTION_POWER_DISCONNECTED
  • ACTION_SHUTDOWN
7.标准Categories常量 为Action增加额外的附加类别信息,通常使用addCategory (String category)方法。
  • CATEGORY_DEFAULT
  • CATEGORY_BROWSABLE
  • CATEGORY_TAB
  • CATEGORY_ALTERNATIVE
  • CATEGORY_SELECTED_ALTERNATIVE
  • CATEGORY_LAUNCHER
  • CATEGORY_INFO
  • CATEGORY_HOME
  • CATEGORY_PREFERENCE
  • CATEGORY_TEST
  • CATEGORY_CAR_DOCK
  • CATEGORY_DESK_DOCK
  • CATEGORY_LE_DESK_DOCK
  • CATEGORY_HE_DESK_DOCK
  • CATEGORY_CAR_MODE
  • CATEGORY_APP_MARKET
8.标准Extra Data常量 通过putExtra(String, Bundle)方法实现。
  • EXTRA_ALARM_COUNT
  • EXTRA_BCC
  • EXTRA_CC
  • EXTRA_CHANGED_COMPONENT_NAME
  • EXTRA_DATA_REMOVED
  • EXTRA_DOCK_STATE
  • EXTRA_DOCK_STATE_HE_DESK
  • EXTRA_DOCK_STATE_LE_DESK
  • EXTRA_DOCK_STATE_CAR
  • EXTRA_DOCK_STATE_DESK
  • EXTRA_DOCK_STATE_UNDOCKED
  • EXTRA_DONT_KILL_APP
  • EXTRA_EMAIL
  • EXTRA_INITIAL_INTENTS
  • EXTRA_INTENT
  • EXTRA_KEY_EVENT
  • EXTRA_ORIGINATING_URI
  • EXTRA_PHONE_NUMBER
  • EXTRA_REFERRER
  • EXTRA_REMOTE_INTENT_TOKEN
  • EXTRA_REPLACING
  • EXTRA_SHORTCUT_ICON
  • EXTRA_SHORTCUT_ICON_RESOURCE
  • EXTRA_SHORTCUT_INTENT
  • EXTRA_STREAM
  • EXTRA_SHORTCUT_NAME
  • EXTRA_SUBJECT
  • EXTRA_TEMPLATE
  • EXTRA_TEXT
  • EXTRA_TITLE
  • EXTRA_UID
9.Flags 通过 setFlags(int) 和addFlags(int)设置intent的flags属性。
  • getFlags()
  • addFlags(int)
  • FLAG_GRANT_READ_URI_PERMISSION
  • FLAG_GRANT_WRITE_URI_PERMISSION
  • FLAG_GRANT_PERSISTABLE_URI_PERMISSION
  • FLAG_GRANT_PREFIX_URI_PERMISSION
  • FLAG_DEBUG_LOG_RESOLUTION
  • FLAG_FROM_BACKGROUND
  • FLAG_ACTIVITY_BROUGHT_TO_FRONT
  • FLAG_ACTIVITY_CLEAR_TASK
  • FLAG_ACTIVITY_CLEAR_TOP
  • FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET
  • FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS
  • FLAG_ACTIVITY_FORWARD_RESULT
  • FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY
  • FLAG_ACTIVITY_MULTIPLE_TASK
  • FLAG_ACTIVITY_NEW_DOCUMENT
  • FLAG_ACTIVITY_NEW_TASK
  • FLAG_ACTIVITY_NO_ANIMATION
  • FLAG_ACTIVITY_NO_HISTORY
  • FLAG_ACTIVITY_NO_USER_ACTION
  • FLAG_ACTIVITY_PREVIOUS_IS_TOP
  • FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
  • FLAG_ACTIVITY_REORDER_TO_FRONT
  • FLAG_ACTIVITY_SINGLE_TOP
  • FLAG_ACTIVITY_TASK_ON_HOME
  • FLAG_RECEIVER_REGISTERED_ONLY

参考: http://developer.android.com/reference/android/net/Uri.html













更多相关文章

  1. Android清单文件中相关属性含义(Provider)
  2. layout 布局
  3. Android(安卓)activity之间传递自定义类型数据【Serializable实
  4. android中的隐式intent跟显式intent的使用
  5. android基础知识13:AndroidManifest.xml文件解析
  6. Android(安卓)init简介
  7. Android(安卓)中自定义控件和属性
  8. Android(安卓)JNI编程学习
  9. Android(安卓)开发——浅谈onInterceptTouchEvent、onTouchEvent

随机推荐

  1. android TextView 文本过长时用滚动条显
  2. Android(安卓)viewPage notifyDataSetCha
  3. android:网路检测
  4. android 傻瓜式 MultiDex 插件,从此再也不
  5. 一个android参考网站,工具+源码
  6. minSdkVersion各个版本号对应android版本
  7. android源码下载-等待提示动画
  8. centos下安装adb环境
  9. android:inputType参数类型说明
  10. Android通过PHP连接MySQL(传值查询)