Android中Intent的显示和隐式使用
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的显示和隐式使用的更多相关文章
- Intent 显示意图 隐式意图
//显式意图 :必须指定要激活的组件的完整包名和类名 (应用程序之间耦合在一起) // 一般激活自己应用的组件的时候 采用显示意图 //隐式意图: 只需要指定要动作和数据就可以 ( 好处应用程序之 ...
- 为什么在注册和注销的时候intent要改成隐式调用
显式意图:调用Intent.setComponent()或Intent.setClass()方法明确指定了组件名的Intent为显式意图,显式意图明确指定了Intent应该传递给哪个组件. 隐式意图: ...
- Android中Intent具体解释(一)
Intent是一种消息传递机制.它能够在应用程序内使用,也能够在应用程序间使用,主要用途分为: 1.使用类名显示的启动一个特定的Activity或Service 2.启动Activity或Servic ...
- Android中Intent传值与Bundle传值的区别详解
Android中Intent传值与Bundle传值的区别详解 举个例子我现在要从A界面跳转到B界面或者C界面 这样的话 我就需要写2个Intent如果你还要涉及的传值的话 你的Intent就要写两 ...
- Android中Intent传递对象的两种方法(Serializable,Parcelable)
今天要给大家讲一下Android中 Intent中如何传递对象,就我目前所知道的有两种方法,一种是Bundle.putSerializable(Key,Object);另一种是 Bundle.putP ...
- [转]Android中Intent传递对象的两种方法(Serializable,Parcelable)
http://blog.csdn.net/xyz_lmn/article/details/5908355 今天要给大家讲一下Android中Intent中如何传递对象,就我目前所知道的有两种方法,一种 ...
- Android中intent如何传递自定义数据类型
转载自:http://www.cnblogs.com/GoAhead/archive/2012/07/16/2593868.html 大家好,好久不见,今天要给大家讲一下Android中Intent中 ...
- (4.19)sql server中的事务模式(隐式事务,显式事务,自动提交事务)
(4.19)sql server中的事务模式(隐式事务,显式事务,自动提交事务) 1.概念:隐式事务,显式事务,自动提交事务 2.操作:如何设置事务模式 3.存储过程中的事务 XACT_ABORT 1 ...
- Android高手进阶教程(十七)之---Android中Intent传递对象的两种方法(Serializable,Parcelable)!
[转][原文] 大家好,好久不见,今天要给大家讲一下Android中Intent中如何传递对象,就我目前所知道的有两种方法,一种是Bundle.putSerializable(Key,Object); ...
随机推荐
- MySQL性能优化之char、varchar、text的区别
参考来源:https://blog.csdn.net/brycegao321/article/details/78038272 在存储字符串时, 可以使用char.varchar或者text类型, 那 ...
- Adobe PS CS6安装详解
Adobe PS CS6安装破解详解 注:电脑上是否拥有虚拟光驱,若是没有,推荐2345好压:官网http://haozip.2345.com/下载地址:http://dl.2345.com/haoz ...
- Android与GPL、BSD和Apache之间的关系
参考资料 Android ,在争议中逃离 Linux 内核的 GPL 约束 | 爱范儿 简介 众所周知,Linux内核基于GPL v2发行.GPL规定,基于GPL的软件产品的衍生产品,也必须使用GPL ...
- c#程序员机试题
一.题目: 有一数组: int[] arr = new int[] { 48,1,3,55,15,29,12,33,26,41,56,32}; 1.求出最大值 2.按每个数字的10位数分组(说明:0~ ...
- 【SQL】185. Department Top Three Salaries
The Employee table holds all employees. Every employee has an Id, and there is also a column for the ...
- JAVA内存泄漏解决办法
JVM调优工具 Jconsole,jProfile,VisualVM Jconsole : jdk自带,功能简单,但是可以在系统有一定负荷的情况下使用.对垃圾回收算法有很详细的跟踪.详细说明参考这里 ...
- CodeForces1070A Find a Number 图论
令状态$f(i, j)$表示模$d$为$i$,和为$j$时的最小数 可以通过$bfs$来转移 然而就没了... 复杂度$O(10ds)$ #include <queue> #include ...
- nginx安装第三方模块
原已经安装好的nginx,现在需要添加一个未被编译安装的模块 举例说明:安装第三方的ngx_cache_purge模块(用于清除指定URL的缓存) nginx的模块是需要重新编译nginx,而不是像a ...
- Codeforces Round #259 (Div. 1) A. Little Pony and Expected Maximum 数学公式结论找规律水题
A. Little Pony and Expected Maximum Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.c ...
- hrbust 2176 Mac的投票 二分/水题
Mac的投票 Time Limit: 1000 MS Memory Limit: 32768 K Total Submit: 52(12 users) Total Accepted: 12(10 us ...