一、显示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概念及应用的更多相关文章

  1. Android中Intent传递对象的两种方法(Serializable,Parcelable)

    今天要给大家讲一下Android中 Intent中如何传递对象,就我目前所知道的有两种方法,一种是Bundle.putSerializable(Key,Object);另一种是 Bundle.putP ...

  2. Android中Intent传值与Bundle传值的区别详解

    Android中Intent传值与Bundle传值的区别详解 举个例子我现在要从A界面跳转到B界面或者C界面   这样的话 我就需要写2个Intent如果你还要涉及的传值的话 你的Intent就要写两 ...

  3. [转]Android中Intent传递对象的两种方法(Serializable,Parcelable)

    http://blog.csdn.net/xyz_lmn/article/details/5908355 今天要给大家讲一下Android中Intent中如何传递对象,就我目前所知道的有两种方法,一种 ...

  4. Android中intent如何传递自定义数据类型

    转载自:http://www.cnblogs.com/GoAhead/archive/2012/07/16/2593868.html 大家好,好久不见,今天要给大家讲一下Android中Intent中 ...

  5. Android高手进阶教程(十七)之---Android中Intent传递对象的两种方法(Serializable,Parcelable)!

    [转][原文] 大家好,好久不见,今天要给大家讲一下Android中Intent中如何传递对象,就我目前所知道的有两种方法,一种是Bundle.putSerializable(Key,Object); ...

  6. Android中Intent组件详解

    Intent是不同组件之间相互通讯的纽带,封装了不同组件之间通讯的条件.Intent本身是定义为一个类别(Class),一个Intent对象表达一个目的(Goal)或期望(Expectation),叙 ...

  7. Android中Intent的用法总结

    Intent只在Android中特有,我把它比作一种运载工具,就像飞机一样,会把一些人带到某个地方,而且如果需要的话,还可以找到机上有哪些人员(数据),这就需要另外一些设备来支持(如:Bundle), ...

  8. 【转】Android中intent传递对象和Bundle的用法

    原文网址:http://blog.csdn.net/lixiang0522/article/details/8642202 android中的组件间传递的对象一般实现Parcelable接口,当然也可 ...

  9. Android中Intent具体解释(二)之使用Intent广播事件及Broadcast Receiver简单介绍

    通过第一篇的解说,我们已经看到了怎样使用Intent来启动新的应用程序组件,可是实际上他们也能够使用sendBroadcast方法来在组件间匿名的广播消息. 作为一个系统级别的消息传递机制,Inten ...

随机推荐

  1. ubuntu WiFi: operation not possible due to RF-kill《转载》

    Some people have been experiencing WiFi problems with Ubuntu 10.10 since an update that happend just ...

  2. C#关于ref的用法(多个实参值的传递)

    按照C#默认的按值调用参数的传递机制,不能刻编写出一个方法来实现两个int类型的值交换,因为一个方法只能对应一个返回值,如何实现将两个交换的值传递回去,这里我将用到的是ref修饰符. 使用ref的单值 ...

  3. 为net-snmp添加读readTimeTicks

    function readTimeTicks(time){ if(time === 0) return ''; var d = 0, h = 0, m = 0, s = 0; d = parseInt ...

  4. 将json格式日期(毫秒数)转成日常日期格式和日常格式时间对比

    第一:是把生成的Json格式的时间转换,注意要看清楚时间的格式 function (cellval) { var date = new Date(parseInt(cellval.replace(&q ...

  5. 用户"IIS APPPOOL\xxxxxxxx"登录失败解决方案

    Server Error in '/' Application.-------------------------------------------------------------------- ...

  6. dir_colors linux颜色配置

    在控制台下,用ls,就会发现,shell将不同类型的文件项目显示为不同的颜色.者可以提高效率,不用ls -l便能大概的把各个文件的类型情况了解一下. 你有没有想过更改这个着色配置呢? 其实,在/etc ...

  7. Spring Annotation注解进行aop的学习

    使用Maven管理项目,pom文件为: <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi=" ...

  8. USACO 1.4 ariprog 解题报告

    这是继虫洞之后又让我为难的一个 剪枝题目,无论如何,做的再快,也只能过6个点,最后三个点也TLE.后来参考了一下标答,大概思路是这样的. 朴素算法就不多说了,枚举a,b然后判断就行,网上说这样优化到位 ...

  9. 用记事本编写C#程序并运行C#代码

    net framework自带有C#编译器 csc.exe,用它就好了 它在.NET框架目录下的<\Microsoft.NET\Framework\v**** (*号内容与版本有关) 不行你直接 ...

  10. android学习—should use @string resource警告

    在布局文件中,文本的设置使用如下写法时会有警告:Hardcoded string "BUTTON", should use @string resource <Button ...