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机制来协助应用间的交互与通 ...
随机推荐
- Loadrunner VuGen实战---基本组成、录制流程、协议、脚本优化、参数化(三)
一.3大基本组件:VuGen.Controller.Analysis 1.VuGen:录制.编写脚本. 2.Controller:性能测试场景设计以及监控的地方. 3.Analysis:生成图表报告的 ...
- [转]于Fragment和Activity之间onCreateOptionsMenu的问题
Fragment和Activity一样,可以重写onCreateOptionsMenu方法来设定自己的菜单,其实这两个地方使用onCreateOptionsMenu的目的和效果都是完全一样的,但是由于 ...
- linux shell 远程执行命令
经常要部署多台服务器上面的应用,如果一个个机器的登录太麻烦. 所有就想到编写一个脚本来部署不同的服务器 前提条件: 配置ssh免登陆 如果不会的请参加我的另外一篇文章 http://blog.csdn ...
- Hibernate锁机制
业务逻辑的实现过程中,往往需要保证数据访问的排他性.因此,我们就需要通过一些机制来保证这些数据在某个操作过程中不会被外界修改,这样的机制,在这里,也就是所谓的“锁”,即给我们选定的目标数据上锁,使其无 ...
- 编写MR代码中,JAVA注意事项
在编写一个job的过程中,发现代码中抛出 java.lang.UnsupportedOperationException 异常. 编写相似逻辑的测试代码: String[] userid = {&qu ...
- asp.net C# 题目大全
net001在线饰品销售系统 net002鲜花商城 net003商品销售管理系统 net004在线辅导答疑 net005土地税务管理系统 net006旅游管理 net007房产中介 net008房产信 ...
- hdu_5179_beautiful number(数位DP)
题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=5179 题意:给你一个范围,问你漂亮的数有多少个,漂亮的数的定义为 数位高的比数位低的大,并且 数位高的 ...
- 第19章 网络通信----TCP程序设计基础
TCP网络程序设计是指利用Socket类编写通信程序.利用TCP协议进行通信的两个应用程序是有主次之分的,一个称为服务器程序,另一个称为客户机程序,两者的功能和编写方法大不一样. 1.InetAddr ...
- C#调用C++动态库时类型转换
因为本人主要从事c#开发,但是在工作中经常需要用到c++编写的DLL,因此需要知道c++中的类型与c#中的类型是如何转换的.搜集整理如下. //C++中的DLL函数原型为 //extern &qu ...
- BASE64-客户端(js)加码-服务器端(c#)解码,URL传递“汉字参数”解决方法
HttpUtility.UrlEncode 在 Encode 的时候, 将空格转换成加号('+'), 在 Decode 的时候将加号转为空格, 但是浏览器是不能理解加号为空格的, 所以如果文件名包含了 ...