Android突破三:Intent类

Intent类
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( Intent o)
Copy constructor.

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

Intent( String action,  Uri uri)
Create an intent with a  given action and for a given  data url.

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

Intent( String action,  Uri uri,  Context packageContext,  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.方法
addCategory(String category)
Add a new category to the intent.
addFlags(int flags)
Add additional flags to the intent (or with existing flags value).
clone()
Creates and returns a copy of this  Object.
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(Intent target, CharSequence title)
Convenience function for  creating a ACTION_CHOOSER Intent.
int
Describe the kinds of special objects contained in this Parcelable's marshalled representation.
int
fillIn( Intent other, int flags)
Copy the contents of  other in to this object, but only where fields are not defined by this object.
boolean
filterEquals( Intent other)
Determine if two intents are the same for the purposes of intent resolution (filtering).
int
Generate hash code that matches semantics of filterEquals().
Retrieve the general action to be performed, such as  ACTION_VIEW.
boolean[]
Retrieve extended data from the intent.
boolean
getBooleanExtra( String name, boolean defaultValue)
Retrieve extended data from the intent.
Retrieve extended data from the intent.
byte[]
Retrieve extended data from the intent.
byte
getByteExtra( String name, byte defaultValue)
Retrieve extended data from the intent.
Return the set of all categories in the intent.
char[]
Retrieve extended data from the intent.
char
getCharExtra( String name, char defaultValue)
Retrieve extended data from the intent.
Retrieve extended data from the intent.
Retrieve extended data from the intent.
Retrieve extended data from the intent.
Return the  ClipData associated with this Intent.
Retrieve the concrete component associated with the intent.
Uri
Retrieve data this intent is operating on.
The same as  getData(), but returns the URI as an encoded String.
double[]
Retrieve extended data from the intent.
double
getDoubleExtra( String name, double defaultValue)
Retrieve extended data from the intent.
Retrieves a map of extended data from the intent.
int
Retrieve any special flags associated with this intent.
float[]
Retrieve extended data from the intent.
float
getFloatExtra( String name, float defaultValue)
Retrieve extended data from the intent.
int[]
Retrieve extended data from the intent.
int
getIntExtra( String name, int defaultValue)
Retrieve extended data from the intent.
Retrieve extended data from the intent.
static  Intent
getIntent( String uri)
This method was deprecated in API level 4. Use parseUri(String, int) instead.
static  Intent
long[]
Retrieve extended data from the intent.
long
getLongExtra( String name, long defaultValue)
Retrieve extended data from the intent.
Retrieve the application package name this Intent is limited to.
Retrieve extended data from the intent.
<T extends  ParcelableArrayList<T>
Retrieve extended data from the intent.
<T extends  Parcelable> T
Retrieve extended data from the intent.
Return the scheme portion of the intent's data.
Return the specific selector associated with this Intent.
Retrieve extended data from the intent.
short[]
Retrieve extended data from the intent.
short
getShortExtra( String name, short defaultValue)
Retrieve extended data from the intent.
Get the bounds of the sender of this intent, in screen coordinates.
Retrieve extended data from the intent.
Retrieve extended data from the intent.
Retrieve extended data from the intent.
Retrieve any explicit MIME type included in the intent.
boolean
hasCategory( String category)
Check if a category exists in the intent.
boolean
hasExtra( String name)
Returns true if an extra value is associated with the given name.
boolean
Returns true if the Intent's extras contain a parcelled file descriptor.
static  Intent
Create an intent to launch the main (root) activity of a task.
static  Intent
makeMainSelectorActivity( String selectorAction,  String selectorCategory)
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
Make an Intent that can be used to re-launch an application's task in its base state.
static  String
Normalize a MIME data type.
static  Intent
parseIntent( Resources resources,  XmlPullParser parser,  AttributeSet attrs)
Parses(解析) the "intent" element (and its children) from XML and instantiates an Intent object.
static  Intent
parseUri( String uri, int flags)
Create an intent from a URI.
Add extended data to the intent.
putExtra( String name, double[] value)
Add extended data to the intent.
putExtra( String name, int value)
Add extended data to the intent.
putExtra( String name,  CharSequence value)
Add extended data to the intent.
putExtra( String name, char value)
Add extended data to the intent.
putExtra( String name,  Bundle value)
Add extended data to the intent.
putExtra( String name,  Parcelable[] value)
Add extended data to the intent.
putExtra( String name,  Serializable value)
Add extended data to the intent.
putExtra( String name, int[] value)
Add extended data to the intent.
putExtra( String name, float value)
Add extended data to the intent.
putExtra( String name, byte[] value)
Add extended data to the intent.
putExtra( String name, long[] value)
Add extended data to the intent.
putExtra( String name,  Parcelable value)
Add extended data to the intent.
putExtra( String name, float[] value)
Add extended data to the intent.
putExtra( String name, long value)
Add extended data to the intent.
putExtra( String name,  String[] value)
Add extended data to the intent.
putExtra( String name, boolean value)
Add extended data to the intent.
putExtra( String name, boolean[] value)
Add extended data to the intent.
putExtra( String name, short value)
Add extended data to the intent.
putExtra( String name, double value)
Add extended data to the intent.
putExtra( String name, short[] value)
Add extended data to the intent.
putExtra( String name,  String value)
Add extended data to the intent.
putExtra( String name, byte value)
Add extended data to the intent.
putExtra( String name, char[] value)
Add extended data to the intent.
putExtra( String name,  CharSequence[] value)
Add extended data to the intent.
putExtras( Intent src)
Copy all extras in 'src' in to this intent.
putExtras( Bundle extras)
Add a set of extended data to the intent.
Add extended data to the intent.
putParcelableArrayListExtra( String name,  ArrayList<? extends  Parcelable> value)
Add extended data to the intent.
Add extended data to the intent.
void
void
removeCategory( String category)
Remove a category from an intent.
void
Remove extended data from the intent.
replaceExtras( Bundle extras)
Completely replace the extras in the Intent with the given Bundle of extras.
Completely replace the extras in the Intent with the extras in the given Intent.
Return the Activity component that should be used to handle this intent.
Resolve the Intent into an  ActivityInfo describing the activity that should execute the intent.
Return the MIME data type of this intent.
resolveType( Context context)
Return the MIME data type of this intent.
Return the MIME data type of this intent, only if it will be needed for intent resolution.
setAction( String action)
Set the general action to be performed.
setClass( Context packageContext,  Class<?> cls)
Convenience for calling setComponent(ComponentName) with the name returned by a Class object.
setClassName( Context packageContext,  String className)
Convenience for calling setComponent(ComponentName) with an explicit class name.
setClassName( String packageName,  String className)
Convenience for calling  setComponent(ComponentName) with an explicit application package name and class name.
void
Set a  ClipData associated with this Intent.
(Usually optional) Explicitly set the component to handle the intent.
setData( Uri data)
Set the data this intent is operating on.
Normalize and set the data this intent is operating on.
setDataAndType( Uri data,  String type)
(Usually optional) Set the data for the intent along with an explicit MIME data type.
(Usually optional) Normalize and set both the data Uri and an explicit MIME data type.
void
Sets the ClassLoader that will be used when unmarshalling any Parcelable values from the extras of this Intent.
setFlags(int flags)
Set special flags controlling how this intent is handled.
setPackage( String packageName)
(Usually optional) Set an explicit application package name that limits the components this Intent will resolve to.
void
setSelector( Intent selector)
Set a selector for this Intent.
void
Set the bounds of the sender of this intent, in screen coordinates.
setType( String type)
Set an explicit MIME data type.
Normalize and set an explicit MIME data type.
Returns a string containing a concise(简洁), human-readable (可读)description of this object.
toURI()
This method was deprecated in API level 4. Use toUri(int) instead.
toUri(int flags)
Convert this Intent into a String holding a URI representation of it.
void
writeToParcel( Parcel out, int flags)
Flatten this object in to a Parcel.
6.ACTION属性常量
(1)标准Activity Actions
    以下actions常量,用于Intent定义用来操作各种Activity,通常使用 startActivity(Intent)方法实现。
(2) 标准 Broadcast Actions
    以下"意图"的action属性常量,用于接收广播,通常使用 registerReceiver(BroadcastReceiver, IntentFilter)方法或者 在AndroidManifest.xml 文件中定义了<receiver>属性的Activity。
7. 标准Categories常量 8.标准Extra Data常量 9.Flags
    通过 setFlags(int) 和addFlags(int)设置intent的flags属性。















评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值