Intent的概念及应用(一)
------siwuxie095
1、显式Intent
(1)先创建一个项目:LearnIntent,选择API:21 Android 5.0,
选择Empty Activity,完成
(2)创建一个类:MyAty,让它继承自Activity,再绑定一个新创建的
视图:myaty,最后在AndroidManifest.xml中,对Intent进行配置
(3)工程结构目录一览:

(4)为layout中的
myaty.xml,添加一个TextView 和 Button
|
<?xml <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:text="TextView" android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/textView2" <Button android:textAllCaps="false" android:text="Button" android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/button" </LinearLayout> |
(5)在
MyAty.java 中,重写onCreate()函数,绑定视图 myaty
|
package com.siwuxie095.learnintent; import android.app.Activity; import android.os.Bundle; /** * Created by siwux on 2017/1/6. */ public class MyAty extends Activity { @Override protected super.onCreate(savedInstanceState); setContentView(R.layout.myaty); } } |
(6)在layout的 activity_main.xml中,添加一个Button,用于启动Activity:MyAty
|
<?xml <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.siwuxie095.learnintent.MainActivity"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Hello World!" android:id="@+id/textView" <Button android:textAllCaps="false" android:text="Start MyAty" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/textView" android:layout_alignParentStart="true" android:layout_marginStart="57dp" android:layout_marginTop="57dp" android:id="@+id/btnStartMyAty" </RelativeLayout> |
(7)在AndroidManifest.xml中,注册Activity:MyAty,即添加:
|
<activity android:name=".MyAty"/> |
其中
name 属性为: .MyAty,之所以前面加一个点号,是因为程序在运行时,
会把 name 属性和package相连接,即 com.siwuxie095.learnintent.MyAty,
作为当前Activity:MyAty的类定义的全路径(全路径可以代替 .MyAty 的写法)
AndroidManifest.xml:
|
<?xml <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.siwuxie095.learnintent"> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" <category android:name="android.intent.category.LAUNCHER" </intent-filter> </activity> <activity android:name=".MyAty"/> </application> </manifest> |
(8)最后在
MainActivity.java 中,加入启动 MyAty 的代码,注意:
这里直接指明了被启动的类的定义,就是显式Intent
|
package com.siwuxie095.learnintent; import android.content.Intent; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; public class MainActivity extends AppCompatActivity { @Override protected super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); findViewById(R.id.btnStartMyAty).setOnClickListener(new View.OnClickListener() { @Override public //通过这种用方式所创建的Intent,是显式的Intent, // 因为这里直接指明了被启动的类的定义 //而隐式Intent,就不会指明被启动的类的定义, // 而是通过其他方式查找到这个Activity(一种系统的筛选机制) startActivity(new Intent(MainActivity.this,MyAty.class)); } }); } } |
2、隐式Intent
创建了一个Intent,却并不指定被启动的Activity的类的定义
(1)启动的最常用的方式就是在 AndroidManifest.xml 中,配置一个 action
首先在MyAty的activity下添加子标签:intent-filter,再在 intent-filter下添加 category 和 action,
category 的 name 配置为默认项,即:
|
<category android:name="android.intent.category.DEFAULT"/> |
当指明一个
intent-filter 的 category 为 DEFAULT时,表明这个intent-filter的行为方式是一个activity
而 action 的 name 则可以为任意字符串,在启动时直接根据设定的字符串启动即可
如:将字符串设置为 siwuxie095
|
<action android:name="siwuxie095"/> |
启动时同样设置为 siwuxie095
|
startActivity(new Intent("siwuxie095")); |
运行无误
虽然可以采用任意字符串,但为了更好的区分程序,应该采用约定的格式:
package名+intent+action+指定的类名
这里是:
|
<action android:name="com.siwuxie095.learnintent.intent.action.MyAty"/> |
看到此action就能知道启动的是哪个类
启动时的设置:
|
startActivity(new Intent("com.siwuxie095.learnintent.intent.action.MyAty")); |
运行无误
显然这个格式虽然已经约定俗成,但依然难记,那么还可以简化:一般会在被启动的Activity中
添加一个字符串类型的静态常量,如下:
|
public static final String ACTION="com.siwuxie095.learnintent.intent.action.MyAty"; |
MyAty.java:
|
package com.siwuxie095.learnintent; import android.app.Activity; import android.os.Bundle; /** * Created by siwux on 2017/1/6. */ public class MyAty extends Activity { public static final String ACTION="com.siwuxie095.learnintent.intent.action.MyAty"; @Override protected super.onCreate(savedInstanceState); setContentView(R.layout.myaty); } } |
启动时:通过类名访问这个字符串常量
|
startActivity(new Intent(MyAty.ACTION)); |
知道一个
ACTION 相比于
显式Intent中通过类的定义启动,优势在于可以启动其他应用程序里的
Activity。如:从 A应用 启动 B应用 里的一个Activity,A应用 不可能获取到 B应用 里指定Activity
的类的定义,而通过 action 可以做到
跨应用启动Activity:
首先新创建一个应用程序
App1:(用于启动MyAty)
File->New->New Module->Phone & Tablet Module->Application name
->Empty Activity
(1)

(2)

(3)


(4)

(5)

(6)同一工程下出现
app1

(7)切换运行目标

在layout中,添加一个Button,修改其 text 和 id 属性,activity_main.xml如下:
|
<?xml <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.siwuxie095.app1.MainActivity"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Hello World!" android:id="@+id/textView" <Button android:textAllCaps="false" android:text="Start MyAty from App1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/textView" android:layout_toEndOf="@+id/textView" android:layout_marginTop="81dp" android:id="@+id/btnStartMyAty" </RelativeLayout> |
MainActivity.java中,直接通过 action 就能启动MyAty:
|
package com.siwuxie095.app1; import android.content.Intent; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; public class MainActivity extends AppCompatActivity { @Override protected super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); findViewById(R.id.btnStartMyAty).setOnClickListener(new View.OnClickListener() { @Override public startActivity(new Intent("com.siwuxie095.learnintent.intent.action.MyAty")); } }); } } |
运行无误
如果想要一个Activity只允许在同一应用内部访问,不允许其他应用访问,配置方法:
只需要在AndroidManifest.xml中的activity标签中添加属性 exported 为 false,
即不被导出,只能在同一应用内部访问。默认情况下是
true,可以被其他应用访问。
这时,打开App1,点击 Start MyAty From App1,程序会闪退,即报错:
指定Activity没有被导出,并抛出异常:SecurityException

