【转】Pro Android学习笔记(十):了解Intent(上)
Android引入了Intent的概念来唤起components,component包括:
1、Activity(UI元件)
2、Service(后台代码)
3、Broadcast receiver(处理广播消息的代码)
4、Content provider(抽象数据的代码)
Intent基本含义
intent是通知平台处理(唤起)的动作。Android唤起的动作将取决于注册了什么动作。例如我们有个简单的Activity:IntentBaiscViewActivity。在AndroidManifest中,我们注册该动作,允许其他应用唤起该Activity。
<activity android:name=".IntentBasicViewActivity" android:label="@string/intent_basic_test">
<intent-filter>
<!-- 注册的action的名字为.intent.action.YOUR_ACTION_NAME。(试验:如果不采用package-name,调用时会出现找不到Activity的异常) -->
<action android:name="cn.xxxxxxx.android.pro.intent.action.TestIntentBasicView"/>
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
</activity>
采用以下方式进行调用:
String actionName = "cn.xxxxxxx.android.pro.intent.action.TestIntentBasicView";
Intent i = new Intent(actionName);
showInfo("invoke intent : \n\t" + i);
this.startActivity(i);
在被唤起的IntentBaiscViewActivity,可以找到唤起他的intent对象,如下:
Intent i = this.getIntent();
showInfo("invoke intent : \n\t" + i.toString() );
showInfo()是在textView中显示信息,本例子如下

