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机制来协助应用间的交互与通 ...
随机推荐
- g++ 编译c文件
//编译c文件为.o文件 g++ -c virify.c //打包.o文件为.a静态库文件 ar crv libandroid_um36_virify.a virify.o //将静态库.a文件编译进 ...
- [转]java static final 初始化
http://tanbing1986411.blog.163.com/blog/static/7259798220103610224434/ java static final 初始化 1.stati ...
- elasticsearch 使用事项
template的使用 刚开始的时候,每次实验都去改/etc/elasticsearch/elasticsearch.yml配置文件.事实上在template里修改settings更方便而且灵活!当 ...
- Windows编译安装mod_wsgi,配合使用Django+Apahce
编译环境: 均是32位版本 Microsoft Visual Studio 10.0 Microsoft SDKs v7.1 Apache2.4 Python3.4 mod_wsgi-4.5.0 要求 ...
- CodeForces 546 D. Soldier and Number Game(素数有关)
Description Two soldiers are playing a game. At the beginning first of them chooses a positive integ ...
- [转]solr DataImportHandler 解决mysql 表导入内存溢出问题
最近一个项目要用到solr做全文检索,开始盲人摸象. 用tomcat 7 开始配置,开始正常,但是遇到cookie里有中文就报错. 无奈,换tomcat 6, 结果DataImportHandler ...
- 自定义solr的search UI
solr使用apache的velocity来定义UI,在solr的search ui的基础上更改即可,主要改\example\solr\collection1\conf\velocity里的文件. 详 ...
- 保存iptables的防火墙规则的方法【转载】
转自: 保存iptables的防火墙规则的方法 - 51CTO.COMhttp://os.51cto.com/art/201103/249504.htm 保存iptables的防火墙规则的方法如下: ...
- 解决Android Studio Gradle Build特别慢的问题
解决Android Studio Gradle Build 特别慢的问题 C:\Users\你的用户名\.gradle目录下新建一个文件名为gradle.properties的文件.内容为:org.g ...
- nginx 504 Gateway Time-out 解决办法
今天用PHP执行一个非常耗时的文件[ps:自己有用,大概3分钟] 但是执行到一分钟后显示 nginx 504 Gateway Time-out 于是修改php-ini.php中的max_executi ...