Intent的概念及应用(二)
来源:互联网 发布:java框架面试题及答案 编辑:程序博客网 时间:2024/05/16 08:02
------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一样,如下:
<?xmlversion="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
publicvoid 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的配置:
<?xmlversion="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 LocalAppAtyextends AppCompatActivity {
@Override
protectedvoid 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:
<?xmlversion="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的概念及应用(二)
- Intent的概念及应用(一)
- 在 Android 中 Intent 的概念及应用
- 在 Android 中 Intent 的概念及应用
- 在 Android 中 Intent 的概念及应用
- 《android 学习》三、Intent 的概念及应用
- intent的概念及其应用
- Android之Intent的概念与应用
- Intent的概念及应用,以及Intentfilter过滤器的选项配置
- Intent的应用(二)——数据传递
- Android_实现页面跳转(intent的应用)及进阶
- 电子商务的概念及应用
- 中间件的概念及应用
- Intent的应用(初识intent)
- Intent的应用(初识intent)
- Intent的概念
- 数据库的本质、概念及其应用实践(二)
- web service的概念及应用场合(转载)
- UVaLive 3938 "Ray, Pass me the dishes!" (线段树求动态最大连续和)
- OpenCV--阈值分割-threshold()
- java实现DH非对称加密工具包
- 使用U盘安装ubuntu 16.04简易教程
- Unity&Shader高级篇-渲染路径(Rendering Paths)
- Intent的概念及应用(二)
- ScrollView源码分析
- 算法提高 c++_ch02_03
- Nuxt.js中使用Element-UI填坑
- 消息队列接口的简单封装
- 单源最短路 SPFA 算法模板
- POJ 2229 Sumsets(DP计数问题)
- 他们的面试风尚一
- 我读《基于WebRTC的无线实时通信QoS-QoE评估与预测》