解决:在App1的MainActivity.java中添加一个try…catch语句即可
|
findViewById(R.id.btnStartMyAty).setOnClickListener(new View.OnClickListener() { @Override public try{ startActivity(new Intent("com.siwuxie095.learnintent.intent.action.MyAty")); }catch (Exception e){ //提示信息 LENGTH_SHORT 短时呈现 Toast.makeText(MainActivity.this,"无法启动指定的Activity",Toast.LENGTH_SHORT).show(); } } }); |
这时就不会闪退,且会提示

【made by siwuxie095】
Intent的概念及应用(一)的更多相关文章
- 【Android】12.1 Intent基本概念
分类:C#.Android.VS2015: 创建日期:2016-02-23 一.简介 Intent:意图,含义就是你想利用它调用哪个组件实现相关的功能,比如调用相机组件实现拍照.调用Contact组件 ...
- 在Android中Intent的概念及应用(二)——Intent过滤器相关选项
一.如果多个Activity拥有同一个Intent Action,启动时用同一个Action启动会是什么情况? 如何指定某一个Activity启动? 在多个Activity拥有同一个Intent Ac ...
- 在Android中Intent的概念及应用(一)——显示Intent和隐式Intent
Intent寻找目标组件的两种方式: 显式Intent:通过指定Intent组件名称来实现的,它一般用在知道目标组件名称的前提下,一般是在相同的应用程序内部实现的. 隐式Intent:通过Intent ...
- 在 Android 中 Intent 的概念及应用
一.显式Intent: startActivity(new Intent(MainActivity.this, 类名.class)); 二.隐式Intent: 1.在AndroidManiFest ...
- Intent的概念及应用(二)
------siwuxie095 1.Intent过滤器 intent-filter 相关选项 如果多个Activity拥有同样的action,在启动时这个action时的情况: 首先在LearnIn ...
- Android --差缺补漏之 Intent&putExtra()
伴随着Android Developers 的开发,再也不用FQ了,这意味着Android 对中国学习者有着越来越多的官方学习资料,学习起来有更明确的方向和目标. Android Developer ...
- 第十章:Intent详解
[正文] Intent组件虽然不是四大组件,但却是连接四大组件的桥梁,学习好这个知识,也非常的重要. 一.什么是Intent 1.Intent的概念: Android中提供了Intent机制来协助应用 ...
- Android学习之 Intent详解
一. Intent 作用 Intent 是一个将要执行的动作的抽象的描述,一般来说是作为参数来使用,由Intent来协助完成android各个组件之间的通讯.比如说调用startActivity()来 ...
- Intent详解
ntent组件虽然不是四大组件,但却是连接四大组件的桥梁,学习好这个知识,也非常的重要. 一.什么是Intent 1.Intent的概念: Android中提供了Intent机制来协助应用间的交互与通 ...
随机推荐
- linux系统定时重启tomcat
#touch auto-start.sh [root@Linux opt]# echo $LANGen_US.UTF-8 #vim auto-start.sh #!/bin/sh export LAN ...
- AI 人工智能 探索 (九)
链接:http://pan.baidu.com/s/1c0AM3g0 密码:uccw 今天补充 创建物体 移动物体 ,当点击创建后 ,会出来一个 上图的 ui,他跟随 物体,当你把物体拖动到 指定的地 ...
- android 垂直 SeekBar 源代码(VerticalSeekBar)[转]
主要是继承 AbsSeekBar 然后修改下面这些方法 onProgressRefresh() //当进度条数据更新的时候,例如我们拖动滑动条的时候,这个方法被调用 setThumbPos() //这 ...
- Linux学习 -- Shell编程 -- 流程控制
if语句 单分支 if [ 条件判断式 ]; then 程序 fi 或者 if [ 条件判断式 ] then 程序 fi 例子: 双分支 if [ 条件判断式 ] then 程序 else 程序 fi ...
- 转:【WebDriver】封装GET方法来解决页面跳转不稳定的问题
在大多数测试环境中,网络或者测试服务器主机之间并不是永远不出问题的,很多时候一个客户端的一个跳转的请求会因为不稳定的网络或者偶发的其它异常hang死在那里半天不动,直到人工干预动作的出现. ...
- PHP资源类型
在PHP中,我们经常使用到资源类型变量.例如:mysql连接.文件句柄等. 这些变量无法使用标量来表示,那么在Zend内核中是如何将PHP中的资源变量与C语言中的资源衔接的呢? 一.资源变量在PHP中 ...
- 仿360新闻的热搜图片,win8风格随机九宫格布局
360新闻地址:http://sh.qihoo.com/i/ 感觉这效果挺好的,随机九宫格,在不少地方可以用到,就研究了下他的源码,基本原理就是预先定义好几种布局模块,然后根据需要进行拼接,具体代码可 ...
- Linux通过防火墙禁止IP来防止攻击
1. iptables -I INPUT -s 211.1.0.0 -j DROP 禁止211.1.0.0这个IP访问服务器 2. iptables -I INPUT -s 211.1.0.0 -j ...
- SQL求解两个时间差
sql 求解两个时间差 SELECTDATEDIFF( Second, '2009-8-25 12:15:12', '2009-9-1 7:18:20') --返回相差秒数 SELECTDATEDIF ...
- Mac 生产力探究
转载自:http://devtian.me/2015/04/15/about-my-productivity-tool-in-MacOSX/ ##密码管理器 1Password 1Password 是 ...