(三)Android中Intent概念及应用
一、显示Intent
startActivity(new Intent(MainActivity.this,BAty.class));
显示Intent直接指定要启动的Intent类
注意自己通过创建一个java类,然后让其继承Activity时,只需在该类中添加onCreate重载函数,然后在其中设置setContentView(R.layout."自定义的xml布局文件")
package com.example.shiyanshi.learnintent; import android.app.Activity;
import android.os.Bundle; /**
* Created by shiyanshi on 2015/12/28.
*/
public class MyAty extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
}
}
除此之外还要在AndroidManifest.xml中的application中添加<activity android:name=".MyAty"/>
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.shiyanshi.learnintent" > <application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<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>
二、隐式Intent
通过在AndroidManifest.xml文件中指定<intent-filter>的action和category,然后在startActivity设置new Intent(“com.example.shiyanshi.learnintent.MyAty”).注意此处的字符串的名字要与action属性中name中的字符串一样。
通过这种隐式的Intent,可以在另外的一个app中调用这个app定义的activity,注意若不想让当前的activity被其他应用程序调用只需设置<activity android:name=".MyAty" android:exported="false">acitivity的exported属性为false(默认为真)。
可以通过try-catch语句捕获异常信息,并使用Toast.makeText(……).show();进行显示出来
try {
startActivity(new Intent("com.example.shiyanshi.learnintent.MyAty"));
}catch (Exception e){
Toast.makeText(MainActivity.this,"无法启动指定的Activity",Toast.LENGTH_SHORT).show();
}
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.shiyanshi.learnintent" > <application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".MyAty">
<intent-filter>
<action android:name="com.example.shiyanshi.learnintent.MyAty"/>
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
</activity>
</application> </manifest>
三、Intent过滤器相关选项
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.shiyanshi.learnintent" > <application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".MyAty"
android:label="MyAty">
<intent-filter>
<action android:name="com.example.shiyanshi.learnintent.MyAty" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name=".MyAty1"
android:label="@string/title_activity_my_aty1" >
<intent-filter>
<action android:name="com.example.shiyanshi.learnintent.MyAty"/>
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
</activity>
</application> </manifest>
当新建的两个不同的Activity的intent-filter的action属性中的name为同一个值时,仍然可以通过上述startActivity(new Intent("com.example.shiyanshi.learnintent.MyAty"));方式启动,
但是在应用程序启动后会有一个选择具体要启动哪个Activity的对话框,可选择一次或总是,选择总是后会在被调用的应用程序中设定默认信息,下次启动时便不会再提问,可以拖动app到的应用信息上,清除其中的默认设置。
此外还可以指定data属性,如下所示。这样在启动Activity时可以使用startActivity(new Intent("com.example.shiyanshi.learnintent.MyAty", Uri.parse("app://hello")));其中app://是指协议,后面紧跟的参数可以任意设置。
<activity
android:name=".MyAty1"
android:label="@string/title_activity_my_aty1" >
<intent-filter>
<action android:name="com.example.shiyanshi.learnintent.MyAty"/>
<category android:name="android.intent.category.DEFAULT"/>
<data android:scheme="app"/>
</intent-filter>
</activity>
四、通过浏览器启动Activity
若想Activity被浏览器启动需设置红色标注的部分,在浏览器开发中href设置为“app://hello"(其中hello为任意设置的参数)。
<activity
android:name=".LocalActivity"
android:label="@string/title_activity_local" >
<intent-filter>
<category android:name="ANDROID.INTENT.CATEGORY.BROWSABLE"/>
<category android:name="android.intent.category.DEFAULT"/>
<action android:name="android.intent.action.VIEW"/>
<data android:scheme="app"/>
</intent-filter>
</activity>
若想获取浏览器的返回数据可以设置Uri uri=getIntent().getData();来返回一个Uri对象。
(三)Android中Intent概念及应用的更多相关文章
- Android中Intent传递对象的两种方法(Serializable,Parcelable)
今天要给大家讲一下Android中 Intent中如何传递对象,就我目前所知道的有两种方法,一种是Bundle.putSerializable(Key,Object);另一种是 Bundle.putP ...
- Android中Intent传值与Bundle传值的区别详解
Android中Intent传值与Bundle传值的区别详解 举个例子我现在要从A界面跳转到B界面或者C界面 这样的话 我就需要写2个Intent如果你还要涉及的传值的话 你的Intent就要写两 ...
- [转]Android中Intent传递对象的两种方法(Serializable,Parcelable)
http://blog.csdn.net/xyz_lmn/article/details/5908355 今天要给大家讲一下Android中Intent中如何传递对象,就我目前所知道的有两种方法,一种 ...
- Android中intent如何传递自定义数据类型
转载自:http://www.cnblogs.com/GoAhead/archive/2012/07/16/2593868.html 大家好,好久不见,今天要给大家讲一下Android中Intent中 ...
- Android高手进阶教程(十七)之---Android中Intent传递对象的两种方法(Serializable,Parcelable)!
[转][原文] 大家好,好久不见,今天要给大家讲一下Android中Intent中如何传递对象,就我目前所知道的有两种方法,一种是Bundle.putSerializable(Key,Object); ...
- Android中Intent组件详解
Intent是不同组件之间相互通讯的纽带,封装了不同组件之间通讯的条件.Intent本身是定义为一个类别(Class),一个Intent对象表达一个目的(Goal)或期望(Expectation),叙 ...
- Android中Intent的用法总结
Intent只在Android中特有,我把它比作一种运载工具,就像飞机一样,会把一些人带到某个地方,而且如果需要的话,还可以找到机上有哪些人员(数据),这就需要另外一些设备来支持(如:Bundle), ...
- 【转】Android中intent传递对象和Bundle的用法
原文网址:http://blog.csdn.net/lixiang0522/article/details/8642202 android中的组件间传递的对象一般实现Parcelable接口,当然也可 ...
- Android中Intent具体解释(二)之使用Intent广播事件及Broadcast Receiver简单介绍
通过第一篇的解说,我们已经看到了怎样使用Intent来启动新的应用程序组件,可是实际上他们也能够使用sendBroadcast方法来在组件间匿名的广播消息. 作为一个系统级别的消息传递机制,Inten ...
随机推荐
- c# 高斯模糊
using System; using System.Collections.Generic; using System.Text; using System.Drawing; using Syste ...
- Android窗口管理服务WindowManagerService的简要介绍和学习计划
在前一个系列文章中,我们从个体的角度来分析了Android应用程序窗口的实现框架.事实上,如果我们从整体的角度来看,Android应用程序窗口的 实现要更复杂,因为它们的类型和作用不同,且会相互影响. ...
- hibernate报错:org.hibernate.MappingException: No Dialect mapping for JDBC type: -1
解决方法:自定义一个Hibernate Dialect. package com.yourcompany.util ; import java.sql.Types; import org.hibern ...
- 实现两个select list box间item的移动和过滤
<head> <title> </title> <!--Standard jQuery --> <script type="text/j ...
- javascript 高级程序设计学习笔记(面向对象的程序设计)继承
ECMAScript中描述了原型链的概念,原型链是实现继承的主要方法. 实现原型链继承有一种基本模式 function SuperType () { this.property = true; } S ...
- 设计模式之桥接模式(Bridge)--结构模型
1.意图 将抽象部分与它的实现部分分离,使它们可以独立地变化. 2.适用性 你不希望在抽象和它的实现部分之间有一个固定的绑定关系. 类的抽象与它的实现都应该可以通过子类的方式加以扩展. 抽象部分与实现 ...
- Linux下MySql出现#1036 – Table ‘ ‘ is read only 错误解决方法
本文为转载内容,感谢原作者.原文出自:http://zhaoxiaoru39.blog.163.com/blog/static/609552192012511104730115/ 我遇到的问题是:在n ...
- 重读LPTHW-Lesson25
1.ex25接触了字符串函数split().列表函数sorted().pop()函数,整理一下其用法以及其他常见的字符串.列表操作函数如下. (1)split()函数 split()是拆分字符串函数, ...
- ios显示手机信息
NSString *strname=[[UIDevice currentDevice] name]; NSLog(@"设备名:%@",strname); NSString *str ...
- MySQL冷备份的跨操作系统还原
数据来源:linux平台mysql版本为5.7 数据去向:windows平台mysql版本为5.7 操作步骤: 第一步:关闭mysql服务 service mysqld stop 第二步:归档linu ...