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机制来协助应用间的交互与通 ...
随机推荐
- Android 系统编译
最近研究了下Android 的编译系统,下面结合编译我们自己的产品 mobot 来对整个编译系统进行必要的介绍,方便大家今 后对默认编译的修改. 先列出几个觉得重要的Make 文件: build/bu ...
- linux下环境变量PS1设置
PS1变量中提示符各项含义: \d :代表日期,格式为weekday month date,例如:Wed Dec 12 \H :完整的主机名称.例如:hostname是debian.linux \ ...
- js框架——angular.js(5)
1. 3种过滤方式 <html> <head> <meta charset='utf-8'> <script src="js/angular.js& ...
- CodeForces 546 D. Soldier and Number Game(素数有关)
Description Two soldiers are playing a game. At the beginning first of them chooses a positive integ ...
- Problem H: 小火山的围棋梦想 多校训练2(小火山专场)
题目链接:http://acm.zzuli.edu.cn/zzuliacm/problem.php?id=1908 题意:如果'.'被'*'围起来,就把'.'变为'*'. 分析:如果是'*'直接输出, ...
- linux自动化构建工具-scons指南
1.scons是linux下的自动构建工具 scons是用Python编写的,使用scons之前需确认是否已经安装了Python.(在系统的命令行中运行python -V或python --versi ...
- EL表达式,保留小数点后两位
你遇到过页面显示小数有9.987870488E9这个吗? 这是因为没有保留小数的原因 有时候用js保留小数很麻烦的时候,可以用EL表达式 <fmt:formatNumber type=" ...
- STL笔记之【map之移除元素】
//---------------------------------------------------------// 移除map中满足条件的元素//----------------------- ...
- android脚步---使用framelayout实现霓虹灯效果
轮换帧布局中7个TextView的背景颜色,会出现上面颜色渐变不断变换. 首先在main.xml文件中进行布局 总体布局为framelayout 中间有7个Textview,代表7种不同的颜色,可以看 ...
- Chapter 1 First Sight——6
"You didn't need to do that, Dad. I was going to buy myself a car." 你不需要这样,父亲,我会自己买一辆车的 &q ...