------siwuxie095

1、Intent过滤器 intent-filter 相关选项

如果多个Activity拥有同样的action,在启动时这个action时的情况:

首先在LearnIntent下new一个 Empty Activity:MyAty1,

在其对应的布局中添加一个TextView,起标识作用

在AndroidManifest.xml中,先去掉MyAty的activity中的 android:exported="false",

为 MyAty 和 MyAty1 的 activity 添加 label 属性,这样在后续显示时就采用label中的名字,

在MyAty1 的activity下添加 intent-filter,再在Intent-filter下添加 category 和 action,

category设置为默认,action则设置成和MyAty的action一样,如下:

<?xml
version="1.0"
encoding="utf-8"?>

<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" android:label="MyAty">

<intent-filter>

<category android:name="android.intent.category.DEFAULT"
/>

<action android:name="com.siwuxie095.learnintent.intent.action.MyAty"
/>

</intent-filter>

</activity>

<activity android:name=".MyAty1" android:label="MyAty1">

<intent-filter>

<category android:name="android.intent.category.DEFAULT"
/>

<action android:name="com.siwuxie095.learnintent.intent.action.MyAty"
/>

</intent-filter>

</activity>

</application>

</manifest>

运行App1,一览:

若选择某个后点击"始终",以后打开App1,就不会再弹出这个选择界面,

可以进入LearnIntent的设置里,清除默认操作即可

对于隐式Intent,在启动时除了 action 单独匹配的方式之外,还可以加上其他的匹配方式

在intent-filter下,添加data标签,

各种可匹配的属性,这里选择属性scheme为:app(即协议是app)

最后:

<activity android:name=".MyAty1" android:label="MyAty1">

<intent-filter>

<category android:name="android.intent.category.DEFAULT"
/>

<action android:name="com.siwuxie095.learnintent.intent.action.MyAty"
/>

<data android:scheme="app"
/>

</intent-filter>

</activity>

那么在启动App1时,如果指明要启动的是MyAty1,只需对App1的MainActivity.java

中的startActivity()略作修改

findViewById(R.id.btnStartMyAty).setOnClickListener(new View.OnClickListener() {

@Override

public
void onClick(View v) {

try{

// app:和scheme协议中的保持一致
双斜杠后的是参数,任意即可

startActivity(new Intent("com.siwuxie095.learnintent.intent.action.MyAty", Uri.parse("app://hello")));

}catch (Exception e){

//提示信息 LENGTH_SHORT 短时呈现

Toast.makeText(MainActivity.this,"无法启动指定的Activity",Toast.LENGTH_SHORT).show();

}

}

});

运行,不会再出现选择对话框,直接启动指明协议的Activity:MyAty1

2、通过浏览器链接启动本地Activity

创建一个新项目:LaunchLocalActivity,选择API:21 Android 5.0,选择Empty Activity

再new一个Empty Activity:LocalAppAty

(整个过程用不上MainActivity,所以不用理会MainActivity.java和activity_main.xml)

工程结构目录一览:

AndroidManifest.xml中LocalAppAty的配置:

<?xml
version="1.0"
encoding="utf-8"?>

<manifest xmlns:android="http://schemas.android.com/apk/res/android"

package="com.siwuxie095.launchlocalapp">

<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=".LocalAppAty">

<intent-filter>

<!-- 可被浏览器启动的
可浏览的 -->

<category android:name="android.intent.category.BROWSABLE"
/>

<!-- 因为是Activity 需要一个DEFAULT -->

<category android:name="android.intent.category.DEFAULT"
/>

<!-- 浏览器链接被点击后,会发送一个action:VIEW -->

<action android:name="android.intent.action.VIEW"
/>

<!-- 配置data属性
协议名为app -->

<data android:scheme="app"
/>

</intent-filter>

</activity>

</application>

</manifest>

在LocalAppAty.java中获取传入参数:

package com.siwuxie095.launchlocalapp;

import android.net.Uri;

import android.support.v7.app.AppCompatActivity;

import android.os.Bundle;

public class LocalAppAty extends AppCompatActivity {

@Override

protected
void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_local_app_aty);

//接受传入参数:getIntent()获取启动这个Activity的Intent对象,

// 再通过getData() 获取到与这个Intent相关的数据对象,是Uri类型的对象

//注意是android.net类型的Uri,不是java.net类型的URI

Uri uri=getIntent().getData();

//输出为 app://hello

System.out.println(uri);

}

}

在布局文件layout中的activity_local_app_aty.xml中添加一个TextView,

并修改text:

<?xml
version="1.0"
encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

xmlns:tools="http://schemas.android.com/tools"

android:id="@+id/activity_local_app_aty"

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.launchlocalapp.LocalAppAty">

<TextView

android:text="这是用于被浏览器链接启动的一个本地Activity"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_alignParentTop="true"

android:layout_alignParentStart="true"

android:layout_marginStart="103dp"

android:layout_marginTop="100dp"

android:id="@+id/textView"
/>

</RelativeLayout>

下面是如何从浏览器启动Activity:LocalAppAty

首先打开Eclipse EE(Eclipse for Java EE Developers),创建一个

Dynamic Web Project,再new一个JSP文件:index.jsp,如下:

<%@ page language="java" contentType="text/html; charset=UTF-8"

pageEncoding="UTF-8"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

<title>链接启动Activity</title>

<style type="text/css">

a{

font-size: 50pt;

}

</style>

</head>

<body>

<!-- 跳转地址是app, //后是参数,随便写一个 -->

<p align="center">

