标签:
Intent是一个将要执行的动作的抽象的描述,解决Android应用的各项组件之间的通讯,Intent负责对应用中一次操作的动作、动作涉及数据、附加数据进行描述,Android则根据此Intent的描述,负责找到对应的组件,将 Intent传递给调用的组件,并完成组件的调用。intent主要用来启动activity或者service(并携带需要传递的参数信息)。
意图包括:Action(动作),Category(附加信息),Data(数据,具体内容),Tpye(类型)等等,举个例子,说白了意图就是启动一个组件的的完整的动作信息,就像打人,打就是Action动作,人就是Data内容,而Type就是类型,打什么人呢?打坏人,type就是坏指的类型,只有这些信息全了才能执行一个完整的意图,当然还有一些信息,比如scheme就是URI类型的数据的前缀,就像这个例子当中的sms:,还有host主机名,path路径等。
(1).显示意图(Explicit Intents)
// 1.创建Intent实例化对象几种方式 Intent intent = new Intent(); intent.setClass(Context packageContext, Class<?> cls) ; //内部调用setComponent(ComponentName) intent.setClassName(Context packageContext, String className) ; //内部调用setComponent(ComponentName) intent.setClassName(String packageName, String className) ; //内部调用setComponent(ComponentName),可以激活外部应用 intent.setComponent(new ComponentName(this, Class<?> cls)); intent.setComponent(new ComponentName(this, "package Name"));
(2).隐式意图(Implicit Intents)
intent.setAction("com.baidu.action.TEST");
<action android:name="com.baidu.action.TEST"/>
Action | Data(Uri) | Content |
---|---|---|
ACTION_VIEW | content://contacts/people/1 | Display information about the person whose identifier is "1". |
ACTION_VIEW | tel:123 | Display the phone dialer with the given number filled in. |
ACTION_DIAL | tel:123 | Display the phone dialer with the given number filled in. |
intent.setData(Uri.parse("baidu://www.baidu.com/news"));
<!-- android:path 内容字符串需要以 / 开头 --> <data android:scheme="baidu" android:host="www.baidu.com" android:path="/news"/>
intent.addCategory("com.baidu.category.TEST");
<!-- 必须指定CATEGORY_DEFAULT,只有这样startActivity(intent)才能找到 --> <category android:name="com.baidu.category.TEST" /> <category android:name="android.intent.category.DEFAULT" />
intent.setType("image/jpeg");
//或
<data android:mimeType="image/*" />
intent.setDataAndType(Uri.parse("baidu://www.baidu.com/news"), "image/jpeg");
<data android:mimeType="image/*" android:scheme="baidu" android:host="www.baidu.com" android:path="/news"/>
(3).两者的使用区别
//Activity间的数据传递 // 1.直接向intent对象中传入键值对(相当于Intent对象具有Map键值对功能) intent.putExtra("first", text1.getText().toString()); intent.putExtra("second", text2.getText().toString()); // 2.新建一个Bundle对象 ,想该对象中加入键值对,然后将该对象加入intent中 Bundle bundle = new Bundle(); bundle.putString("first", "zhang"); bundle.putInt("age", 20); intent.putExtras(bundle); // 3.向intent中添加ArrayList集合对象 intent.putIntegerArrayListExtra(name, value); intent.putIntegerArrayListExtra(name, value); // 4.intent传递Object对象(被传递的对象的类实现Parcelable接口,或者实现Serialiable接口) public Intent putExtra(String name, Serializable value) public Intent putExtra(String name, Parcelable value)
4.Activity退出的返回结果
// 1.通过startActivityForResult方式启动一个Activity MainActivity.this.startActivityForResult(intent, 200); //intent对象,和 requestCode请求码 // 2.新activity设定setResult方法,通过该方法可以传递responseCode 和 Intent对象 setResult(101, intent2); //responseCode响应码 和 intent对象 // 3.在MainActivity中覆写onActivityResult方法,新activity一旦退出,就会执行该方法 protected void onActivityResult(int requestCode, int resultCode, Intent data) { Toast.makeText(this, data.getStringExtra("info")+"requestCode:"+requestCode+"resultCode:"+resultCode, Toast.LENGTH_LONG).show(); }
<activity android:name="com.example.player.MainActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> <!-- 指定action, data类型为video, category为default,设置完成之后,当打开video文件时,本应用作为可选打开软件 --> <intent-filter> <action android:name="android.intent.action.VIEW" /> <data android:mimeType="video/*" /> <category android:name="android.intent.category.DEFAULT" /> </intent-filter> </activity>
/** * 隐式意图的方法启动系统短信 * * 简单概括就是: 意图包括:Action(动作),Category(附加信息),Data(数据,具体内容),Tpye(类型)等等,举个例子, * 说白了意图就是启动一个组件的的完整的动作信息 * ,就像打人,打就是Action动作,人就是Data内容,而Type就是类型,打什么人呢?打坏人,type就是坏指的类型 * ,只有这些信息全了才能执行一个完整的意图 * ,当然还有一些信息,比如scheme就是URI类型的数据的前缀,就像这个例子当中的sms:,还有host主机名,path路径等 * * @param view */ public void startOne(View view) { Intent intent = new Intent(); intent.setAction("android.intent.action.SENDTO");// 发送信息的动作 intent.addCategory("android.intent.category.DEFAULT");// 附加信息 intent.setData(Uri.parse("sms:10086"));// 具体的数据,发送给10086 startActivity(intent); }
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="net.loonggg.intent" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="17" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name="net.loonggg.intent.MainActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name="net.loonggg.intent.SecondActivity" > <intent-filter> <!-- 自定义的动作 --> <action android:name="net.loonggg.xxx" /> <!-- 自定义的scheme和host --> <data android:host="www.baidu.com" android:path="/person" android:scheme="loonggg" /> <!-- 自定义的类型 --> <data android:mimeType="person/people" /> <!-- 附加信息 --> <category android:name="android.intent.category.DEFAULT" /> </intent-filter> </activity> </application> </manifest>
<activity android:name="net.loonggg.intent.SecondActivity" > <intent-filter> <!-- 自定义的动作 --> <action android:name="net.loonggg.xxx" /> <!-- 自定义的scheme和host --> <data android:host="www.baidu.com" android:path="/person" android:scheme="loonggg" /> <!-- 自定义的类型 --> <data android:mimeType="person/people" /> <!-- 附加信息 --> <category android:name="android.intent.category.DEFAULT" /> </intent-filter> </activity>
/** * 通过自定义的隐式意图启动 * * @param view */ public void startTwo(View view) { Intent intent = new Intent(); intent.setAction("net.loonggg.xxx"); intent.addCategory("android.intent.category.DEFAULT"); intent.setDataAndType(Uri.parse("loonggg://www.baidu.com/person"), "person/people"); startActivity(intent); }
注意:
intent.setData(data)和intent.setType(type)注意这两个方法会互相清除,意思就是:如果先设置setData(data)后设置setType(type),那么后设置的setType(type)会把前面setData(data)设置的内容清除掉,而且会报错,反之一样,所以如果既要设置类型与数据,那么使用setDataAndType(data,type)这个方法。
标签:
原文地址:http://www.cnblogs.com/deny-66/p/5589505.html