系统的Intent
可以通过intent来调用Android系统的某些应用,具体的使用方式见http://developer.android.com/guide/appendix/g-app-intents.html。下面的小例子将试验这些调用。在这个例子中使用OptionMenu来选择具体的调用应用。Android的layout资源下有一个menu目录,我们在之下建议一个XML文件,也列举menu的group和item,当然我们也可以在代码中编写(可参考:Android学习笔记(八):Activity-OpenMenu和LinearLayout)。
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<group android:id="@+id/intentMenu_MAIN">
<item android:id="@+id/intent_menu_browser" android:title="@string/intent_menu_browser" />
<item android:id="@+id/intent_menu_search" android:title="@string/intent_menu_search" />
<item android:id="@+id/intent_menu_dial" android:title="@string/intent_menu_dial" />
<item android:id="@+id/intent_menu_call" android:title="@string/intent_menu_call" />
<item android:id="@+id/intent_menu_map" android:title="@string/intent_menu_map" />
</group>
</menu>
Java代码如下:
public class IntentTestDemo extends Activity{
… …
protected void onCreate(Bundle savedInstanceState) {
……
}
//【Menu】1、创建OptionMenu需要重写onCreateOptionsMenu,再此通过MenuInflater从xml资源获取信息并构建菜单
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
MenuInflater mi = getMenuInflater();
mi.inflate(R.menu.intent_test_menu, menu);
return true;
}
// 【Menu】2、选择用户选择某菜单后触发onOptionsItemtSelected(),本例我们根据不同选择,出发不同的系统应用。
public boolean onOptionsItemSelected(MenuItem item) {
try{
switch(item.getItemId()){
case R.id.intent_menu_browser: //对应xml中的id
invokeBrowser();
break;
case R.id.intent_menu_search:
invokeSearch();
break;
case R.id.intent_menu_dial:
invokeDial();
break;
case R.id.intent_menu_call:
invokeCall();
break;
case R.id.intent_menu_map:
invokeMap();
break;
default:
Log.d("pro","get option error.");
break;
}
}catch(Exception e){
Log.d("pro",e.toString());
}
return true;
}
//【intent】触发浏览器打开指定网页,对于ACTION_VIEW,系统将根据传递的URI的格式,调用不同的应用,http://,https://时调用browser
private void invokeBrowser(){
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse("http://www.google.com.hk"));
startActivity(intent);
}
//【intent】实际也是触发浏览器,不过名曰网络搜索,和上面ACTION_VIEW不同,运行传递的data为””(empty string),因为ACTION_VIEW对应多个可能的应用,而WEB_SEARCH只对应browser。
private void invokeSearch(){
Intent intent = new Intent(Intent.ACTION_WEB_SEARCH);
intent.setData(Uri.parse("http://www.google.com.hk"));
startActivity(intent);
}
//【intent】打开拨号盘UI,可以预设tel:phone_number,或者voicemail:xxxxx。
private void invokeDial(){
Intent intent = new Intent(Intent.ACTION_DIAL);
startActivity(intent);
}
//【intent】打开拨号盘UI,同时呼出tel:phone_number,和ACTION_DIAL的需要用户按拨打键不同,ACTION_CALL直接呼出,需要授权android.permission_CALL_PHONE
private void invokeCall(){
Intent intent = new Intent(Intent.ACTION_CALL);
intent.setData(Uri.parse("tel:02012345678"));
startActivity(intent);
}
//【intent】给出地理位置,ACTION_VIEW选择并触发Google Map,前提是已经安装了Google Map。之前我们在模拟器上安装了华为的智慧云,有个应用商店比较方便,可以从那里直接安装。此外如果URI为google.streetview:cbll=lat,lng&cbp=1,yaw,,pitch,zoom&mz=mapZoom,VIEW将调用Google Streetview。
private void invokeMap(){
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse("geo:23.0,123.0"));
//intent.setData(Uri.parse("geo:23.0,123.0?z=4&q=business+near+city")); //增加zoom等参数
startActivity(intent);
}
}
相关链接: 我的Android开发相关文章
转自http://blog.csdn.net/flowingflying/article/details/9270557
【转】Pro Android学习笔记(十):了解Intent(上)的更多相关文章
- 【转】Pro Android学习笔记(三十):Menu(1):了解Menu
目录(?)[-] 创建Menu MenuItem的属性itemId MenuItem的属性groupId MenuItem的属性orderId MenuItem的属性可选属性 Menu触发 onOpt ...
- 【转】 Pro Android学习笔记(十九):用户界面和控制(7):ListView
目录(?)[-] 点击List的item触发 添加其他控件以及获取item数据 ListView控件以垂直布局方式显示子view.系统的android.app.ListActivity已经实现了一个只 ...
- 【转】 Pro Android学习笔记(四十):Fragment(5):适应不同屏幕或排版
目录(?)[-] 设置横排和竖排的不同排版风格 改写代码 对于fragment,经常涉及不同屏幕尺寸和不同的排版风格.我们在基础小例子上做一下改动,在横排的时候,仍是现实左右两个fragment,在竖 ...
- 【转】 Pro Android学习笔记(三五):Menu(6):XML方式 & PopUp菜单
目录(?)[-] 利用XML创建菜单 XML的有关属性 onClick事件 Pop-up菜单 利用XML创建菜单 在代码中对每个菜单项进行设置,繁琐且修改不灵活,不能适配多国语言的要求,可以利用资源进 ...
- 【转】 Pro Android学习笔记(三三):Menu(4):Alternative菜单
目录(?)[-] 什么是Alternative menu替代菜单 小例子说明 Alternative menu代码 关于Category和规范代码写法 关于flags 多个匹配的itemId等参数 什 ...
- Pro Android学习笔记 ActionBar(1):Home图标区
Pro Android学习笔记(四八):ActionBar(1):Home图标区 2013年03月10日 ⁄ 综合 ⁄ 共 3256字 ⁄ 字号 小 中 大 ⁄ 评论关闭 ActionBar在A ...
- 【转】 Pro Android学习笔记(六七):HTTP服务(1):HTTP GET
目录(?)[-] HTTP GET小例子 简单小例子 出现异常NetworkOnMainThreadException 通过StrictMode进行处理 URL带键值对 Andriod应用可利用ser ...
- 【转】 Pro Android学习笔记(五二):ActionBar(5):list模式
可以在action bar中加入spinner的下来菜单,有关spinner,可以参考Pro Android学习笔记(二十):用户界面和控制(8):GridView和Spinner. list的样式和 ...
- 【转】Pro Android学习笔记(二五):用户界面和控制(13):LinearLayout和TableLayout
目录(?)[-] 布局Layout 线性布局LinearLayout 表格布局TableLayout 布局Layout Layout是容器,用于对所包含的view进行布局.layout是view的子类 ...
- 【转】 Pro Android学习笔记(二二):用户界面和控制(10):自定义Adapter
目录(?)[-] 设计Adapter的布局 代码部分 Activity的代码 MyAdapter的代码数据源和构造函数 MyAdapter的代码实现自定义的adapter MyAdapter的代码继续 ...
随机推荐
- ASP获取上月本月下月的第一天和最后一天
上月第一天:<%=dateadd("m",-1,year(date)&"-"&month(date)&"-1" ...
- ElasticSearch(二十六)修改分词器及定制自己的分词器
1.默认的分词器 standard 分词器 standard tokenizer:以单词边界进行切分standard token filter:什么都不做lowercase token filter: ...
- [POI2006]SZK-Schools
[POI2006]SZK-Schools luogu #include<bits/stdc++.h> using namespace std; const int N=405,M=1e5+ ...
- PAT 1059. C语言竞赛(20)
C语言竞赛是浙江大学计算机学院主持的一个欢乐的竞赛.既然竞赛主旨是为了好玩,颁奖规则也就制定得很滑稽: 0. 冠军将赢得一份“神秘大奖”(比如很巨大的一本学生研究论文集……). 1. 排名为素数的学生 ...
- Java的接口和抽象类(转发:http://www.importnew.com/18780.html)
深入理解Java的接口和抽象类 对于面向对象编程来说,抽象是它的一大特征之一.在Java中,可以通过两种形式来体现OOP的抽象:接口和抽象类.这两者有太多相似的地方,又有太多不同的地方.很多人在初学的 ...
- Qt Creator 调试器 在 Ubuntu 13.10下 局部变量和表达式(Locals) 无内容
此篇算是一个翻译,万一有国内同样的小白遇到同样问题,方便参考. 原文http://hostilefork.com/2013/10/20/qtcreator-debugger-no-locals-ubu ...
- linux shell 字符串操作(长度,查找,替换)
感谢原创,文章很有帮助. 转自:http://www.cnblogs.com/chengmo/archive/2010/10/02/1841355.html 一.判断读取字符串值 表达式 含义 ${v ...
- 淘宝开源平台(taobao-code)使用
偶尔之下翻到的这个东西,瞬间觉得足以解决自己在开发过程中的版本控制问题.就注册了一个试试.先是在度娘上搜寻“淘code”,进入官网之后直接注册.然后构建自己的项目,上传代码就OK了. 一.搜寻“淘co ...
- Qt之任务栏系统托盘图标
转自 --> http://blog.csdn.net/qivan/article/details/7506306 托盘图标,一个自己脑子出现很久的词,可惜自己都没动手去实现.最近看见的,听见 ...
- GUI创建各常用控件(一)
首先,作个申明: 1.这是一个野路子非科班的小菜鸟的学习,故诚心欢迎批评指正(同时所述内容可能有误): 2.本人目前使用的Unity3D版本为 5.3.5: 言归正传! 事实上在开发过程中已经很少用G ...