(三)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 ...
 
随机推荐
- linux下ACE使用epoll
			
select和epoll的比较就不用多说了.ACE在linux下默认使用select来实现Reactor的.如何在linux下让ACE使用epoll. 1.加一个编译宏,告诉ACE不要使用默认的sel ...
 - stagefright框架(三)-选择Video Decode
			
在<Stagefright (1) – Video Playback的流程>中,我们并没有详述Stagefright是如何根据影片档的类型来选择适合的video decoder,现在,就让 ...
 - NET基础课--WinForm开发推荐3
			
用户体验 较长时间的运算:使用进度条(progress bar) 不要阻塞界面(UI)线程:使用多线程进行长时间的运算 状态栏(status bar)提示应用程序的状态 操作开始之后,用户应当能够通过 ...
 - JS中表格的全选和删除要注意的问题
			
在项目开发中,由于刚刚开始做项目,我对js还不是很精通,所以在用js对表格的全选和删除中遇到了不少问题,后来通过查找资料解决了,之后总结了一下关于js表格的全选和删除出现的一些问题,希望能帮助到大家. ...
 - FtpClient的使用
			
摘自:http://hi.baidu.com/yuanhotel/item/000b6334894d11f42784f4da Java的ftp操作 package com.why.ftp; impor ...
 - 定时排程刷新微信access-token
			
微信公众号开发中最常遇到的就是调用接口时候需要有API的access-token(非网页授权的access-token),有了这个token之后,才可以发生模板消息等.这里的做法主要是用nodejs的 ...
 - JavaWeb中filter的详解及应用案例
			
一:Filter介绍 Filter可认为是Servlet的一种“变种”,它主要用于对用户请求(HttpServletRequest)进行预处理,也可以对服务器响应(HttpServletRespons ...
 - js文件代码未加载或者没有js效果
			
问题:在页面中js文件中的代码未加载或者没有任何效果. 原因: 成功引用了js文件,但无效果或者提示未加载该文档中的代码. 可能页面引用js文件的路径存在问题 解决: 重新检查你引用的js文件的路径是 ...
 - python操作csv-xls完善后的代码
			
#coding:utf-8 #导入相应模块 import csv,xlwt,sys,os,fnmatch,xlrd from xlutils.copy import copy #对xls文件中的绝对值 ...
 - Asp.net MVC4 下二级联动
			
效果图: