zoukankan      html  css  js  c++  java
  • Intent的概念及应用(二)

    ------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" />

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

    <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()获取启动这个ActivityIntent对象,

    // 再通过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】

       

  • 相关阅读:
    大象中原
    自己动手,编写神经网络程序,解决Mnist问题,并网络化部署-网络化部署
    自己动手,编写神经网络程序,解决Mnist问题,并网络化部署-编写网络
    py4CV例子2.5车牌识别和svm算法重构
    py4CV例子3Mnist识别和ANN
    【CC评网】2013.第41周 不求排版,简单就好
    【CC评网】2013.第39周 漂亮的作息表
    【CC评网】2013.第38周 要阅读 要有好工具
    书评 《软件随想录》
    BerkeleyDB 多索引查询
  • 原文地址:https://www.cnblogs.com/siwuxie095/p/6288012.html
Copyright © 2011-2022 走看看