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机制来协助应用间的交互与通 ...
随机推荐
- POJ 1470 Closest Common Ancestors(LCA 最近公共祖先)
其实这是一个裸求LCA的题目,我使用的是离线的Tarjan算法,但是这个题的AC对于我来说却很坎坷……首先是RE,我立马想到数组开小了,然后扩大了数组,MLE了……接着把数组调整适当大小,又交了一发, ...
- Linux学习 -- 权限管理
1 ACL权限 1.1 简介与开启 1.1.1 ACL权限是什么 access control list 访问控制表 解决传统的(owner,group,others)身份不足的情况 可以设置 特定用 ...
- 图片处理中的Dithering技术
话说二战的时候,美国轰炸机每次执行任务,除了满载着威力强大的炸弹以外,还常常要装配一台计算机,飞机飞行方向和投弹的抛物线的计算都离不开这台机器.可是世界上第一台电子计算机在二战结束后才发明,轰炸机上当 ...
- 编译filezilla
编译zilla的时候,需要用到与mysql连接的地方(这里先忽略zila的编译) VC听过mysql connector c++, 下载了1.1.3版本,然后飞安装包,之后从官网上下载boost 把库 ...
- The Accumulation of Capital
The Accumulation of Capital ---- by *Adam Smith Capitals are increased by parsimony, and diminished ...
- 最小生成树Prim
首先解释什么是最小生成树,最小生成树是指在一张图中找出一棵树,任意两点的距离已经是最短的了. 算法要点: 1.用book数组存放访问过的节点. 2.用dis数组保存对应下标的点到树的最近距离,这里要注 ...
- 判断iPhone/android手机
JS判断请求来自Android手机还是iPhone手机,根据不同的手机跳转到不同的链接. var browser = {versions: function () {var u = navigator ...
- jquery控制audio的播放与暂停
<audio id="audio" src='music.mp3'></audio> <script type="type/javascri ...
- IPSec VPN实验
IPSec VPN实验 实验拓扑: 实验目的:掌握IPSec VPN原理 掌握site-to-site VPN配置 IPSec配置参数: IKE policy isakmp key 转换集 加密算法 ...
- Android应用性能测试之CPU和内存占用
最近发现自己学的很多东西没有做好积淀的工作,也萌生了写一些东西的念头.本人也没有写博客的习惯,下边就写一下手机端的性能测试. 最近公司,要我们从事对竞品的性能测试,我负责CPU和内存的性能测试,下面就 ...