Android应用程序中组件之间的通信都少不了Intent的使用,Intent负责对应用中一次操作的动作、动作涉及数据、附加数据进行描述,Android则根据此Intent的描述,负责找到对应的组件,将 Intent传递给调用的组件,并完成组件的调用。intent就是意图的意思。Intent分两种:显式(Explicit intent)和隐式(Implicit intent)。

显示调用Intent

简单的Demo从一个Activity转到另外一个Aactivity:

Mainactivity的布局文件

    <EditText
android:id="@+id/edt_content"
android:layout_width="match_parent"
android:layout_height="wrap_content" /> <Button
android:id="@+id/btn_login"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/edt_content"
android:onClick="login"
android:text="查询" />

 Mainactivity中调用点击事件:

EditText  contentEditText=(EditText) findViewById(R.id.edt_content);
Intent intent=new Intent(this,PersonActivity.class);
intent.putExtra(EXTRA,contentEditText.getText().toString());
startActivity(intent);

  这个时候的Intent就是显示调用,直接指定了接收参数的Activity,可以唯一确定一个Activity,意图特别明确,这个时候需要在PersonActivity接收参数:

public class PersonActivity extends Activity {

	@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_person);
TextView textView=(TextView) findViewById(R.id.txt_content);
Intent intent=getIntent();
String str=intent.getStringExtra(MainActivity.EXTRA);
textView.setText(str);
textView.setTextSize(20);
textView.setTextColor(Color.RED); }

另外这个时候传递的参数使用的方法是putExtra,如果传递的参数比较多可以使用Bundle类似于map。

隐式调用

隐式,即不是像显式的那样直接指定需要调用的Activity,隐式不明确指定启动哪个Activity,而是设置Action、Data、Category,让系统来筛选出合适的Activity。筛选是根据所有的<intent-filter>来筛选。

这个时候需要在AndroidManifest.xml中设置一下intent-filter中去设置一下,如下,Category直接使用默认的就行:

    <intent-filter>
<action android:name="com.example.googleone.Peson" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>

 Mainactivity中的调用使用,这个时候的调用:

  Intent intent=new  Intent("com.example.googleone.Peson");
startActivity(intent);

 这个自己定义的Action字符串可以调用自身程序的Activity,还可以其他应用程序的Action,比如说常用的拨号面板:

	Intent intent = new Intent(Intent.ACTION_DIAL);
startActivity(intent);

如果这个时候在AndroidManifest.xml文件中给PersonActivity, 加一个Action,如下:

  <activity
android:name=".PersonActivity"
android:label="@string/title_activity_person" >
<intent-filter>
<action android:name="android.intent.action.DIAL"/>
<action android:name="com.example.googleone.Peson" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>

Mainactivity中的调用:

	Intent intent=new Intent(Intent.ACTION_DIAL);
if(intent.resolveActivity(getPackageManager()) == null)
{
view.setEnabled(false);
}
try {
startActivity(intent);
} catch (ActivityNotFoundException e) {
// TODO Auto-generated catch block
Toast.makeText(this,"找不到对应的Activity",Toast.LENGTH_SHORT).show();
}

结果如图所示:

  

Intent.ACTION_DIAL是系统常量字符串,等价于android.intent.action.DIAL,调用的时候通过这个action的名称,去寻找具有这个action的activity~

