Android 中的Intent
来源:互联网 发布:金融数据分析师好学么? 编辑:程序博客网 时间:2024/06/07 16:25
Android基本的设计理念是鼓励减少组件之间的耦合,因此Android提供了Intent(意图),Intent提供了一种通用的消息系统,它允许在你的应用程序与其它的应用程序间传递,Intent来执行动作和产生事件,使用Intent可以激活Android应用三种类型的核心组件:活动,服务和广播接收者。
Intent可以划分成显式意图和隐式意图
显式意图:
调用intent.setComponent()\intent.setClassName()或intent.setClass()方法明确的指定了组件名的Intent为显式意图,显式意图明确指定了要激活的组件是哪个组件
隐式意图:
没有指定组件名的Intent为隐式意图,Android系统会根据隐式意图中设置的动作(action),类别(category),数据(Uri和数据类型),找到最合适的组件来处理这个意图
AndroidManifest.xml配置
<?xmlversion="1.0"encoding="utf-8"?>
<manifestxmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.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="com.example.intent.MainActivity"
android:label="@string/app_name">
<intent-filter>
<actionandroid:name="android.intent.action.MAIN"/>
<categoryandroid:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<activity
android:name="com.example.intent.OtherActivity"
android:label="@string/title_activity_other">
<intent-filter>
<actionandroid:name="com.darren.filter"/>
<categoryandroid:name="con.darren.category.type"/>
<categoryandroid:name="android.intent.category.DEFAULT"/>
<dataandroid:scheme="darren"android:host="www.darren.com"android:path="/zhang"android:mimeType="image/*"/>
</intent-filter>
</activity>
</application>
</manifest>
MainActivity 代码:
packagecom.example.intent;
importandroid.net.Uri;
importandroid.os.Bundle;
importandroid.app.Activity;
importandroid.content.Intent;
importandroid.view.Menu;
importandroid.view.View;
publicclassMainActivityextendsActivity {
@Override
protectedvoidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
publicbooleanonCreateOptionsMenu(Menu menu) {
//Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main,menu);
returntrue;
}
publicvoidopenActivity(View v){
//隐式意图激活Activity
Intent intent =newIntent();
//以下两行与要打开的Activity中的意图拦截器中的配置一致
/*
<intent-filter>
<actionandroid:name="com.darren.filter"/>
<categoryandroid:name="con.darren.category.type"/>
<categoryandroid:name="android.intent.category.DEFAULT"/>
</intent-filter>
*/
intent.setAction("com.darren.filter");
intent.addCategory("con.darren.category.type");
/*
*如果还要传递参数,那么要按照参数的格式进行传递,否则也会失败
<intent-filter>
<actionandroid:name="com.darren.filter"/>
<categoryandroid:name="con.darren.category.type"/>
<categoryandroid:name="android.intent.category.DEFAULT"/>
<dataandroid:scheme="darren"android:host="www.darren.com"/>
</intent-filter>
*/
// intent.setData(Uri.parse("darren://www.darren.com/liu"));
/*
*如果还要匹配路径,那么路径也要和过滤器的配置一样,否则失败
<intent-filter>
<actionandroid:name="com.darren.filter"/>
<categoryandroid:name="con.darren.category.type"/>
<categoryandroid:name="android.intent.category.DEFAULT"/>
<dataandroid:scheme="darren"android:host="www.darren.com" android:path="/zhang"/>
</intent-filter>
*/
// intent.setData(Uri.parse("darren://www.darren.com/zhang"));
/*
*如果还要设置文件类型,则还要匹配其类型,否则也失败
<intent-filter>
<actionandroid:name="com.darren.filter"/>
<categoryandroid:name="con.darren.category.type"/>
<categoryandroid:name="android.intent.category.DEFAULT"/>
<dataandroid:scheme="darren"android:host="www.darren.com" android:path="/zhang"android:mimeType="image/*"/>
</intent-filter>
*/
intent.setDataAndType(Uri.parse("darren://www.darren.com/zhang"),"image/gif");
/*
*注:如果用以下两行代码来表示的话会报错
intent.setData(Uri.parse("darren://www.darren.com/zhang"));
intent.setType("image/gif");
*因为setType方法会把设置的Data清空,导致data不匹配
*/
//方法内部为Intent添加了android.intent.category.DEFAULT类别
startActivity(intent);
}
//注:只要意图中的动作和类别都出现在意图过滤器中,就都能与意图过滤器匹配,否则匹配失败
}
- Android中的intent
- 【转】Android中的Intent
- Android中的Intent
- Android中的Intent
- Android中的Intent
- Android中的Intent总结
- Android中的Intent
- Android 中的Intent
- android中的intent
- Android中的Intent
- Android中的Intent
- android 中的意图intent
- Android中的Intent详解
- Android 中的 Intent
- android中的intent
- Android中的intent
- android中的activity---intent
- Android中的Intent详解
- ubuntu12.04给firefox浏览器安装java插件
- 高精度加法也贴上吧...虽然很简单...
- android的内外边距
- 图片_防盗链
- 【问题记录】sbd——数据库突然hang住
- Android 中的Intent
- zoj1028-Flip and Shift
- .NET ajax 阻止联动造成页面刷新
- Lua语言中pairs和ipairs的区别
- 无名管道的C++封装
- 查询表空间建立表空间和删除表空间
- Morphia 注解详解
- 使用Varnish代替Squid做网站缓存加速器的详细解决方案
- varnish后端的备份