Android菜鸟的成长笔记(8)——Intent与Intent Filter(上)
原文:[置顶] Android菜鸟的成长笔记(8)——Intent与Intent Filter(上)
Intent代表了Android应用的启动“意图”,Android应用将会根据Intent来启动指定组件,至于到底启动哪个组件,取决于Intent的各个属性。
一、显式的Intent
明确指定了要启动的组件的Intent我们称为显式的Intent
例如:
package com.example.testintent; import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button; public class MainActivity extends Activity {
Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button)findViewById(R.id.button1);
button.setOnClickListener(new OnClickListener() { @Override
public void onClick(View arg0) {
ComponentName comp = new ComponentName(MainActivity.this, SecondActivity.class);
Intent intent = new Intent();
intent.setComponent(comp);
startActivity(intent);
}
});
}
}
注意在manifest文件里注册SecondActivity
Intent的Componet属性需要接收一个ComponentName对象,ComponetName对象其实是一个指定包和要启动的Activity路径的类,有如下几个构造
componentName(String pkg, String cls)
componentName(Context pkg, String cls)
componentName(Context pkg, Class<?> cls)
componentName(Parcel in)
除了这个属性之外Intent还包含了如下三个方法:
setClass(Context packageContext, Class<?> cls )
setClassName(Context packageContext, String className)
setClassName(String packageName, String className )
上面的代码可以简化为如下代码:
Intent intent = new Intent(MainActivity.this, SecondActivity.class);
startActivity(intent);
二、隐式的Intent
没有明确指定要启动的组件的Intent我们称为隐式的Intent
Intent除了上面的Componet属性外还有Action、Category属性
Action代表Intent所要完成的一个抽象动作,而Category则是动作附加的类别信息。
例如:
package com.example.testintent; import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button; public class MainActivity extends Activity {
Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button)findViewById(R.id.button1);
button.setOnClickListener(new OnClickListener() { @Override
public void onClick(View arg0) {
Intent intent = new Intent();
intent.setAction("com.example.intent.action.TEST_ACTION");
startActivity(intent);
}
});
}
}
manifest文件中配置
<activity
android:name=".SecondActivity">
<intent-filter >
<action android:name="com.example.intent.action.TEST_ACTION"/>
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
</activity>
一个Intent只能指定一个Action属性,可以包含多个Category属性,当程序创建时,默认启动category为DEFAULT的组件。
接下来我们来看看Category属性的用法
public void onClick(View arg0) {
Intent intent = new Intent();
intent.setAction("com.example.intent.action.TEST_ACTION");
intent.addCategory("android.intent.category.TEST_CATEGERY");
startActivity(intent);
}
manifest文件中配置
<activity
android:name=".SecondActivity">
<intent-filter >
<action android:name="com.example.intent.action.TEST_ACTION"/>
<category android:name="android.intent.category.DEFAULT"/>
<category android:name="android.intent.category.TEST_CATEGERY"/>
</intent-filter>
</activity>
可以看出其实是根据Action和Category两个属性共同决定启动哪个Activity的,Category可以有多个,只要满足其中的一个即可。
实际上Intent不仅可以启动我们定义的Activity,也可以启动系统和其他应用的Activity
ACTION_MAIN 应用程序入口ACTION_VIEW 显示指定数据ACTION_ATTACH_DATA 指定某块数据将被附加到其他地方ACTION_EDIT 编辑指定数据ACTION_PICK 从列表中选择某项,并返回所选数据ACTION_CHOOSER 显示一个Activity选择器ACTION_GET_CONTENT 让用户选择数据,并返回所选数据ACTION_DIAL 显示拨号面板ACTION_CALL 直接向指定用户打电话ACTION_SEND 向其他人发送数据ACTION_SENDTO 向其他人发送消息ACTION_ANSWER 应答电话ACTION_INSERT 插入数据ACTION_DELETE 删除数据ACTION_RUN 运行数据ACTION_SYNC 执行数据同步ACTION_PICK_ACTIVITY 用于选择ActivityACTION_SEARCH 执行搜索ACTION_WEB_SEARCH 执行web搜索ACTION_FACTORY_TEST 工厂测试的入口点
这里仅列出部分更多关于Action和Category属性请参阅:http://developer.android.com/reference/android/content/Intent.html
三、一个获取通讯录的实例:
package com.example.testintent; import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.provider.ContactsContract.Contacts;
import android.support.v4.content.CursorLoader;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button; public class MainActivity extends Activity {
private static final String TAG = "MainActivity";
Button button;
final int PICK_CONTACT = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button)findViewById(R.id.button1);
button.setOnClickListener(new OnClickListener() { @Override
public void onClick(View arg0) {
Intent intent = new Intent();
intent.setAction(Intent.ACTION_GET_CONTENT);
intent.setType("vnd.android.cursor.item/phone");
startActivityForResult(intent, PICK_CONTACT);
}
});
} @Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case PICK_CONTACT:
if(resultCode == Activity.RESULT_OK){
//获取返回数据
Uri contactData = data.getData();
CursorLoader cursorLoader = new CursorLoader(this, contactData, null, null, null, null);
Cursor cursor = cursorLoader.loadInBackground();
if(cursor.moveToFirst()){
String contactId = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
String name = cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME));
Log.i(TAG, "姓名:" + name);
//根据联系人查看详细信息
Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = " + contactId, null, null);
if(phones.moveToFirst()){
System.out.println("进来了");
String phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
Log.i(TAG, "电话:" + phoneNumber);
}
//关闭游标
phones.close();
}
//关闭游标
cursor.close();
}
break; default:
break;
}
}
}
Android菜鸟的成长笔记(8)——Intent与Intent Filter(上)的更多相关文章
- Android菜鸟的成长笔记(9)——Intent与Intent Filter(下)
原文:[置顶] Android菜鸟的成长笔记(9)——Intent与Intent Filter(下) 接着上一篇的内容,下面我们再来看看Intent的Data与Type属性. 一.Data属性与Typ ...
- Android菜鸟的成长笔记(10)——使用Bundle在Activity之间传值
原文:[置顶] Android菜鸟的成长笔记(10)——使用Bundle在Activity之间传值 前面我们了解了如何启动一个Activity,一个Activity在启动另外一个Activity的时候 ...
- Android菜鸟的成长笔记(7)——什么是Activity
原文:[置顶] Android菜鸟的成长笔记(7)——什么是Activity 前面我们做了一个小例子,在分析代码的时候我们提到了Activity,那么什么是Activity呢? Activity是An ...
- Android菜鸟的成长笔记(6)——剖析源码学自定义主题Theme
原文:Android菜鸟的成长笔记(6)--剖析源码学自定义主题Theme 还记得在Android菜鸟的成长笔记(3)中我们曾经遇到了一个问题吗?"这个界面和真真的QQ界面还有点不同的就是上 ...
- Android菜鸟的成长笔记(17)—— 再看Android中的Unbounded Service
原文:Android菜鸟的成长笔记(17)-- 再看Android中的Unbounded Service 前面已经写过关于startService(Unbounded Service)的一篇文章:&l ...
- Android菜鸟的成长笔记(3)——给QQ登录界面说So Easy
原文:Android菜鸟的成长笔记(3)--给QQ登录界面说So Easy 上一篇:Android菜鸟的成长笔记(2)--第一个Android应用 我们前面已经做了第一个Android应用程序,虽然有 ...
- Android菜鸟的成长笔记(2)——第一个Android应用
原文:Android菜鸟的成长笔记(2)--第一个Android应用 上一篇:Android菜鸟的成长笔记(1)--Anddroid环境搭建从入门到精通 在上一篇Android菜鸟的成长笔记(1)中我 ...
- Android菜鸟的成长笔记(1)——Android开发环境搭建从入门到精通
原文:Android菜鸟的成长笔记(1)--Android开发环境搭建从入门到精通 今天在博客中看到好多Android的初学者对Android的开发环境的搭建不熟悉而导致不能进行学习,所以我决定自己写 ...
- Android菜鸟的成长笔记(14)—— Android中的状态保存探究(上)
原文:[置顶] Android菜鸟的成长笔记(14)—— Android中的状态保存探究(上) 我们在用手机的时候可能会发现,即使应用被放到后台再返回到前台数据依然保留(比如说我们正在玩游戏,突然电话 ...
随机推荐
- java小练习--获取abc字符串在整个字符串中出现的次数
在下面一行字符串中获取abc字符串在整个字符串中出现的次数. "wabcerabctyabcuiabcabcqq" 思路:使用indexOf和substring(); 源码如下: ...
- Office 2010 垃圾邮件过滤设置
垃圾邮件过滤设置 有同事反馈给我,某些时候应该收到的邮件,却到了垃圾邮件里,给工作带来了不便,下面简单介绍一下outlook 2010 有关垃圾邮件的过滤设置. 1: 找到相关的邮件,点右键,在”垃圾 ...
- (解决tomcat端口被占用的问题)create[8005]java.net.BindException: Address already in use: JVM_Bind
create[8005]java.net.BindException: Address already in use: JVM_Bind”,原来是Tomcat8005端口被其他进程占用,8005端口是 ...
- DP HDIJ1421 搬宿舍
Problem Description 搬寝室是很累的,xhd深有体会.时间追述2006年7月9号,那天xhd迫于无奈要从27号楼搬到3号楼,因为10号要封楼了.看着寝室里的n件物品,xhd开始发呆, ...
- 前端SEO优化
结构优化 1.扁平化结构,目录层次越少越好
- 基于Qt的FreeType字体轮廓解析
一.本文目的 以前的文档中.详细的介绍了FreeType开源字体引擎库的基础知识.基本用法.但并未详细的阐明在TurboCG中.是如何解析出一个文字的轮廓的,本文集中阐述.怎么样使用FreeType开 ...
- 集群安装配置Hadoop具体图解
集群安装配置Hadoop 集群节点:node4.node5.node6.node7.node8. 详细架构: node4 Namenode,secondnamenode,jobtracker node ...
- 利用内容提供者插入sms(装B程序)
1.sms的权限配置 <uses-permission android:name="android.permission.READ_SMS"/> <uses-pe ...
- log4net结构
log4net是.Net下一个非常优秀的开源日志记录组件.log4net记录日志的功能非常强大.它可以将日志分不同的等级,以不同的格式,输出到不同的媒介.其大致分为如下这些模块. Appenders模 ...
- perl中 wx返回的json需要encode_utf8($d);
$count is 9 now not support message 51 Wide character in print at /root/scanwx/lib/synccheck.pm line ...