<a href="app://hello">Launch My App</a>

</p>

</body>

</html>

实际上主要就是下面sublime中的代码,因为此过程需要利用Tomcat,才用的Eclipse

先将上面的JSP文件:index.jsp,运行在Tomcat服务器,再将电脑和手机连入同一个WiFi,

电脑打开命令行,输入ipconfig,查看路由器分配给电脑的IP地址,这里是:192.168.2.104,

替换掉localhost,即可在手机浏览器访问。

电脑:localhost:8080/Demo/index.jsp,手机:192.168.2.104:8080/Demo/index.jsp

手机UC浏览器打开一览:

(1)点击链接前

(2)点击链接后

(3)启动LocalAppAty后

【made by siwuxie095】

Intent的概念及应用(二)的更多相关文章

  1. 在Android中Intent的概念及应用(二)——Intent过滤器相关选项

    一.如果多个Activity拥有同一个Intent Action,启动时用同一个Action启动会是什么情况? 如何指定某一个Activity启动? 在多个Activity拥有同一个Intent Ac ...

  2. 在 Android 中 Intent 的概念及应用

    一.显式Intent: startActivity(new Intent(MainActivity.this, 类名.class));   二.隐式Intent: 1.在AndroidManiFest ...

  3. 【Android】12.1 Intent基本概念

    分类:C#.Android.VS2015: 创建日期:2016-02-23 一.简介 Intent:意图,含义就是你想利用它调用哪个组件实现相关的功能,比如调用相机组件实现拍照.调用Contact组件 ...

  4. 在Android中Intent的概念及应用(一)——显示Intent和隐式Intent

    Intent寻找目标组件的两种方式: 显式Intent:通过指定Intent组件名称来实现的,它一般用在知道目标组件名称的前提下,一般是在相同的应用程序内部实现的. 隐式Intent:通过Intent ...

  5. C功底挑战Java菜鸟入门概念干货(二)

    (接上篇博文:C功底挑战Java菜鸟入门概念干货(一)) 一.Java面向对象程序设计-类的基本形式 1.“类”是把事物的数据与相关的功能封装在一起,形成的一种特殊结构,用以表达对真实世界的一种抽象概 ...

  6. 面向对象【day07】:面向对象概念介绍(二)

    本节内容 1.概念 2.特性 3.面向对象介绍 一丶概念 1.面向对象编程 OOP(Object-Oriented Programming)编程是利用“类”和“对象”来创建各种模型来实现对真实世界的描 ...

  7. 【C#小知识】C#中一些易混淆概念总结(二)--------构造函数,this关键字,部分类,枚举 分类: C# 2014-02-03 01:24 1576人阅读 评论(0) 收藏

    目录: [C#小知识]C#中一些易混淆概念总结--------数据类型存储位置,方法调用,out和ref参数的使用 继上篇对一些C#概念问题进行细节的剖析以后,收获颇多.以前,读书的时候,一句话一掠而 ...

  8. Istio的流量管理(概念)(istio 系列二)

    Istio的流量管理(概念) 目录 Istio的流量管理(概念) 概述 Virtual services 为什么使用virtual service Virtual services举例 hosts字段 ...

  9. Intent的概念及应用(一)

    ------siwuxie095 1.显式Intent (1)先创建一个项目:LearnIntent,选择API:21 Android 5.0, 选择Empty Activity,完成 (2)创建一个 ...

随机推荐

  1. php获取文件名

    php获取文件名$phpself =$_SERVER['PHP_SELF']; //获取当前文件名$str = end(explode("/",$phpself)); //去掉'/ ...

  2. Zabbix之配置文件详解

    zabbix的配置文件一般有三种:zabbixserver的配置文件zabbix_server.confzabbixproxy的配置文件zabbix_proxy.confzabbix_agentd的配 ...

  3. C#中String和stringBuilder的区别

    Stringbuilder类是直接用于字符串操作的类,打个比方把(1)string aa="123456";(2)aa+="789"; (3)StringBui ...

  4. AngularJS中的$http.post与jQuery.post的区别

    原文:http://my.oschina.net/tommyfok/blog/287748 很多时候我们需要用ajax提交post数据,angularjs与jq类似,也有封装好的post. 但是jQu ...

  5. Android Paint画笔及Color .

    引自:http://blog.csdn.net/q445697127/article/details/7736926 Paint paint = new Paint(); // 设置paint为无锯齿 ...

  6. ARM汇编指令集

    一.跳转指令.跳转指令用于实现程序流程的跳转,在ARM程序中有以下两种方法可以实现程序流程的跳转. Ⅰ.使用专门的跳转指令.Ⅱ.直接向程序计数器PC写入跳转地址值. 通过向程序计数器PC写入跳转地址值 ...

  7. DRAM Memory Rank知识

    DRAM的一些知识点,先记录下来再进行整理 1.何为Memory rank? A memory rank is a set of DRAM chips connected to the same ch ...

  8. MyBatis基础用法(一)

    <select id="getErrorTimes" resultType="Integer"> SELECT ErrorTimes FROM `e ...

  9. Nexus搭建私服 学习

    为什么要搭建nexus私服 因为有些公司不提供外网给项目组人员,因此就不能使用maven访问远程的仓库地址,所以,有必要再局域网里找一台有外网权限的机器.搭建nexus私服,然后开发人员连接到这台私服 ...

  10. 省市二级联动--使用app-jquery-cityselect.js插件

    只有省市二级联动,三级联动还没处理好,会尽快完善. 嵌入id: <div class="form-group"> <label>地址</label&g ...