Intent—隐式Intent与显示Intent
来源:互联网 发布:中国产业经济数据库 编辑:程序博客网 时间:2024/04/30 01:45
button1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(FirstActivity.this, SecondActivity.class);
startActivity(intent);
}
});
<activity android:name=".SecondActivity" >
<intent-filter>
<action android:name="com.example.activitytest.ACTION_START" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="com.example.activitytest.MY_CATEGORY"/>
</intent-filter>
</activity>
在<action>标签中我们指明了当前活动可以响应com.example.activitytest.ACTION_ START这个action,而<category>标签则包含了一些附加信息,更精确地指明了当前的活动能够响应的Intent中还可能带有的category。
只用<action>和<category>中的内容同时能够匹配上Intent中指定的action和category时,这个活动才能响应该Intent。
button1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent("com.example.activitytest.ACTION_START");
startActivity(intent);
}
});
注:android.intent.category.DEFAULT是一种默认的category,在调用startActivity()方法的时候会自动将这个category添加到Intent中。
每个Intent钟只能指定一个action,但却能指定多个category。如下:
button1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent("com.example.activitytest.ACTION_START");
//需要在<activity>标签中加入<category>声明,否则程序会报错。
intent.addCategory("com.example.activitytest.MY_CATEGORY");
startActivity(intent);
}
});
使用隐式Intent,我们不仅可以再自己的程序内活动,还可以启动其他程序的活动。例如:
button1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse("http://www.baidu.com"));
startActivity(intent);
}
});
<intent-filter>标签中再配置一个<data>标签,用于更精确地指定当前活动能够响应什么类型的数据。<data>标签中主要可以配置以下内容。
用于指定数据的协议部分,如上的http
android:host
用于指定数据的主机名部分,如www.baidu.com
android:port
用于指定数据的端口部分,一般紧随在主机名之后。
android:path
用于指定可以处理的数据类型,运行使用通配符的方式进行指定。
只有<data>标签中指定的内容和Intent中携带的Data完全一致时,当前活动才能够响应该Intent。不过一般在<data>标签中都不会指定过多的内容
<activity android:name=".ThirdActivity" >
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<data android:scheme="http" />
</intent-filter>
</activity>
button1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(Intent.ACTION_DIAL);
intent.setData(Uri.parse("tel:10086"));
startActivity(intent);
}
});
--------------------------------------------------------------------------------------------------------------------------------------
android 开发交流群:264706196
- Intent—隐式Intent与显示Intent
- 显示Intent,隐式Intent
- 显示Intent,隐式Intent
- 显示Intent,隐式Intent
- 隐式Intent和显示Intent
- 14-显示Intent,隐式Intent
- Intent(显示使用intent)
- 显示Intent与隐式Intent
- Intent与<intent-filter>
- intent与intent filter
- Intent 与 Intent filters
- intent
- Intent
- Intent
- Intent
- intent
- Intent
- intent
- Hebernate与DB2数据类型映射
- Android-SAX解析XML文件(http访问取回服务器XML)
- [Activity的工具类]BaseAppManager
- <form:errors>使用注意
- iOS base64 加密解密
- Intent—隐式Intent与显示Intent
- bash 与 csh 区别
- openwrt SDK, 利用SDK生成自己的ipk安装包
- linux中线程同步和互斥的区别
- elasticsearch-mapping-introduction
- 《SED 单行脚本快速参考》的 perl 实现
- addr2line探秘
- Intent-数据传递
- Interpreting Raw SQL_TRACE output (文档 ID 39817.1)