Android中Intent的显示和隐式使用的更多相关文章

  1. Intent 显示意图 隐式意图

    //显式意图  :必须指定要激活的组件的完整包名和类名 (应用程序之间耦合在一起) // 一般激活自己应用的组件的时候 采用显示意图  //隐式意图: 只需要指定要动作和数据就可以 ( 好处应用程序之 ...

  2. 为什么在注册和注销的时候intent要改成隐式调用

    显式意图:调用Intent.setComponent()或Intent.setClass()方法明确指定了组件名的Intent为显式意图,显式意图明确指定了Intent应该传递给哪个组件. 隐式意图: ...

  3. Android中Intent具体解释(一)

    Intent是一种消息传递机制.它能够在应用程序内使用,也能够在应用程序间使用,主要用途分为: 1.使用类名显示的启动一个特定的Activity或Service 2.启动Activity或Servic ...

  4. Android中Intent传值与Bundle传值的区别详解

    Android中Intent传值与Bundle传值的区别详解 举个例子我现在要从A界面跳转到B界面或者C界面   这样的话 我就需要写2个Intent如果你还要涉及的传值的话 你的Intent就要写两 ...

  5. Android中Intent传递对象的两种方法(Serializable,Parcelable)

    今天要给大家讲一下Android中 Intent中如何传递对象,就我目前所知道的有两种方法,一种是Bundle.putSerializable(Key,Object);另一种是 Bundle.putP ...

  6. [转]Android中Intent传递对象的两种方法(Serializable,Parcelable)

    http://blog.csdn.net/xyz_lmn/article/details/5908355 今天要给大家讲一下Android中Intent中如何传递对象,就我目前所知道的有两种方法,一种 ...

  7. Android中intent如何传递自定义数据类型

    转载自:http://www.cnblogs.com/GoAhead/archive/2012/07/16/2593868.html 大家好,好久不见,今天要给大家讲一下Android中Intent中 ...

  8. (4.19)sql server中的事务模式(隐式事务,显式事务,自动提交事务)

    (4.19)sql server中的事务模式(隐式事务,显式事务,自动提交事务) 1.概念:隐式事务,显式事务,自动提交事务 2.操作:如何设置事务模式 3.存储过程中的事务 XACT_ABORT 1 ...

  9. Android高手进阶教程(十七)之---Android中Intent传递对象的两种方法(Serializable,Parcelable)!

    [转][原文] 大家好,好久不见,今天要给大家讲一下Android中Intent中如何传递对象,就我目前所知道的有两种方法,一种是Bundle.putSerializable(Key,Object); ...

随机推荐

  1. python 与 matlab 混编

    用于 Python 的 MATLAB 引擎 API 快速入门 安装用于 Python 的 MATLAB 引擎 API Matlab的官方文档中介绍了 Matlab 与其余编程语言之间的引擎接口,其中包 ...

  2. JAVAEE——Lucene基础:什么是全文检索、Lucene实现全文检索的流程、配置开发环境、索引库创建与管理

    1. 学习计划 第一天:Lucene的基础知识 1.案例分析:什么是全文检索,如何实现全文检索 2.Lucene实现全文检索的流程 a) 创建索引 b) 查询索引 3.配置开发环境 4.创建索引库 5 ...

  3. Javap -c 字节码解析

              栈和局部变量操作 将常量压入栈的指令 aconst_null         将null对象引用压入栈   iconst_m1         将int类型常量-1压入栈 icon ...

  4. 使用DNSPod域名解析

    1 在GoDaddy域名注册商 注册域名 https://sg.godaddy.com/zh/ 2 登陆DNSPod https://www.dnspod.cn 3 选择域名解析 添加域名 4 添加记 ...

  5. C# 非模式窗体show()和模式窗体showdialog()的区别

    对话框不是模式就是无模式的.模式对话框,在可以继续操作应用程序的其他部分之前,必须被关闭(隐藏或卸载).例如,如果一个对话框,在可以切换到其它窗 体或对话框之前要求先单击"确定"或 ...

  6. redis 客户端命令

    Redis 通过监听一个 TCP 端口或者 Unix socket 的方式来接收来自客户端的连接 1 .CLIENT LIST 返回连接到 redis 服务的客户端列表 2 .CLIENT SETNA ...

  7. QT学习笔记5:QMouseEvent鼠标事件简介

    一.QMouseEvent的详细描述 首先请注意,Qt中的QMouseEvent一般只涉及鼠标左键或右键的单击.释放等操作,而对鼠标滚轮的响应则通过QWheeEvent来处理. QMouseEvent ...

  8. ARC 101 D - Median of Medians

    题面在这里! 这种题只能二分答案把qwwq,直接做根本做不了啊... 首先你需要知道如何通过 一个区间<=x的数有多少个 来判断x和这个区间中位数的关系. 很显然当数有至少 [L/2]+1 个( ...

  9. 【spfa】【动态规划】zoj3847 Collect Chars

    转载自:http://blog.csdn.net/madaidao/article/details/42616743 Collect Chars Time Limit: 2 Seconds       ...

  10. bzoj 2406 二分+有源有汇上下界网络流可行流判定

    弱爆了,典型的行列建模方式,居然想不到,题做少了,总结少了...... 二分答案mid s----------------------->i行-----------------------> ...