【转】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的代码继续 ...
随机推荐
- maven3 org.codehaus.plexus.classworlds.launcher.launcher 找不到或无法加载主类
maven3 org.codehaus.plexus.classworlds.launcher.launcher 找不到或无法加载主类 嗯,网上很多资料说是路径的问题,确实是有可能是路径的问题,而且还 ...
- C++, Java和C#的编译、链接过程解析
总是感觉java是解释性语言,转载下一篇感觉写的容易理解的文章 转自 http://www.cnblogs.com/rush/p/3155665.html 1.1.1 摘要 我们知道计算机不能直接理解 ...
- windows10下载
http://care.dlservice.microsoft.com/dl/download/F/5/7/F574727C-B145-4A7D-B85B-11C4E8DC894B/9841.0.14 ...
- 【TFS】解决TFS编译中文乱码问题
前言; TFS2018做程序集成非常方便,线上编译然后直接生成docker镜像,但是在使用过程中遇到编译窗口中文乱码的问题,这个问题找了好久没人知道怎么解决.如下: 这个问题不解决,每次编译失败,研发 ...
- DatagramSocket总是发送UDP数据后无法接收数据
ref:http://blog.chinaunix.net/uid-20771867-id-3416509.html cmd:telnet localhost 5554 ...
- P1393 动态逆序对
题目 P1393 动态逆序对 做题前写篇博客是个好方法 做法 题目规定仅有删除,给每个位置标个号,逆序对+时间轴,显然这是个三维偏序 很久没做过\(cdq\)了,就当模板题讲一下: 按删除的先后顺序为 ...
- 20145229吴姗珊 《Java程序设计》第9周总结
20145229吴姗珊 <Java程序设计>第9周总结 教材学习内容总结 第十六章 整合数据库 JDBC入门 数据库本身是个独立运行的应用程序 撰写应用程序是利用通信协议对数据库进行指令交 ...
- [java]final关键字、finally关键字与finalize()方法
final关键字: final关键字通常指的是“无法改变的”,使用“无法改变”这样修饰可能出于两个原因:设计或者效率. final可以修饰变量.方法和类. 一.final变量 一个既是static又是 ...
- EntityFramework 学习 一 Migration from Entity Framework 4.1/4.3 to Entity Framework 5.0/6.0
To migrate your existing Entity Framework 4.x project to Entity Framework 5.0 using VS2012, first ta ...
- EntityFramework 学习 一 Change Tracking in Entity Framework
EntityFramework自动跟踪上下文中已经加载的实体,DbChangeTracker类给你关于当前实体的所有跟踪信息 注意,每个实体都要有EntityKey(主键)的属性,EntityFram ...